|
| 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