|
18 | 18 | } |
19 | 19 | end |
20 | 20 |
|
| 21 | + let(:saved_reviewers) {[{"type" => "Team", "reviewer" => {"id" => 123, "slug" => "managers"}}]} |
| 22 | + let(:saved_environment) do |
| 23 | + { |
| 24 | + "can_admins_bypass" => true, |
| 25 | + "deployment_branch_policy" => {"protected_branches" => false, "custom_branch_policies" => true}, |
| 26 | + "protection_rules" => [ |
| 27 | + {"type" => "branch_policy"}, |
| 28 | + {"type" => "required_reviewers", "prevent_self_review" => false, "reviewers" => saved_reviewers}, |
| 29 | + ], |
| 30 | + } |
| 31 | + end |
| 32 | + |
21 | 33 | before do |
22 | 34 | Bake::Gem::GitHub::Setup.new(root).generate(repository: "socketry/example", checks: ["Tests"], reviewers: ["socketry/managers"]) |
23 | 35 | project.responses["repos/socketry/example/rulesets?per_page=100"] = [] |
24 | 36 | project.responses["repos/socketry/example/environments"] = {"environments" => [environment]} |
25 | | - project.responses["repos/socketry/example/environments/rubygems"] = environment |
26 | | - project.responses["orgs/socketry/teams/managers"] = {"id" => 123} |
| 37 | + project.responses["repos/socketry/example/environments/rubygems"] = -> do |
| 38 | + project.writes.any?{|payload| payload.key?("reviewers")} ? saved_environment : environment |
| 39 | + end |
| 40 | + project.responses["orgs/socketry/teams/managers"] = {"id" => 123, "privacy" => "closed"} |
27 | 41 | end |
28 | 42 |
|
29 | 43 | with "#doctor" do |
|
53 | 67 | with "#apply" do |
54 | 68 | it "updates reviewers without weakening existing environment protections" do |
55 | 69 | environment["can_admins_bypass"] = false |
| 70 | + saved_environment["can_admins_bypass"] = false |
| 71 | + saved_environment["protection_rules"] << {"type" => "wait_timer", "wait_timer" => 30} |
| 72 | + saved_environment["protection_rules"].find{|rule| rule["type"] == "required_reviewers"}["prevent_self_review"] = true |
56 | 73 | environment["protection_rules"] += [ |
57 | 74 | {"type" => "wait_timer", "wait_timer" => 30}, |
58 | 75 | {"type" => "required_reviewers", "prevent_self_review" => true, "reviewers" => [{"type" => "User", "reviewer" => {"id" => 456}}]}, |
|
66 | 83 | "deployment_branch_policy" => environment.fetch("deployment_branch_policy"), |
67 | 84 | "reviewers" => [{"type" => "Team", "id" => 123}], |
68 | 85 | } |
69 | | - expect(project.requests.last[0, 5]).to be == ["gh", "api", "repos/socketry/example/environments/rubygems", "--method", "PUT"] |
| 86 | + expect(project.requests.last).to be == ["gh", "api", "repos/socketry/example/environments/rubygems"] |
70 | 87 | expect(project.writes.size).to be == 5 |
71 | 88 | end |
72 | 89 |
|
73 | 90 | it "preserves an unrestricted branch policy and enabled administrator bypass" do |
74 | 91 | environment["deployment_branch_policy"] = nil |
| 92 | + saved_environment["deployment_branch_policy"] = nil |
75 | 93 | project.apply |
76 | 94 |
|
77 | 95 | expect(project.writes.last).to have_keys("deployment_branch_policy" => be_nil, "can_admins_bypass" => be == true) |
|
85 | 103 | expect(project.writes.all?{|payload| payload.key?("rules")}).to be == true |
86 | 104 | end |
87 | 105 |
|
| 106 | + it "accepts reordered reviewers and ignores duplicate configured identities" do |
| 107 | + project.config["reviewers"] = ["ioquatix", "socketry/managers", "ioquatix"] |
| 108 | + project.responses["users/ioquatix"] = {"id" => 456} |
| 109 | + saved_reviewers.unshift({"type" => "User", "reviewer" => {"id" => 456}}) |
| 110 | + |
| 111 | + project.apply |
| 112 | + expect(project.writes.last.fetch("reviewers")).to be == [{"type" => "Team", "id" => 123}, {"type" => "User", "id" => 456}] |
| 113 | + |
| 114 | + project.apply |
| 115 | + expect(project.writes.count{|payload| payload.key?("reviewers")}).to be == 1 |
| 116 | + end |
| 117 | + |
| 118 | + it "rejects a successful write which silently drops every reviewer" do |
| 119 | + saved_reviewers.clear |
| 120 | + |
| 121 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain.*rubygems.*gem:github:setup:plan/) |
| 122 | + expect(project.writes.last.fetch("reviewers")).to be == [{"type" => "Team", "id" => 123}] |
| 123 | + end |
| 124 | + |
| 125 | + it "rejects a successful write which drops one of the requested reviewers" do |
| 126 | + project.config["reviewers"] << "ioquatix" |
| 127 | + project.responses["users/ioquatix"] = {"id" => 456} |
| 128 | + |
| 129 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain/) |
| 130 | + end |
| 131 | + |
| 132 | + it "rejects an unexpected reviewer who could approve publication" do |
| 133 | + saved_reviewers << {"type" => "User", "reviewer" => {"id" => 456}} |
| 134 | + |
| 135 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain/) |
| 136 | + end |
| 137 | + |
| 138 | + it "distinguishes user and team reviewers with the same numeric ID" do |
| 139 | + saved_reviewers.first["type"] = "User" |
| 140 | + |
| 141 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain/) |
| 142 | + end |
| 143 | + |
| 144 | + it "rejects a successful write which removes the reviewer protection rule" do |
| 145 | + saved_environment["protection_rules"].reject!{|rule| rule["type"] == "required_reviewers"} |
| 146 | + |
| 147 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain/) |
| 148 | + end |
| 149 | + |
| 150 | + it "rejects a successful write which changes an existing protection" do |
| 151 | + environment["can_admins_bypass"] = false |
| 152 | + |
| 153 | + expect{project.apply}.to raise_exception(RuntimeError, message: be =~ /did not retain/) |
| 154 | + expect(project.writes.last.fetch("can_admins_bypass")).to be == false |
| 155 | + end |
| 156 | + |
| 157 | + it "propagates failure to read back the applied environment" do |
| 158 | + project.responses["repos/socketry/example/environments/rubygems"] = -> do |
| 159 | + raise "GitHub readback failed" unless project.writes.empty? |
| 160 | + environment |
| 161 | + end |
| 162 | + |
| 163 | + expect{project.apply}.to raise_exception(RuntimeError, message: be == "GitHub readback failed") |
| 164 | + expect(project.writes.size).to be == 5 |
| 165 | + end |
| 166 | + |
88 | 167 | it "leaves environment settings unmanaged when reviewers are omitted" do |
89 | 168 | project.config.delete("reviewers") |
90 | 169 | project.apply |
|
125 | 204 |
|
126 | 205 | it "escapes the configured environment name in API requests" do |
127 | 206 | project.config["environment"] = "release / gems" |
128 | | - project.responses["repos/socketry/example/environments/release%20%2F%20gems"] = environment |
| 207 | + project.responses["repos/socketry/example/environments/release%20%2F%20gems"] = project.responses.fetch("repos/socketry/example/environments/rubygems") |
129 | 208 | project.apply |
130 | 209 |
|
131 | 210 | expect(project.requests.last.fetch(2)).to be == "repos/socketry/example/environments/release%20%2F%20gems" |
132 | 211 | end |
133 | 212 | end |
| 213 | + |
| 214 | + [:doctor, :apply].each do |operation| |
| 215 | + with "##{operation}" do |
| 216 | + it "rejects secret reviewer teams before changing any settings" do |
| 217 | + project.responses["orgs/socketry/teams/managers"]["privacy"] = "secret" |
| 218 | + |
| 219 | + expect{project.public_send(operation)}.to raise_exception(RuntimeError, message: be =~ /socketry\/managers is Secret.*Visible/) |
| 220 | + expect(project.writes).to be == [] |
| 221 | + end |
| 222 | + end |
| 223 | + end |
134 | 224 | end |
0 commit comments