Skip to content

Commit c491754

Browse files
dlapiduzwillnorris
authored andcommitted
Add support for deployment api
1 parent eb2a40b commit c491754

File tree

3 files changed

+264
-0
lines changed

3 files changed

+264
-0
lines changed

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ const (
3939

4040
// https://developer.github.com/changes/2014-08-05-team-memberships-api/
4141
mediaTypeMembershipPreview = "application/vnd.github.the-wasp-preview+json"
42+
43+
// https://developer.github.com/changes/2014-01-09-preview-the-new-deployments-api/
44+
mediaTypeDeploymentPreview = "application/vnd.github.cannonball-preview+json"
4245
)
4346

4447
// A Client manages communication with the GitHub API.

github/repos_deployments.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright 2014 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"encoding/json"
10+
"fmt"
11+
)
12+
13+
// Deployment represents a deployment in a repo
14+
type Deployment struct {
15+
Url *string `json:"url,omitempty"`
16+
ID *int `json:"id,omitempty"`
17+
SHA *string `json:"sha,omitempty"`
18+
Ref *string `json:"ref,omitempty"`
19+
Task *string `json:"task,omitempty"`
20+
Payload json.RawMessage `json:"payload,omitempty"`
21+
Environment *string `json:"environment,omitempty"`
22+
Description *string `json:"description,omitempty"`
23+
Creator *User `json:"creator,omitempty"`
24+
CreatedAt *Timestamp `json:"created_at,omitempty"`
25+
UpdatedAt *Timestamp `json:"pushed_at,omitempty"`
26+
}
27+
28+
// DeploymentRequest represents a deployment request
29+
type DeploymentRequest struct {
30+
Ref *string `json:"ref,omitempty"`
31+
Task *string `json:"task,omitempty"`
32+
AutoMerge *bool `json:"auto_merge,omitempty"`
33+
RequiredContexts []string `json:"required_contexts,omitempty"`
34+
Payload *string `json:"payload,omitempty"`
35+
Environment *string `json:"environment,omitempty"`
36+
Description *string `json:"description,omitempty"`
37+
}
38+
39+
// DeploymentsListOptions specifies the optional parameters to the
40+
// RepositoriesService.ListDeployments method.
41+
type DeploymentsListOptions struct {
42+
// SHA of the Deployment.
43+
SHA string `url:"sha,omitempty"`
44+
45+
// List deployments for a given ref.
46+
Ref string `url:"ref,omitempty"`
47+
48+
// List deployments for a given task.
49+
Task string `url:"task,omitempty"`
50+
51+
// List deployments for a given environment.
52+
Environment string `url:"environment,omitempty"`
53+
54+
ListOptions
55+
}
56+
57+
// ListDeployments lists the deployments of a repository.
58+
//
59+
// GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployments
60+
func (s *RepositoriesService) ListDeployments(owner, repo string, opt *DeploymentsListOptions) ([]Deployment, *Response, error) {
61+
u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
62+
u, err := addOptions(u, opt)
63+
if err != nil {
64+
return nil, nil, err
65+
}
66+
67+
req, err := s.client.NewRequest("GET", u, nil)
68+
if err != nil {
69+
return nil, nil, err
70+
}
71+
72+
// TODO: remove custom Accept header when this API fully launches
73+
req.Header.Set("Accept", mediaTypeDeploymentPreview)
74+
75+
deployments := new([]Deployment)
76+
resp, err := s.client.Do(req, deployments)
77+
if err != nil {
78+
return nil, resp, err
79+
}
80+
81+
return *deployments, resp, err
82+
}
83+
84+
// CreateDeployment creates a new deployment for a repository.
85+
//
86+
// GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment
87+
func (s *RepositoriesService) CreateDeployment(owner, repo string, request *DeploymentRequest) (*Deployment, *Response, error) {
88+
u := fmt.Sprintf("repos/%v/%v/deployments", owner, repo)
89+
90+
req, err := s.client.NewRequest("POST", u, request)
91+
if err != nil {
92+
return nil, nil, err
93+
}
94+
95+
// TODO: remove custom Accept header when this API fully launches
96+
req.Header.Set("Accept", mediaTypeDeploymentPreview)
97+
98+
d := new(Deployment)
99+
resp, err := s.client.Do(req, d)
100+
if err != nil {
101+
return nil, resp, err
102+
}
103+
104+
return d, resp, err
105+
}
106+
107+
// DeploymentStatus represents the status of a
108+
// particular deployment.
109+
type DeploymentStatus struct {
110+
ID *int `json:"id,omitempty"`
111+
State *string `json:"state,omitempty"`
112+
Creator *User `json:"creator,omitempty"`
113+
Description *string `json:"description,omitempty"`
114+
TargetUrl *string `json:"target_url,omitempty"`
115+
CreatedAt *Timestamp `json:"created_at,omitempty"`
116+
UpdatedAt *Timestamp `json:"pushed_at,omitempty"`
117+
}
118+
119+
// DeploymentRequest represents a deployment request
120+
type DeploymentStatusRequest struct {
121+
State *string `json:"state,omitempty"`
122+
TargetUrl *string `json:"target_url,omitempty"`
123+
Description *string `json:"description,omitempty"`
124+
}
125+
126+
// ListDeploymentStatuses lists the statuses of a given deployment of a repository.
127+
//
128+
// GitHub API docs: https://developer.github.com/v3/repos/deployments/#list-deployment-statuses
129+
func (s *RepositoriesService) ListDeploymentStatuses(owner, repo string, deployment int, opt *ListOptions) ([]DeploymentStatus, *Response, error) {
130+
u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
131+
u, err := addOptions(u, opt)
132+
if err != nil {
133+
return nil, nil, err
134+
}
135+
136+
req, err := s.client.NewRequest("GET", u, nil)
137+
if err != nil {
138+
return nil, nil, err
139+
}
140+
141+
// TODO: remove custom Accept header when this API fully launches
142+
req.Header.Set("Accept", mediaTypeDeploymentPreview)
143+
144+
statuses := new([]DeploymentStatus)
145+
resp, err := s.client.Do(req, statuses)
146+
if err != nil {
147+
return nil, resp, err
148+
}
149+
150+
return *statuses, resp, err
151+
}
152+
153+
// CreateDeploymentStatus creates a new status for a deployment.
154+
//
155+
// GitHub API docs: https://developer.github.com/v3/repos/deployments/#create-a-deployment-status
156+
func (s *RepositoriesService) CreateDeploymentStatus(owner, repo string, deployment int, request *DeploymentStatusRequest) (*DeploymentStatus, *Response, error) {
157+
u := fmt.Sprintf("repos/%v/%v/deployments/%v/statuses", owner, repo, deployment)
158+
159+
req, err := s.client.NewRequest("POST", u, request)
160+
if err != nil {
161+
return nil, nil, err
162+
}
163+
164+
// TODO: remove custom Accept header when this API fully launches
165+
req.Header.Set("Accept", mediaTypeDeploymentPreview)
166+
167+
d := new(DeploymentStatus)
168+
resp, err := s.client.Do(req, d)
169+
if err != nil {
170+
return nil, resp, err
171+
}
172+
173+
return d, resp, err
174+
}

github/repos_deployments_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2014 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"encoding/json"
10+
"fmt"
11+
"net/http"
12+
"reflect"
13+
"testing"
14+
)
15+
16+
func TestRepositoriesService_ListDeployments(t *testing.T) {
17+
setup()
18+
defer teardown()
19+
20+
mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) {
21+
testMethod(t, r, "GET")
22+
testFormValues(t, r, values{"environment": "test"})
23+
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
24+
})
25+
26+
opt := &DeploymentsListOptions{Environment: "test"}
27+
deployments, _, err := client.Repositories.ListDeployments("o", "r", opt)
28+
if err != nil {
29+
t.Errorf("Repositories.ListDeployments returned error: %v", err)
30+
}
31+
32+
want := []Deployment{{ID: Int(1)}, {ID: Int(2)}}
33+
if !reflect.DeepEqual(deployments, want) {
34+
t.Errorf("Repositories.ListDeployments returned %+v, want %+v", deployments, want)
35+
}
36+
}
37+
38+
func TestRepositoriesService_CreateDeployment(t *testing.T) {
39+
setup()
40+
defer teardown()
41+
42+
input := &DeploymentRequest{Ref: String("1111"), Task: String("deploy")}
43+
44+
mux.HandleFunc("/repos/o/r/deployments", func(w http.ResponseWriter, r *http.Request) {
45+
v := new(DeploymentRequest)
46+
json.NewDecoder(r.Body).Decode(v)
47+
48+
testMethod(t, r, "POST")
49+
if !reflect.DeepEqual(v, input) {
50+
t.Errorf("Request body = %+v, want %+v", v, input)
51+
}
52+
53+
fmt.Fprint(w, `{"ref": "1111", "task": "deploy"}`)
54+
})
55+
56+
deployment, _, err := client.Repositories.CreateDeployment("o", "r", input)
57+
if err != nil {
58+
t.Errorf("Repositories.CreateDeployment returned error: %v", err)
59+
}
60+
61+
want := &Deployment{Ref: String("1111"), Task: String("deploy")}
62+
if !reflect.DeepEqual(deployment, want) {
63+
t.Errorf("Repositories.CreateDeployment returned %+v, want %+v", deployment, want)
64+
}
65+
}
66+
67+
func TestRepositoriesService_ListDeploymentStatuses(t *testing.T) {
68+
setup()
69+
defer teardown()
70+
71+
mux.HandleFunc("/repos/o/r/deployments/1/statuses", func(w http.ResponseWriter, r *http.Request) {
72+
testMethod(t, r, "GET")
73+
testFormValues(t, r, values{"page": "2"})
74+
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
75+
})
76+
77+
opt := &ListOptions{Page: 2}
78+
statutses, _, err := client.Repositories.ListDeploymentStatuses("o", "r", 1, opt)
79+
if err != nil {
80+
t.Errorf("Repositories.ListDeploymentStatuses returned error: %v", err)
81+
}
82+
83+
want := []DeploymentStatus{{ID: Int(1)}, {ID: Int(2)}}
84+
if !reflect.DeepEqual(statutses, want) {
85+
t.Errorf("Repositories.ListDeploymentStatuses returned %+v, want %+v", statutses, want)
86+
}
87+
}

0 commit comments

Comments
 (0)