-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub.go
More file actions
124 lines (97 loc) · 2.47 KB
/
github.go
File metadata and controls
124 lines (97 loc) · 2.47 KB
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/pkg/errors"
)
type github struct {
Token string
}
func (gh *github) auth(req *http.Request) {
if gh.Token != "" {
req.Header.Set("Authorization", "token "+gh.Token)
}
}
func (gh *github) DispatchRepositoryEvent(ctx context.Context, org, repo string, eventType string) error {
endpoint := fmt.Sprintf("https://api.github.com/repos/%s/%s/dispatches",
org,
repo,
)
payload, err := json.Marshal(struct {
EventType string `json:"event_type"`
}{
EventType: eventType,
})
if err != nil {
return errors.Wrap(err, "marshal payload")
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(payload))
if err != nil {
return errors.Wrap(err, "build request")
}
gh.auth(req)
req.Header.Set("Accept", "application/vnd.github.everest-preview+json")
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "do request")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return errors.Wrap(err, "read body")
}
log.Printf("%s", string(body))
if res.StatusCode != http.StatusNoContent {
var apiErr struct {
Message string `json:"message"`
}
if err := json.Unmarshal(body, &apiErr); err != nil {
return errors.Wrap(err, "unmarshal error")
}
return errors.New(apiErr.Message)
}
return nil
}
func (gh *github) GetLastCommitSHA(ctx context.Context, org, repo, branch string) (string, error) {
endpoint := fmt.Sprintf("https://api.github.com/repos/%s/%s/commits/%s",
org,
repo,
branch,
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return "", errors.Wrap(err, "build request")
}
gh.auth(req)
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", errors.Wrap(err, "do request")
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "read body")
}
if res.StatusCode != http.StatusOK {
var apiErr struct {
Message string `json:"message"`
}
if err := json.Unmarshal(body, &apiErr); err != nil {
return "", errors.Wrap(err, "unmarshal error")
}
return "", errors.New(apiErr.Message)
}
var response struct {
SHA string `json:"sha"`
}
if err := json.Unmarshal(body, &response); err != nil {
return "", errors.Wrap(err, "umarshal body")
}
return response.SHA, nil
}