-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathgithub.go
161 lines (138 loc) · 4.55 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"github.com/coreos/pkg/flagutil"
"github.com/dghubble/sling"
"golang.org/x/oauth2"
)
const baseURL = "https://api.github.com/"
// Issue is a simplified Github issue
// https://developer.github.com/v3/issues/#response
type Issue struct {
ID int `json:"id"`
URL string `json:"url"`
Number int `json:"number"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
}
// GithubError represents a Github API error response
// https://developer.github.com/v3/#client-errors
type GithubError struct {
Message string `json:"message"`
Errors []struct {
Resource string `json:"resource"`
Field string `json:"field"`
Code string `json:"code"`
} `json:"errors"`
DocumentationURL string `json:"documentation_url"`
}
func (e GithubError) Error() string {
return fmt.Sprintf("github: %v %+v %v", e.Message, e.Errors, e.DocumentationURL)
}
// IssueRequest is a simplified issue request
// https://developer.github.com/v3/issues/#create-an-issue
type IssueRequest struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Assignee string `json:"assignee,omitempty"`
Milestone int `json:"milestone,omitempty"`
Labels []string `json:"labels,omitempty"`
}
// IssueListParams are the params for IssueService.List
// https://developer.github.com/v3/issues/#parameters
type IssueListParams struct {
Filter string `url:"filter,omitempty"`
State string `url:"state,omitempty"`
Labels string `url:"labels,omitempty"`
Sort string `url:"sort,omitempty"`
Direction string `url:"direction,omitempty"`
Since string `url:"since,omitempty"`
}
// Services
// IssueService provides methods for creating and reading issues.
type IssueService struct {
sling *sling.Sling
}
// NewIssueService returns a new IssueService.
func NewIssueService(httpClient *http.Client) *IssueService {
return &IssueService{
sling: sling.New().Client(httpClient).Base(baseURL),
}
}
// List returns the authenticated user's issues across repos and orgs.
func (s *IssueService) List(params *IssueListParams) ([]Issue, *http.Response, error) {
issues := new([]Issue)
githubError := new(GithubError)
resp, err := s.sling.New().Path("issues").QueryStruct(params).Receive(issues, githubError)
if err == nil {
err = githubError
}
return *issues, resp, err
}
// ListByRepo returns a repository's issues.
func (s *IssueService) ListByRepo(owner, repo string, params *IssueListParams) ([]Issue, *http.Response, error) {
issues := new([]Issue)
githubError := new(GithubError)
path := fmt.Sprintf("repos/%s/%s/issues", owner, repo)
resp, err := s.sling.New().Get(path).QueryStruct(params).Receive(issues, githubError)
if err == nil {
err = githubError
}
return *issues, resp, err
}
// Create creates a new issue on the specified repository.
func (s *IssueService) Create(owner, repo string, issueBody *IssueRequest) (*Issue, *http.Response, error) {
issue := new(Issue)
githubError := new(GithubError)
path := fmt.Sprintf("repos/%s/%s/issues", owner, repo)
resp, err := s.sling.New().Post(path).BodyJSON(issueBody).Receive(issue, githubError)
if err == nil {
err = githubError
}
return issue, resp, err
}
// Client to wrap services
// Client is a tiny Github client
type Client struct {
IssueService *IssueService
// other service endpoints...
}
// NewClient returns a new Client
func NewClient(httpClient *http.Client) *Client {
return &Client{
IssueService: NewIssueService(httpClient),
}
}
func main() {
// Github Unauthenticated API
client := NewClient(nil)
params := &IssueListParams{Sort: "updated"}
issues, _, _ := client.IssueService.ListByRepo("golang", "go", params)
fmt.Printf("Public golang/go Issues:\n%v\n", issues)
// Github OAuth2 API
flags := flag.NewFlagSet("github-example", flag.ExitOnError)
// -access-token=xxx or GITHUB_ACCESS_TOKEN env var
accessToken := flags.String("access-token", "", "Github Access Token")
flags.Parse(os.Args[1:])
flagutil.SetFlagsFromEnv(flags, "GITHUB")
if *accessToken == "" {
log.Fatal("Github Access Token required to list private issues")
}
config := &oauth2.Config{}
token := &oauth2.Token{AccessToken: *accessToken}
httpClient := config.Client(oauth2.NoContext, token)
client = NewClient(httpClient)
issues, _, _ = client.IssueService.List(params)
fmt.Printf("Your Github Issues:\n%v\n", issues)
// body := &IssueRequest{
// Title: "Test title",
// Body: "Some test issue",
// }
// issue, _, _ := client.IssueService.Create("dghubble", "temp", body)
// fmt.Println(issue)
}