Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unlock Method #16

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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