Skip to content

Commit f6f9924

Browse files
committed
Add deployment status support.
This commit contains the code needed to create and query deployment statuses.
1 parent 2af8f44 commit f6f9924

5 files changed

Lines changed: 248 additions & 1 deletion

File tree

lib/github_api/client/repos/deployments.rb

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def list(*args)
3232
end
3333
alias :all :list
3434

35-
# List commits on a repository
35+
# Create a deployment
3636
#
3737
# = Parameters
3838
# * <tt>:ref</tt> Required string. Sha or branch to start listing commits from.
@@ -62,5 +62,48 @@ def create(*args)
6262
post_request("repos/#{user}/#{repo}/deployments", params)
6363
end
6464

65+
# List the statuses of a deployment.
66+
#
67+
# = Parameters
68+
# * <tt>:id</tt> Required string. Id of the deployment being queried.
69+
# = Examples
70+
# github = Github.new
71+
# github.repos.deployments.statuses 'user-name', 'repo-name', DEPLOYMENT_ID
72+
# github.repos.deployments.statuses 'user-name', 'repo-name', DEPLOYMENT_ID { |status| ... }
73+
#
74+
def statuses(*args)
75+
arguments(args, :required => [:user, :repo, :id])
76+
77+
params = arguments.params
78+
params['accept'] ||= PREVIEW_MEDIA
79+
80+
statuses = get_request("repos/#{user}/#{repo}/deployments/#{id}/statuses", params)
81+
return statuses unless block_given?
82+
83+
statuses.each { |status| yield status }
84+
end
85+
86+
# Create a deployment status
87+
#
88+
# = Parameters
89+
# * <tt>:id</tt> Required string. Id of the deployment being referenced.
90+
# * <tt>:state</tt> Required string. State of the deployment. Can be one of: pending, success, error, or failure.
91+
# * <tt>:target_url</tt> Optional string. The URL associated with the status.
92+
# * <tt>:description</tt> Optional string. A short description of the status.
93+
# = Examples
94+
# github = Github.new
95+
# github.repos.deployments.create_status 'user-name', 'repo-name', DEPLOYMENT_ID, :state => '...'
96+
#
97+
def create_status(*args)
98+
arguments(args, :required => [:user, :repo, :id]) do
99+
assert_required %w[ state ]
100+
end
101+
102+
params = arguments.params
103+
params['accept'] ||= PREVIEW_MEDIA
104+
105+
post_request("repos/#{user}/#{repo}/deployments/#{id}/statuses", params)
106+
end
107+
65108
end # Client::Repos::Deployments
66109
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952/statuses/12147",
3+
"id": 12147,
4+
"sha": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
5+
"ref": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
6+
"state": "success",
7+
"payload": {},
8+
"environment": "production",
9+
"description": null,
10+
"target_url": null,
11+
"creator": {
12+
"login": "nahiluhmot",
13+
"id": 1449846,
14+
"avatar_url": "https://avatars.githubusercontent.com/u/1449846?",
15+
"gravatar_id": "bf94307804fd07054f5c3c7cbd2112fb",
16+
"url": "https://api.github.com/users/nahiluhmot",
17+
"html_url": "https://github.com/nahiluhmot",
18+
"followers_url": "https://api.github.com/users/nahiluhmot/followers",
19+
"following_url": "https://api.github.com/users/nahiluhmot/following{/other_user}",
20+
"gists_url": "https://api.github.com/users/nahiluhmot/gists{/gist_id}",
21+
"starred_url": "https://api.github.com/users/nahiluhmot/starred{/owner}{/repo}",
22+
"subscriptions_url": "https://api.github.com/users/nahiluhmot/subscriptions",
23+
"organizations_url": "https://api.github.com/users/nahiluhmot/orgs",
24+
"repos_url": "https://api.github.com/users/nahiluhmot/repos",
25+
"events_url": "https://api.github.com/users/nahiluhmot/events{/privacy}",
26+
"received_events_url": "https://api.github.com/users/nahiluhmot/received_events",
27+
"type": "User",
28+
"site_admin": false
29+
},
30+
"created_at": "2014-05-09T21:26:46Z",
31+
"updated_at": "2014-05-09T21:26:46Z",
32+
"deployment_url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952"
33+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[
2+
{
3+
"url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952/statuses/12148",
4+
"id": 12148,
5+
"sha": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
6+
"ref": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
7+
"state": "success",
8+
"payload": {},
9+
"environment": "production",
10+
"description": null,
11+
"target_url": null,
12+
"creator": {
13+
"login":"nahiluhmot",
14+
"id": 1449846,
15+
"avatar_url": "https://avatars.githubusercontent.com/u/1449846?",
16+
"gravatar_id": "bf94307804fd07054f5c3c7cbd2112fb",
17+
"url": "https://api.github.com/users/nahiluhmot",
18+
"html_url": "https://github.com/nahiluhmot",
19+
"followers_url": "https://api.github.com/users/nahiluhmot/followers",
20+
"following_url": "https://api.github.com/users/nahiluhmot/following{/other_user}",
21+
"gists_url": "https://api.github.com/users/nahiluhmot/gists{/gist_id}",
22+
"starred_url": "https://api.github.com/users/nahiluhmot/starred{/owner}{/repo}",
23+
"subscriptions_url": "https://api.github.com/users/nahiluhmot/subscriptions",
24+
"organizations_url": "https://api.github.com/users/nahiluhmot/orgs",
25+
"repos_url": "https://api.github.com/users/nahiluhmot/repos",
26+
"events_url": "https://api.github.com/users/nahiluhmot/events{/privacy}",
27+
"received_events_url": "https://api.github.com/users/nahiluhmot/received_events",
28+
"type": "User",
29+
"site_admin": false
30+
},
31+
"created_at": "2014-05-09T21:30:28Z",
32+
"updated_at": "2014-05-09T21:30:28Z",
33+
"deployment_url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952"
34+
},
35+
{
36+
"url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952/statuses/12147",
37+
"id": 12147,
38+
"sha": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
39+
"ref": "a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b",
40+
"state": "success",
41+
"payload": {},
42+
"environment": "production",
43+
"description": null,
44+
"target_url": null,
45+
"creator": {
46+
"login":"nahiluhmot",
47+
"id": 1449846,
48+
"avatar_url": "https://avatars.githubusercontent.com/u/1449846?",
49+
"gravatar_id": "bf94307804fd07054f5c3c7cbd2112fb",
50+
"url": "https://api.github.com/users/nahiluhmot",
51+
"html_url": "https://github.com/nahiluhmot",
52+
"followers_url": "https://api.github.com/users/nahiluhmot/followers",
53+
"following_url": "https://api.github.com/users/nahiluhmot/following{/other_user}",
54+
"gists_url": "https://api.github.com/users/nahiluhmot/gists{/gist_id}",
55+
"starred_url": "https://api.github.com/users/nahiluhmot/starred{/owner}{/repo}",
56+
"subscriptions_url": "https://api.github.com/users/nahiluhmot/subscriptions",
57+
"organizations_url": "https://api.github.com/users/nahiluhmot/orgs",
58+
"repos_url": "https://api.github.com/users/nahiluhmot/repos",
59+
"events_url": "https://api.github.com/users/nahiluhmot/events{/privacy}",
60+
"received_events_url": "https://api.github.com/users/nahiluhmot/received_events",
61+
"type": "User",
62+
"site_admin": false
63+
},
64+
"created_at": "2014-05-09T21:26:46Z",
65+
"updated_at": "2014-05-09T21:26:46Z",
66+
"deployment_url": "https://api.github.com/repos/nahiluhmot/github/deployments/10952"
67+
}
68+
]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Client::Repos::Deployments, '#create_status' do
6+
let(:user) { 'nahiluhmot' }
7+
let(:repo) { 'github' }
8+
let(:id) { 12147 }
9+
let(:request_path) { "/repos/#{user}/#{repo}/deployments/#{id}/statuses" }
10+
let(:params) {
11+
{
12+
'state' => 'success'
13+
}
14+
}
15+
16+
before {
17+
stub_post(request_path).with(params).
18+
to_return(:body => body, :status => status,
19+
:headers => {:content_type => "application/json; charset=utf-8"})
20+
}
21+
22+
after { reset_authentication_for(subject) }
23+
24+
context "resouce created" do
25+
let(:body) { fixture('repos/deployment_status.json') }
26+
let(:status) { 201 }
27+
28+
it "should fail to create resource if 'state' input is missing" do
29+
expect {
30+
subject.create_status user, repo, id, params.except('state')
31+
}.to raise_error(Github::Error::RequiredParams)
32+
end
33+
34+
it "should create resource successfully" do
35+
subject.create_status user, repo, id, params
36+
a_post(request_path).with(params).should have_been_made
37+
end
38+
39+
it "should return the resource" do
40+
status = subject.create_status user, repo, id, params
41+
status.should be_a Github::ResponseWrapper
42+
end
43+
44+
it "should get the deployment information" do
45+
status = subject.create_status user, repo, id, params
46+
status.state.should == 'success'
47+
end
48+
end
49+
50+
it_should_behave_like 'request failure' do
51+
let(:requestable) { subject.create_status user, repo, id, params }
52+
end
53+
54+
end # create_status
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# encoding: utf-8
2+
3+
require 'spec_helper'
4+
5+
describe Github::Client::Repos::Deployments, '#statuses' do
6+
let(:user) { 'nahiluhmot' }
7+
let(:repo) { 'github' }
8+
let(:id) { 12147 }
9+
let(:request_path) { "/repos/#{user}/#{repo}/deployments/#{id}/statuses" }
10+
11+
before {
12+
stub_get(request_path).to_return(:body => body, :status => status,
13+
:headers => {:content_type => "application/json; charset=utf-8"})
14+
}
15+
16+
after { reset_authentication_for(subject) }
17+
18+
context "resource found" do
19+
let(:body) { fixture('repos/deployments.json') }
20+
let(:status) { 200 }
21+
22+
it { expect { subject.statuses }.to raise_error(ArgumentError) }
23+
it { expect { subject user }.to raise_error(ArgumentError) }
24+
25+
it "should get the resources" do
26+
subject.statuses user, repo, id
27+
a_get(request_path).should have_been_made
28+
end
29+
30+
it_should_behave_like 'an array of resources' do
31+
let(:requestable) { subject.statuses user, repo, id }
32+
end
33+
34+
it "should get deployment information" do
35+
statuses = subject.statuses user, repo, id
36+
statuses.first.sha.should == 'a9a5ad01cf26b646e6f95bf9e2d13a2a155b5c9b'
37+
end
38+
39+
it "should yield to a block" do
40+
yielded = []
41+
result = subject.statuses(user, repo, id) { |obj| yielded << obj }
42+
yielded.should == result
43+
end
44+
end
45+
46+
it_should_behave_like 'request failure' do
47+
let(:requestable) { subject.statuses user, repo, id }
48+
end
49+
end # list

0 commit comments

Comments
 (0)