Skip to content

Commit 40e4ae9

Browse files
committed
Fix latest issues
1 parent 308e22f commit 40e4ae9

File tree

3 files changed

+131
-13
lines changed

3 files changed

+131
-13
lines changed

exe/decidim-action-backporter

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
require "thor"
55

66
require_relative "../lib/decidim/maintainers_toolbox/github_manager/querier"
7-
require_relative "../lib/decidim/maintainers_toolbox/github_manager/poster"
8-
require_relative "../lib/decidim/maintainers_toolbox/git_backport_manager"
97
require_relative "../lib/decidim/maintainers_toolbox/action_backporter"
108

119
class ActionBackporterCLI < Thor

lib/decidim/maintainers_toolbox/action_backporter.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# frozen_string_literal: true
22

3+
require_relative "github_manager/querier"
4+
require_relative "github_manager/poster"
5+
require_relative "git_backport_manager"
6+
37
module Decidim
48
module MaintainersToolbox
59
class ActionBackporter
@@ -15,29 +19,27 @@ def initialize(token: , pull_request_id: ,exit_with_unstaged_changes: )
1519
end
1620

1721
def call
18-
pp pull_request_metadata
19-
2022
exit_with_errors("The requested PR #{pull_request_id} does not contain `type: fix`") unless pull_request_metadata[:labels].include?("type: fix")
2123
exit_with_errors("The requested PR #{pull_request_id} is not merged") unless pull_request_metadata[:is_merged]
2224
exit_with_errors("The requested PR #{pull_request_id} cannot be backported") if pull_request_metadata[:labels].include?("no-backport")
2325

24-
#
25-
# extract_versions.each do |version|
26-
# next if extract_backport_pull_request_for_version(related_issues, version)
27-
# # `decidim-backporter --github_token=#{token} --pull_request_id=#{pull_request_id} --version_number=#{version} --exit_with_unstaged_changes=#{exit_with_unstaged_changes} --with-console=false`
28-
# # create_backport_task(version) unless $CHILD_STATUS.exitstatus.zero?
29-
# end
26+
extract_versions.each do |version|
27+
next if extract_backport_pull_request_for_version(related_issues, version)
28+
system("decidim-backporter --github_token=#{token} --pull_request_id=#{pull_request_id} --version_number=#{version} --exit_with_unstaged_changes=#{exit_with_unstaged_changes} --with-console=false", exception: true)
29+
rescue RuntimeError => e
30+
puts e.message
31+
create_backport_task(version)
32+
end
3033
end
3134

3235
private
3336

3437
attr_reader :token, :pull_request_id, :exit_with_unstaged_changes
3538

3639
def create_backport_task(version)
37-
pp pull_request_metadata
3840
some_params = {
39-
title: "Backport of \"#{pull_request_metadata[:title]}\" failed for version #{version}",
40-
body: "Backport of ##{pull_request_id} failed for version #{version}",
41+
title: "Fail: automatic backport of \"#{pull_request_metadata[:title]}\"",
42+
body: "Automatic backport of ##{pull_request_id} has failed for version #{version}. Please do this action manually.",
4143
assignee: "alecslupu",
4244
labels: pull_request_metadata[:labels]
4345
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# frozen_string_literal: true
2+
3+
require "decidim/maintainers_toolbox/action_backporter"
4+
require "webmock/rspec"
5+
6+
describe Decidim::MaintainersToolbox::ActionBackporter do
7+
8+
subject { described_class.new(token: token, pull_request_id: pull_request_id, exit_with_unstaged_changes: exit_with_unstaged_changes) }
9+
10+
let(:token) { "1234" }
11+
let(:pull_request_id) { 123 }
12+
let(:exit_with_unstaged_changes) { true }
13+
14+
before do
15+
stub_request(:get, "https://api.github.com/repos/decidim/decidim/issues/123").
16+
to_return(status: 200, body: '{"number": 12345, "pull_request": {"merged_at": "" }, "title": "Fix whatever", "labels": [{"name": "type: fix"}, {"name": "module: admin"}]}', headers: {})
17+
18+
stub_request(:post, "https://api.github.com/repos/decidim/decidim/issues")
19+
.to_return(status: 200, body: "{}", headers: {})
20+
end
21+
22+
describe ".exit_with_errors" do
23+
it "exit with a custom message" do
24+
expect { subject.send(:exit_with_errors, "Bye") }.to raise_error(SystemExit).and output(/Bye/).to_stdout
25+
end
26+
end
27+
28+
describe ".call" do
29+
it "exists when the PR is not a fix" do
30+
allow(subject).to receive(:pull_request_metadata).and_return({labels: ["type: change"], is_merged: true })
31+
expect{ subject.call }.to raise_error(SystemExit).and output(/does not contain `type: fix`/).to_stdout
32+
end
33+
34+
it "exists when the PR is not merged" do
35+
allow(subject).to receive(:pull_request_metadata).and_return({labels: ["type: fix"], is_merged: false })
36+
expect{ subject.call }.to raise_error(SystemExit).and output(/is not merged/).to_stdout
37+
end
38+
39+
it "exists when the PR is not backportable" do
40+
allow(subject).to receive(:pull_request_metadata).and_return({labels: ["type: fix", "no-backport"], is_merged: true })
41+
expect{ subject.call }.to raise_error(SystemExit).and output(/cannot be backported/).to_stdout
42+
end
43+
44+
it "calls extract versions" do
45+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "release: v0.28", "release: v0.29"], is_merged: true })
46+
allow(subject).to receive(:extract_versions).and_return(["0.28", "0.29"])
47+
allow(subject).to receive(:related_issues).and_return([])
48+
allow(subject).to receive(:system).and_return(true)
49+
50+
expect(subject).to receive(:extract_versions)
51+
52+
subject.call
53+
end
54+
55+
it "runs the system command" do
56+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "release: v0.28", "release: v0.29"], is_merged: true })
57+
allow(subject).to receive(:extract_versions).and_return(["0.28", "0.29"])
58+
allow(subject).to receive(:related_issues).and_return([])
59+
60+
expect(subject).to receive(:system).with("decidim-backporter --github_token=#{token} --pull_request_id=#{pull_request_id} --version_number=0.28 --exit_with_unstaged_changes=#{exit_with_unstaged_changes} --with-console=false", exception: true)
61+
expect(subject).to receive(:system).with("decidim-backporter --github_token=#{token} --pull_request_id=#{pull_request_id} --version_number=0.29 --exit_with_unstaged_changes=#{exit_with_unstaged_changes} --with-console=false", exception: true)
62+
63+
subject.call
64+
end
65+
66+
it "skips the creation" do
67+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "release: v0.28", "release: v0.29"], is_merged: true })
68+
allow(subject).to receive(:extract_versions).and_return(["0.28", "0.29"])
69+
allow(subject).to receive(:related_issues).and_return([{title: "Backport 0.28"}, {title: "Backport 0.29"}])
70+
71+
expect(subject).to receive(:extract_backport_pull_request_for_version).with(kind_of(Array), "0.28").and_return({})
72+
expect(subject).to receive(:extract_backport_pull_request_for_version).with(kind_of(Array), "0.29").and_return({})
73+
74+
subject.call
75+
end
76+
77+
it "creates the ticket" do
78+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "release: v0.28", "release: v0.29"], is_merged: true })
79+
allow(subject).to receive(:extract_versions).and_return(["0.28", "0.29"])
80+
allow(subject).to receive(:related_issues).and_return([])
81+
allow(subject).to receive(:extract_backport_pull_request_for_version).with(kind_of(Array), "0.28").and_return(nil)
82+
allow(subject).to receive(:extract_backport_pull_request_for_version).with(kind_of(Array), "0.29").and_return(nil)
83+
84+
allow(subject).to receive(:system).and_raise(RuntimeError)
85+
86+
expect(subject).to receive(:create_backport_task).with("0.28")
87+
expect(subject).to receive(:create_backport_task).with("0.29")
88+
89+
subject.call
90+
end
91+
end
92+
93+
describe ".extract_versions" do
94+
it "returns the versions" do
95+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "release: v0.28", "release: v0.29"], is_merged: true })
96+
expect(subject.send(:extract_versions)).to eq(["0.28", "0.29"])
97+
expect(subject.send(:extract_versions).size).to eq(2)
98+
end
99+
100+
it "returns empty array" do
101+
allow(subject).to receive(:pull_request_metadata).and_return({ labels: ["type: fix", "team: documentation", "module: initiatives"], is_merged: true })
102+
expect(subject.send(:extract_versions)).to eq([])
103+
expect(subject.send(:extract_versions).size).to eq(0)
104+
end
105+
end
106+
107+
describe ".create_backport_task" do
108+
109+
before do
110+
allow(subject).to receive(:pull_request_metadata).and_return({ title: "Foo Bar", labels: ["type: fix", "release: v0.28"]})
111+
end
112+
113+
it "returns the respose from the server" do
114+
expect(subject.send(:create_backport_task, "0.29")).to be_a Faraday::Response
115+
end
116+
end
117+
118+
end

0 commit comments

Comments
 (0)