/
pullrequest_handler.go
124 lines (116 loc) · 3.97 KB
/
pullrequest_handler.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
package github
import (
"encoding/json"
"errors"
"net/http"
"path/filepath"
"strings"
"github.com/bivas/rivi/pkg/config/client"
"github.com/bivas/rivi/pkg/types"
"github.com/bivas/rivi/pkg/util"
"github.com/bivas/rivi/pkg/util/log"
)
type pullRequestEventHandler struct {
logger log.Logger
}
func (h *pullRequestEventHandler) readFromJson(context *builderContext, payload *PullRequestPayload) {
pr := payload.PullRequest
if pr.Number > 0 {
context.data.number = pr.Number
} else {
context.data.number = payload.Number
}
context.data.title = pr.Title
context.data.description = pr.Body
context.data.changedFiles = pr.ChangedFiles
context.data.additions = pr.Additions
context.data.deletions = pr.Deletions
context.data.ref = pr.Base.Ref
head := pr.Head
context.data.origin = types.Origin{
User: strings.ToLower(head.User.Login),
Repo: head.Repo.Name,
Ref: head.Ref,
Head: head.Sha[0:6],
SHA: head.Sha,
GitURL: head.Repo.GitURL,
}
context.data.state = pr.State
}
func (h *pullRequestEventHandler) readFromClient(context *builderContext) {
id := context.data.number
context.data.assignees = context.client.GetAssignees(id)
context.data.state = context.client.GetState(id)
context.data.labels = context.client.GetLabels(id)
context.data.comments = context.client.GetComments(id)
fileNames := context.client.GetFileNames(id)
context.data.fileNames = fileNames
stringSet := util.StringSet{Transformer: filepath.Ext}
context.data.fileExt = stringSet.AddAll(fileNames).Values()
context.data.reviewers = context.client.GetReviewers(id)
context.data.locked = context.client.Locked(id)
}
func (h *pullRequestEventHandler) checkProcessState(context *builderContext) bool {
h.logger.DebugWith(
log.MetaFields{
log.F("issue", context.data.GetShortName())},
"Current state is '%s'", context.data.state)
return context.data.state != "closed"
}
func (h *pullRequestEventHandler) FromRequest(config client.ClientConfig, r *http.Request) (types.HookData, bool, error) {
context := &builderContext{secret: []byte(config.GetSecret())}
pl := PullRequestPayload{}
raw, err := ReadPayload(context.secret, r, &pl)
if err != nil {
return nil, false, err
}
if pl.Number == 0 && pl.PullRequest.Number == 0 {
h.logger.Warning("Payload appear to have issue id 0")
h.logger.Debug("Faulty PullRequestPayload %+v", pl)
return nil, false, errors.New("PullRequestPayload appear to have issue id 0")
}
repo := pl.Repository.Name
owner := pl.Repository.Owner.Login
installation := pl.Installation.ID
if installation > 0 {
context.client = NewAppClient(config, owner, repo, installation)
} else {
context.client = NewClient(config, owner, repo)
}
if context.client == nil {
return nil, false, errors.New("unable to initialize github client")
}
context.data = &data{
owner: owner, repo: repo,
payload: raw, eventType: r.Header.Get("X-Github-Event"),
client: context.client}
h.readFromJson(context, &pl)
return context.data, h.checkProcessState(context), nil
}
func (h *pullRequestEventHandler) FromPayload(config client.ClientConfig, raw []byte) (types.Data, bool, error) {
var pl PullRequestPayload
if e := json.Unmarshal(raw, &pl); e != nil {
return nil, false, e
}
repo := pl.Repository.Name
owner := pl.Repository.Owner.Login
installation := pl.Installation.ID
context := &builderContext{}
if installation > 0 {
context.client = NewAppClient(config, owner, repo, installation)
} else {
context.client = NewClient(config, owner, repo)
}
if context.client == nil {
return nil, false, errors.New("unable to initialize github client")
}
context.data = &data{owner: owner, repo: repo, payload: raw, client: context.client}
h.readFromJson(context, &pl)
if context.data.GetNumber() == 0 {
h.logger.Warning("Payload appear to have issue id 0")
h.logger.Debug("Faulty PullRequestPayload %+v", pl)
return nil, false, errors.New("PullRequestPayload appear to have issue id 0")
}
h.readFromClient(context)
return context.data, h.checkProcessState(context), nil
}