/
bot.go
78 lines (58 loc) · 1.76 KB
/
bot.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
package reviewresult
import (
"context"
"github.com/google/go-github/github"
"github.com/mattmoor/knobots/pkg/handler"
"github.com/mattmoor/knobots/pkg/handler/commitstatus"
"github.com/mattmoor/knobots/pkg/review"
)
type reviewresult struct{}
var _ handler.Interface = (*reviewresult)(nil)
func New(context.Context) handler.Interface {
return &reviewresult{}
}
func (*reviewresult) GetType() interface{} {
return &Payload{}
}
func (*reviewresult) Handle(ctx context.Context, x interface{}) (handler.Response, error) {
p := x.(*Payload)
if err := review.CleanupOlder(ctx, p.Name, p.Owner, p.Repository, p.PullRequest); err != nil {
return nil, err
}
pr := &commitstatus.Payload{
Owner: p.Owner,
Repository: p.Repository,
SHA: p.SHA,
Name: p.Name,
Description: p.Description,
}
// Determine the check state and write our review.
if len(p.Comments) != 0 || p.Body != "" {
pr.State = "failure"
if err := review.Create(ctx, p.Name, p.Owner, p.Repository, p.PullRequest, p.Body, p.Comments); err != nil {
return nil, err
}
} else {
pr.State = "success"
}
return nil, nil
// TODO(mattmoor): Don't do this for now.
return pr, nil
}
type Payload struct {
Owner string `json:"owner"`
Repository string `json:"repository"`
PullRequest int `json:"pull_request"`
SHA string `json:"sha"`
Name string `json:"name"`
Description string `json:"description"`
Body string `json:"body,omitempty"`
Comments []*github.DraftReviewComment `json:"comments"`
}
var _ handler.Response = (*Payload)(nil)
func (*Payload) GetSource() string {
return "https://github.com/mattmoor/knobots/cmd/reviewresult"
}
func (*Payload) GetType() string {
return "dev.mattmoor.knobots.reviewresult"
}