-
Notifications
You must be signed in to change notification settings - Fork 12
/
parsers.go
122 lines (115 loc) · 2.94 KB
/
parsers.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
package main
import "encoding/json"
type messageRepository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
SSHURL string `json:"ssh_url"`
}
func parseIssueComment(body []byte) (IssueComment, error) {
var message struct {
Issue struct {
Number int `json:"Number"`
PullRequest struct {
URL string `json:"url"`
} `json:"pull_request"`
User struct {
Login string `json:"login"`
} `json:"user"`
} `json:"issue"`
Repository messageRepository `json:"repository"`
Comment struct {
Body string `json:"body"`
} `json:"comment"`
}
err := json.Unmarshal(body, &message)
if err != nil {
return IssueComment{}, err
}
return IssueComment{
IssueNumber: message.Issue.Number,
Comment: message.Comment.Body,
IsPullRequest: message.Issue.PullRequest.URL != "",
Repository: Repository{
Owner: message.Repository.Owner.Login,
Name: message.Repository.Name,
URL: message.Repository.SSHURL,
},
User: User{
Login: message.Issue.User.Login,
},
}, nil
}
func parsePullRequestEvent(body []byte) (PullRequestEvent, error) {
var message struct {
Action string `json:"action"`
Number int `json:"number"`
PullRequest struct {
Head struct {
SHA string `json:"sha"`
Repository messageRepository `json:"repo"`
} `json:"head"`
User struct {
Login string `json:"login"`
} `json:"user"`
} `json:"pull_request"`
Repository messageRepository `json:"repository"`
}
err := json.Unmarshal(body, &message)
if err != nil {
return PullRequestEvent{}, err
}
return PullRequestEvent{
IssueNumber: message.Number,
Action: message.Action,
Head: PullRequestBranch{
SHA: message.PullRequest.Head.SHA,
Repository: Repository{
Owner: message.PullRequest.Head.Repository.Owner.Login,
Name: message.PullRequest.Head.Repository.Name,
URL: message.PullRequest.Head.Repository.SSHURL,
},
},
Repository: Repository{
Owner: message.Repository.Owner.Login,
Name: message.Repository.Name,
URL: message.Repository.SSHURL,
},
User: User{
Login: message.PullRequest.User.Login,
},
}, nil
}
func parseStatusEvent(body []byte) (StatusEvent, error) {
var message struct {
SHA string `json:"sha"`
State string `json:"state"`
Branches []struct {
Commit struct {
SHA string `json:"sha"`
} `json:"commit"`
} `json:"branches"`
Repository messageRepository `json:"repository"`
}
err := json.Unmarshal(body, &message)
if err != nil {
return StatusEvent{}, err
}
branches := make([]Branch, len(message.Branches))
for i, branch := range message.Branches {
branches[i] = Branch{
SHA: branch.Commit.SHA,
}
}
return StatusEvent{
SHA: message.SHA,
State: message.State,
Branches: branches,
Repository: Repository{
Owner: message.Repository.Owner.Login,
Name: message.Repository.Name,
URL: message.Repository.SSHURL,
},
}, nil
}