Skip to content

Commit

Permalink
Merge pull request #16 from krauselukas/unlock
Browse files Browse the repository at this point in the history
Add Unlock Method
  • Loading branch information
vpereira committed Feb 26, 2021
2 parents a3ac829 + bde895f commit f8d2d34
Show file tree
Hide file tree
Showing 6 changed files with 2,108 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/obs_github_deployments/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def lock(reason:)
true if create_and_set_state(state: "queued", payload: payload_reason(reason: reason))
end

def unlock
deployment_status = latest_status

if deployment_status.blank? || deployment_status.state != "queued"
raise ObsGithubDeployments::Deployment::NothingToUnlockError
end

add_state(deployment: latest, state: "inactive")
true
end

private

def all
Expand All @@ -59,7 +70,10 @@ def create(payload:)
end

def add_state(deployment:, state:)
@client.create_deployment_status(deployment.url, state, { accept: "application/vnd.github.flash-preview+json" })
options = { accept: "application/vnd.github.flash-preview+json" }
options[:accept] = "application/vnd.github.ant-man-preview+json" if state == "inactive"

@client.create_deployment_status(deployment.url, state, options)
end

def create_and_set_state(state:, payload:)
Expand Down
11 changes: 11 additions & 0 deletions lib/obs_github_deployments/deployment/nothing_to_unlock_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module ObsGithubDeployments
class Deployment
class NothingToUnlockError < StandardError
def message
"Current deployment is not locked, nothing to do here"
end
end
end
end

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions spec/obs_github_deployments/deployment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,41 @@
end
end
end

describe "unlock" do
context "with an deployment not in the locked state" do
before do
subject.send :create_and_set_state, state: "success", payload: nil
end

it "throws an exception" do
expect { subject.unlock }.to raise_error(
ObsGithubDeployments::Deployment::NothingToUnlockError
)
end
end

context "with an deployment without any state set" do
before do
subject.send :create, payload: nil
end

it "throws an exception" do
expect { subject.unlock }.to raise_error(
ObsGithubDeployments::Deployment::NothingToUnlockError
)
end
end

context "with a locked deployment in place" do
before do
subject.send :create_and_set_state, state: "queued", payload: nil
end

it "unlocks the deployment and returns true" do
expect(subject.unlock).to eq(true)
expect(subject.send(:latest_status).state).to eq("inactive")
end
end
end
end

0 comments on commit f8d2d34

Please sign in to comment.