/
github_status.go
174 lines (151 loc) · 3.49 KB
/
github_status.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
162
163
164
165
166
167
168
169
170
171
172
173
174
package github
import (
"context"
"errors"
"github.com/google/go-github/github"
"github.com/tychoish/grip"
"github.com/tychoish/grip/message"
"github.com/tychoish/grip/send"
)
type githubStatusMessageLogger struct {
opts *GithubOptions
ref string
gh githubClient
send.Base
}
func (s *githubStatusMessageLogger) Send(m message.Composer) {
if send.ShouldLog(s, m) {
var status *github.RepoStatus
owner := ""
repo := ""
ref := ""
switch v := m.Raw().(type) {
case *Status:
status = githubStatusMessagePayloadToRepoStatus(v)
if v != nil {
owner = v.Owner
repo = v.Repo
ref = v.Ref
}
case Status:
status = githubStatusMessagePayloadToRepoStatus(&v)
owner = v.Owner
repo = v.Repo
ref = v.Ref
case *message.Fields:
status = s.githubMessageFieldsToStatus(v)
owner, repo, ref = githubMessageFieldsToRepo(v)
case message.Fields:
status = s.githubMessageFieldsToStatus(&v)
owner, repo, ref = githubMessageFieldsToRepo(&v)
}
if len(owner) == 0 {
owner = s.opts.Account
}
if len(repo) == 0 {
owner = s.opts.Repo
}
if len(ref) == 0 {
owner = s.ref
}
if status == nil {
s.HandleError(errors.New("composer cannot be converted to github status"))
return
}
_, _, err := s.gh.CreateStatus(context.TODO(), owner, repo, ref, status)
if err != nil {
s.HandleError(send.WrapError(err, m))
}
}
}
// NewStatusSender returns a Sender to send payloads to the Github Status
// API. Statuses will be attached to the given ref.
func NewStatusSender(name string, opts *GithubOptions, ref string) send.Sender {
s := &githubStatusMessageLogger{
gh: &githubClientImpl{},
ref: ref,
}
ctx := context.TODO()
s.gh.Init(ctx, opts.Token)
s.SetName(name)
s.SetFormatter(send.MakePlainFormatter())
s.SetErrorHandler(send.ErrorHandlerFromSender(grip.Sender()))
return s
}
func (s *githubStatusMessageLogger) githubMessageFieldsToStatus(m *message.Fields) *github.RepoStatus {
if m == nil {
return nil
}
state, ok := getStringPtrFromField((*m)["state"])
if !ok {
return nil
}
context, ok := getStringPtrFromField((*m)["context"])
if !ok {
return nil
}
URL, ok := getStringPtrFromField((*m)["URL"])
if !ok {
return nil
}
var description *string
if description != nil {
description, ok = getStringPtrFromField((*m)["description"])
if description != nil && len(*description) == 0 {
description = nil
}
if !ok {
return nil
}
}
status := &github.RepoStatus{
State: state,
Context: context,
TargetURL: URL,
Description: description,
}
return status
}
func getStringPtrFromField(i any) (*string, bool) {
if ret, ok := i.(string); ok {
return &ret, true
}
if ret, ok := i.(*string); ok {
return ret, ok
}
return nil, false
}
func githubStatusMessagePayloadToRepoStatus(c *Status) *github.RepoStatus {
if c == nil {
return nil
}
s := &github.RepoStatus{
Context: github.String(c.Context),
State: github.String(string(c.State)),
}
if len(c.URL) > 0 {
s.TargetURL = github.String(c.URL)
}
if len(c.Description) > 0 {
s.Description = github.String(c.Description)
}
return s
}
func githubMessageFieldsToRepo(m *message.Fields) (string, string, string) {
if m == nil {
return "", "", ""
}
owner, ok := getStringPtrFromField((*m)["owner"])
if !ok {
owner = github.String("")
}
repo, ok := getStringPtrFromField((*m)["repo"])
if !ok {
repo = github.String("")
}
ref, ok := getStringPtrFromField((*m)["ref"])
if !ok {
ref = github.String("")
}
return *owner, *repo, *ref
}