forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_parser.go
229 lines (206 loc) · 6.08 KB
/
event_parser.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package events
import (
"errors"
"fmt"
"strings"
"github.com/google/go-github/github"
"github.com/hootsuite/atlantis/server/events/models"
)
//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_event_parsing.go EventParsing
type Command struct {
Name CommandName
Environment string
Verbose bool
Flags []string
}
type EventParsing interface {
DetermineCommand(comment *github.IssueCommentEvent) (*Command, error)
ExtractCommentData(comment *github.IssueCommentEvent) (baseRepo models.Repo, user models.User, pull models.PullRequest, err error)
ExtractPullData(pull *github.PullRequest) (models.PullRequest, models.Repo, error)
ExtractRepoData(ghRepo *github.Repository) (models.Repo, error)
}
type EventParser struct {
GithubUser string
GithubToken string
}
// DetermineCommand parses the comment as an atlantis command. If it succeeds,
// it returns the command. Otherwise it returns error.
func (e *EventParser) DetermineCommand(comment *github.IssueCommentEvent) (*Command, error) {
// valid commands contain:
// the initial "executable" name, 'run' or 'atlantis' or '@GithubUser' where GithubUser is the api user atlantis is running as
// then a command, either 'plan', 'apply', or 'help'
// then an optional environment argument, an optional --verbose flag and any other flags
//
// examples:
// atlantis help
// run plan
// @GithubUser plan staging
// atlantis plan staging --verbose
// atlantis plan staging --verbose -key=value -key2 value2
commentBody := comment.Comment.GetBody()
if commentBody == "" {
return nil, errors.New("comment.body is null")
}
err := errors.New("not an Atlantis command")
args := strings.Fields(commentBody)
if len(args) < 2 {
return nil, err
}
env := "default"
verbose := false
var flags []string
if !e.stringInSlice(args[0], []string{"run", "atlantis", "@" + e.GithubUser}) {
return nil, err
}
if !e.stringInSlice(args[1], []string{"plan", "apply", "help"}) {
return nil, err
}
if args[1] == "help" {
return &Command{Name: Help}, nil
}
command := args[1]
if len(args) > 2 {
flags = args[2:]
// if the third arg doesn't start with '-' then we assume it's an
// environment not a flag
if !strings.HasPrefix(args[2], "-") {
env = args[2]
flags = args[3:]
}
// check for --verbose specially and then remove any additional
// occurrences
if e.stringInSlice("--verbose", flags) {
verbose = true
flags = e.removeOccurrences("--verbose", flags)
}
}
c := &Command{Verbose: verbose, Environment: env, Flags: flags}
switch command {
case "plan":
c.Name = Plan
case "apply":
c.Name = Apply
default:
return nil, fmt.Errorf("something went wrong parsing the command, the command we parsed %q was not apply or plan", command)
}
return c, nil
}
func (e *EventParser) ExtractCommentData(comment *github.IssueCommentEvent) (baseRepo models.Repo, user models.User, pull models.PullRequest, err error) {
baseRepo, err = e.ExtractRepoData(comment.Repo)
if err != nil {
return
}
pullNum := comment.Issue.GetNumber()
if pullNum == 0 {
err = errors.New("issue.number is null")
return
}
pullCreator := comment.Issue.User.GetLogin()
if pullCreator == "" {
err = errors.New("issue.user.login is null")
return
}
htmlURL := comment.Issue.GetHTMLURL()
if htmlURL == "" {
err = errors.New("issue.html_url is null")
return
}
commentorUsername := comment.Comment.User.GetLogin()
if commentorUsername == "" {
err = errors.New("comment.user.login is null")
return
}
user = models.User{
Username: commentorUsername,
}
pull = models.PullRequest{
Num: pullNum,
}
return
}
func (e *EventParser) ExtractPullData(pull *github.PullRequest) (models.PullRequest, models.Repo, error) {
var pullModel models.PullRequest
var headRepoModel models.Repo
commit := pull.Head.GetSHA()
if commit == "" {
return pullModel, headRepoModel, errors.New("head.sha is null")
}
base := pull.Base.GetSHA()
if base == "" {
return pullModel, headRepoModel, errors.New("base.sha is null")
}
url := pull.GetHTMLURL()
if url == "" {
return pullModel, headRepoModel, errors.New("html_url is null")
}
branch := pull.Head.GetRef()
if branch == "" {
return pullModel, headRepoModel, errors.New("head.ref is null")
}
authorUsername := pull.User.GetLogin()
if authorUsername == "" {
return pullModel, headRepoModel, errors.New("user.login is null")
}
num := pull.GetNumber()
if num == 0 {
return pullModel, headRepoModel, errors.New("number is null")
}
headRepoModel, err := e.ExtractRepoData(pull.Head.Repo)
if err != nil {
return pullModel, headRepoModel, err
}
return models.PullRequest{
BaseCommit: base,
Author: authorUsername,
Branch: branch,
HeadCommit: commit,
URL: url,
Num: num,
}, headRepoModel, nil
}
func (e *EventParser) ExtractRepoData(ghRepo *github.Repository) (models.Repo, error) {
var repo models.Repo
repoFullName := ghRepo.GetFullName()
if repoFullName == "" {
return repo, errors.New("repository.full_name is null")
}
repoOwner := ghRepo.Owner.GetLogin()
if repoOwner == "" {
return repo, errors.New("repository.owner.login is null")
}
repoName := ghRepo.GetName()
if repoName == "" {
return repo, errors.New("repository.name is null")
}
repoSanitizedCloneURL := ghRepo.GetCloneURL()
if repoSanitizedCloneURL == "" {
return repo, errors.New("repository.clone_url is null")
}
// construct HTTPS repo clone url string with username and password
repoCloneURL := strings.Replace(repoSanitizedCloneURL, "https://", fmt.Sprintf("https://%s:%s@", e.GithubUser, e.GithubToken), -1)
return models.Repo{
Owner: repoOwner,
FullName: repoFullName,
CloneURL: repoCloneURL,
SanitizedCloneURL: repoSanitizedCloneURL,
Name: repoName,
}, nil
}
func (e *EventParser) stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
// nolint: unparam
func (e *EventParser) removeOccurrences(a string, list []string) []string {
var out []string
for _, b := range list {
if b != a {
out = append(out, b)
}
}
return out
}