forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_parser.go
662 lines (601 loc) · 23.6 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// Copyright 2017 HootSuite Media Inc.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Modified hereafter by contributors to runatlantis/atlantis.
package events
import (
"encoding/json"
"fmt"
"path"
"strings"
"github.com/google/go-github/github"
"github.com/lkysow/go-gitlab"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs/bitbucketcloud"
"github.com/runatlantis/atlantis/server/events/vcs/bitbucketserver"
"gopkg.in/go-playground/validator.v9"
)
const gitlabPullOpened = "opened"
const usagesCols = 90
// PullCommand is a command to run on a pull request.
type PullCommand interface {
// CommandName is the name of the command we're running.
CommandName() CommandName
// IsVerbose is true if the output of this command should be verbose.
IsVerbose() bool
// IsAutoplan is true if this is an autoplan command vs. a comment command.
IsAutoplan() bool
}
// AutoplanCommand is a plan command that is automatically triggered when a
// pull request is opened or updated.
type AutoplanCommand struct{}
// CommandName is Plan.
func (c AutoplanCommand) CommandName() CommandName {
return PlanCommand
}
// IsVerbose is false for autoplan commands.
func (c AutoplanCommand) IsVerbose() bool {
return false
}
// IsAutoplan is true for autoplan commands (obviously).
func (c AutoplanCommand) IsAutoplan() bool {
return true
}
// CommentCommand is a command that was triggered by a pull request comment.
type CommentCommand struct {
// RepoRelDir is the path relative to the repo root to run the command in.
// Will never end in "/". If empty then the comment specified no directory.
RepoRelDir string
// Flags are the extra arguments appended to the comment,
// ex. atlantis plan -- -target=resource
Flags []string
// Name is the name of the command the comment specified.
Name CommandName
// Verbose is true if the command should output verbosely.
Verbose bool
// Workspace is the name of the Terraform workspace to run the command in.
// If empty then the comment specified no workspace.
Workspace string
// ProjectName is the name of a project to run the command on. It refers to a
// project specified in an atlantis.yaml file.
// If empty then the comment specified no project.
ProjectName string
}
// IsForSpecificProject returns true if the command is for a specific dir, workspace
// or project name. Otherwise it's a command like "atlantis plan" or "atlantis
// apply".
func (c CommentCommand) IsForSpecificProject() bool {
return c.RepoRelDir != "" || c.Workspace != "" || c.ProjectName != ""
}
// CommandName returns the name of this command.
func (c CommentCommand) CommandName() CommandName {
return c.Name
}
// IsVerbose is true if the command should give verbose output.
func (c CommentCommand) IsVerbose() bool {
return c.Verbose
}
// IsAutoplan will be false for comment commands.
func (c CommentCommand) IsAutoplan() bool {
return false
}
// String returns a string representation of the command.
func (c CommentCommand) String() string {
return fmt.Sprintf("command=%q verbose=%t dir=%q workspace=%q project=%q flags=%q", c.Name.String(), c.Verbose, c.RepoRelDir, c.Workspace, c.ProjectName, strings.Join(c.Flags, ","))
}
// NewCommentCommand constructs a CommentCommand, setting all missing fields to defaults.
func NewCommentCommand(repoRelDir string, flags []string, name CommandName, verbose bool, workspace string, project string) *CommentCommand {
// If repoRelDir was empty we want to keep it that way to indicate that it
// wasn't specified in the comment.
if repoRelDir != "" {
repoRelDir = path.Clean(repoRelDir)
if repoRelDir == "/" {
repoRelDir = "."
}
}
return &CommentCommand{
RepoRelDir: repoRelDir,
Flags: flags,
Name: name,
Verbose: verbose,
Workspace: workspace,
ProjectName: project,
}
}
//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_event_parsing.go EventParsing
// EventParsing parses webhook events from different VCS hosts into their
// respective Atlantis models.
// todo: rename to VCSParsing or the like because this also parses API responses #refactor
type EventParsing interface {
// ParseGithubIssueCommentEvent parses GitHub pull request comment events.
// baseRepo is the repo that the pull request will be merged into.
// user is the pull request author.
// pullNum is the number of the pull request that triggered the webhook.
ParseGithubIssueCommentEvent(comment *github.IssueCommentEvent) (
baseRepo models.Repo, user models.User, pullNum int, err error)
// ParseGithubPull parses the response from the GitHub API endpoint (not
// from a webhook) that returns a pull request.
// pull is the parsed pull request.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
ParseGithubPull(ghPull *github.PullRequest) (
pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, err error)
// ParseGithubPullEvent parses GitHub pull request events.
// pull is the parsed pull request.
// pullEventType is the type of event, for example opened/closed.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
// user is the pull request author.
ParseGithubPullEvent(pullEvent *github.PullRequestEvent) (
pull models.PullRequest, pullEventType models.PullRequestEventType,
baseRepo models.Repo, headRepo models.Repo, user models.User, err error)
// ParseGithubRepo parses the response from the GitHub API endpoint that
// returns a repo into the Atlantis model.
ParseGithubRepo(ghRepo *github.Repository) (models.Repo, error)
// ParseGitlabMergeRequestEvent parses GitLab merge request events.
// pull is the parsed merge request.
// pullEventType is the type of event, for example opened/closed.
// baseRepo is the repo the merge request will be merged into.
// headRepo is the repo the merge request branch is from.
// user is the pull request author.
ParseGitlabMergeRequestEvent(event gitlab.MergeEvent) (
pull models.PullRequest, pullEventType models.PullRequestEventType,
baseRepo models.Repo, headRepo models.Repo, user models.User, err error)
// ParseGitlabMergeRequestCommentEvent parses GitLab merge request comment
// events.
// baseRepo is the repo the merge request will be merged into.
// headRepo is the repo the merge request branch is from.
// user is the pull request author.
ParseGitlabMergeRequestCommentEvent(event gitlab.MergeCommentEvent) (
baseRepo models.Repo, headRepo models.Repo, user models.User, err error)
// ParseGitlabMergeRequest parses the response from the GitLab API endpoint
// that returns a merge request.
ParseGitlabMergeRequest(mr *gitlab.MergeRequest, baseRepo models.Repo) models.PullRequest
// ParseBitbucketCloudPullEvent parses a pull request event from Bitbucket
// Cloud (bitbucket.org).
// pull is the parsed pull request.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
// user is the pull request author.
ParseBitbucketCloudPullEvent(body []byte) (
pull models.PullRequest, baseRepo models.Repo,
headRepo models.Repo, user models.User, err error)
// ParseBitbucketCloudPullCommentEvent parses a pull request comment event
// from Bitbucket Cloud (bitbucket.org).
// pull is the parsed pull request.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
// user is the pull request author.
// comment is the comment that triggered the event.
ParseBitbucketCloudPullCommentEvent(body []byte) (
pull models.PullRequest, baseRepo models.Repo,
headRepo models.Repo, user models.User, comment string, err error)
// GetBitbucketCloudPullEventType returns the type of the pull request
// event given the Bitbucket Cloud header.
GetBitbucketCloudPullEventType(eventTypeHeader string) models.PullRequestEventType
// ParseBitbucketServerPullEvent parses a pull request event from Bitbucket
// Server.
// pull is the parsed pull request.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
// user is the pull request author.
ParseBitbucketServerPullEvent(body []byte) (
pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo,
user models.User, err error)
// ParseBitbucketServerPullCommentEvent parses a pull request comment event
// from Bitbucket Server.
// pull is the parsed pull request.
// baseRepo is the repo the pull request will be merged into.
// headRepo is the repo the pull request branch is from.
// user is the pull request author.
// comment is the comment that triggered the event.
ParseBitbucketServerPullCommentEvent(body []byte) (
pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo,
user models.User, comment string, err error)
// GetBitbucketServerPullEventType returns the type of the pull request
// event given the Bitbucket Server header.
GetBitbucketServerPullEventType(eventTypeHeader string) models.PullRequestEventType
}
// EventParser parses VCS events.
type EventParser struct {
GithubUser string
GithubToken string
GitlabUser string
GitlabToken string
BitbucketUser string
BitbucketToken string
BitbucketServerURL string
}
// GetBitbucketCloudPullEventType returns the type of the pull request
// event given the Bitbucket Cloud header.
func (e *EventParser) GetBitbucketCloudPullEventType(eventTypeHeader string) models.PullRequestEventType {
switch eventTypeHeader {
case bitbucketcloud.PullCreatedHeader:
return models.OpenedPullEvent
case bitbucketcloud.PullUpdatedHeader:
return models.UpdatedPullEvent
case bitbucketcloud.PullFulfilledHeader, bitbucketcloud.PullRejectedHeader:
return models.ClosedPullEvent
}
return models.OtherPullEvent
}
// ParseBitbucketCloudPullCommentEvent parses a pull request comment event
// from Bitbucket Cloud (bitbucket.org).
// See EventParsing for return value docs.
func (e *EventParser) ParseBitbucketCloudPullCommentEvent(body []byte) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, comment string, err error) {
var event bitbucketcloud.CommentEvent
if err = json.Unmarshal(body, &event); err != nil {
err = errors.Wrap(err, "parsing json")
return
}
if err = validator.New().Struct(event); err != nil {
err = errors.Wrapf(err, "API response %q was missing fields", string(body))
return
}
pull, baseRepo, headRepo, user, err = e.parseCommonBitbucketCloudEventData(event.CommonEventData)
comment = *event.Comment.Content.Raw
return
}
func (e *EventParser) parseCommonBitbucketCloudEventData(event bitbucketcloud.CommonEventData) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
var prState models.PullRequestState
switch *event.PullRequest.State {
case "OPEN":
prState = models.OpenPullState
case "MERGED":
prState = models.ClosedPullState
case "SUPERSEDED":
prState = models.ClosedPullState
case "DECLINE":
prState = models.ClosedPullState
default:
err = fmt.Errorf("unable to determine pull request state from %q–this is a bug", *event.PullRequest.State)
return
}
headRepo, err = models.NewRepo(
models.BitbucketCloud,
*event.PullRequest.Source.Repository.FullName,
*event.PullRequest.Source.Repository.Links.HTML.HREF,
e.BitbucketUser,
e.BitbucketToken)
if err != nil {
return
}
baseRepo, err = models.NewRepo(
models.BitbucketCloud,
*event.Repository.FullName,
*event.Repository.Links.HTML.HREF,
e.BitbucketUser,
e.BitbucketToken)
if err != nil {
return
}
pull = models.PullRequest{
Num: *event.PullRequest.ID,
HeadCommit: *event.PullRequest.Source.Commit.Hash,
URL: *event.PullRequest.Links.HTML.HREF,
Branch: *event.PullRequest.Source.Branch.Name,
Author: *event.Actor.Username,
State: prState,
BaseRepo: baseRepo,
}
user = models.User{
Username: *event.Actor.Username,
}
return
}
// ParseBitbucketCloudPullEvent parses a pull request event from Bitbucket
// Cloud (bitbucket.org).
// See EventParsing for return value docs.
func (e *EventParser) ParseBitbucketCloudPullEvent(body []byte) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
var event bitbucketcloud.PullRequestEvent
if err = json.Unmarshal(body, &event); err != nil {
err = errors.Wrap(err, "parsing json")
return
}
if err = validator.New().Struct(event); err != nil {
err = errors.Wrapf(err, "API response %q was missing fields", string(body))
return
}
pull, baseRepo, headRepo, user, err = e.parseCommonBitbucketCloudEventData(event.CommonEventData)
return
}
// ParseGithubIssueCommentEvent parses GitHub pull request comment events.
// See EventParsing for return value docs.
func (e *EventParser) ParseGithubIssueCommentEvent(comment *github.IssueCommentEvent) (baseRepo models.Repo, user models.User, pullNum int, err error) {
baseRepo, err = e.ParseGithubRepo(comment.Repo)
if err != nil {
return
}
if comment.Comment == nil || comment.Comment.User.GetLogin() == "" {
err = errors.New("comment.user.login is null")
return
}
commentorUsername := comment.Comment.User.GetLogin()
user = models.User{
Username: commentorUsername,
}
pullNum = comment.Issue.GetNumber()
if pullNum == 0 {
err = errors.New("issue.number is null")
return
}
return
}
// ParseGithubPullEvent parses GitHub pull request events.
// See EventParsing for return value docs.
func (e *EventParser) ParseGithubPullEvent(pullEvent *github.PullRequestEvent) (pull models.PullRequest, pullEventType models.PullRequestEventType, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
if pullEvent.PullRequest == nil {
err = errors.New("pull_request is null")
return
}
pull, baseRepo, headRepo, err = e.ParseGithubPull(pullEvent.PullRequest)
if err != nil {
return
}
if pullEvent.Sender == nil {
err = errors.New("sender is null")
return
}
senderUsername := pullEvent.Sender.GetLogin()
if senderUsername == "" {
err = errors.New("sender.login is null")
return
}
switch pullEvent.GetAction() {
case "opened":
pullEventType = models.OpenedPullEvent
case "synchronize":
pullEventType = models.UpdatedPullEvent
case "closed":
pullEventType = models.ClosedPullEvent
default:
pullEventType = models.OtherPullEvent
}
user = models.User{Username: senderUsername}
return
}
// ParseGithubPull parses the response from the GitHub API endpoint (not
// from a webhook) that returns a pull request.
// See EventParsing for return value docs.
func (e *EventParser) ParseGithubPull(pull *github.PullRequest) (pullModel models.PullRequest, baseRepo models.Repo, headRepo models.Repo, err error) {
commit := pull.Head.GetSHA()
if commit == "" {
err = errors.New("head.sha is null")
return
}
url := pull.GetHTMLURL()
if url == "" {
err = errors.New("html_url is null")
return
}
branch := pull.Head.GetRef()
if branch == "" {
err = errors.New("head.ref is null")
return
}
authorUsername := pull.User.GetLogin()
if authorUsername == "" {
err = errors.New("user.login is null")
return
}
num := pull.GetNumber()
if num == 0 {
err = errors.New("number is null")
return
}
baseRepo, err = e.ParseGithubRepo(pull.Base.Repo)
if err != nil {
return
}
headRepo, err = e.ParseGithubRepo(pull.Head.Repo)
if err != nil {
return
}
pullState := models.ClosedPullState
if pull.GetState() == "open" {
pullState = models.OpenPullState
}
pullModel = models.PullRequest{
Author: authorUsername,
Branch: branch,
HeadCommit: commit,
URL: url,
Num: num,
State: pullState,
BaseRepo: baseRepo,
}
return
}
// ParseGithubRepo parses the response from the GitHub API endpoint that
// returns a repo into the Atlantis model.
// See EventParsing for return value docs.
func (e *EventParser) ParseGithubRepo(ghRepo *github.Repository) (models.Repo, error) {
return models.NewRepo(models.Github, ghRepo.GetFullName(), ghRepo.GetCloneURL(), e.GithubUser, e.GithubToken)
}
// ParseGitlabMergeRequestEvent parses GitLab merge request events.
// pull is the parsed merge request.
// See EventParsing for return value docs.
func (e *EventParser) ParseGitlabMergeRequestEvent(event gitlab.MergeEvent) (pull models.PullRequest, eventType models.PullRequestEventType, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
modelState := models.ClosedPullState
if event.ObjectAttributes.State == gitlabPullOpened {
modelState = models.OpenPullState
}
// GitLab also has a "merged" state, but we map that to Closed so we don't
// need to check for it.
baseRepo, err = models.NewRepo(models.Gitlab, event.Project.PathWithNamespace, event.Project.GitHTTPURL, e.GitlabUser, e.GitlabToken)
if err != nil {
return
}
headRepo, err = models.NewRepo(models.Gitlab, event.ObjectAttributes.Source.PathWithNamespace, event.ObjectAttributes.Source.GitHTTPURL, e.GitlabUser, e.GitlabToken)
if err != nil {
return
}
pull = models.PullRequest{
URL: event.ObjectAttributes.URL,
Author: event.User.Username,
Num: event.ObjectAttributes.IID,
HeadCommit: event.ObjectAttributes.LastCommit.ID,
Branch: event.ObjectAttributes.SourceBranch,
State: modelState,
BaseRepo: baseRepo,
}
switch event.ObjectAttributes.Action {
case "open":
eventType = models.OpenedPullEvent
case "update":
eventType = models.UpdatedPullEvent
case "merge", "close":
eventType = models.ClosedPullEvent
default:
eventType = models.OtherPullEvent
}
user = models.User{
Username: event.User.Username,
}
return
}
// ParseGitlabMergeRequestCommentEvent parses GitLab merge request comment
// events.
// See EventParsing for return value docs.
func (e *EventParser) ParseGitlabMergeRequestCommentEvent(event gitlab.MergeCommentEvent) (baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
// Parse the base repo first.
repoFullName := event.Project.PathWithNamespace
cloneURL := event.Project.GitHTTPURL
baseRepo, err = models.NewRepo(models.Gitlab, repoFullName, cloneURL, e.GitlabUser, e.GitlabToken)
if err != nil {
return
}
user = models.User{
Username: event.User.Username,
}
// Now parse the head repo.
headRepoFullName := event.MergeRequest.Source.PathWithNamespace
headCloneURL := event.MergeRequest.Source.GitHTTPURL
headRepo, err = models.NewRepo(models.Gitlab, headRepoFullName, headCloneURL, e.GitlabUser, e.GitlabToken)
return
}
// ParseGitlabMergeRequest parses the merge requests and returns a pull request
// model. We require passing in baseRepo because we can't get this information
// from the merge request. The only caller of this function already has that
// data so we can construct the pull request object correctly.
func (e *EventParser) ParseGitlabMergeRequest(mr *gitlab.MergeRequest, baseRepo models.Repo) models.PullRequest {
pullState := models.ClosedPullState
if mr.State == gitlabPullOpened {
pullState = models.OpenPullState
}
// GitLab also has a "merged" state, but we map that to Closed so we don't
// need to check for it.
return models.PullRequest{
URL: mr.WebURL,
Author: mr.Author.Username,
Num: mr.IID,
HeadCommit: mr.SHA,
Branch: mr.SourceBranch,
State: pullState,
BaseRepo: baseRepo,
}
}
// GetBitbucketServerPullEventType returns the type of the pull request
// event given the Bitbucket Server header.
func (e *EventParser) GetBitbucketServerPullEventType(eventTypeHeader string) models.PullRequestEventType {
switch eventTypeHeader {
case bitbucketserver.PullCreatedHeader:
return models.OpenedPullEvent
case bitbucketserver.PullMergedHeader, bitbucketserver.PullDeclinedHeader:
return models.ClosedPullEvent
}
return models.OtherPullEvent
}
// ParseBitbucketServerPullCommentEvent parses a pull request comment event
// from Bitbucket Server.
// See EventParsing for return value docs.
func (e *EventParser) ParseBitbucketServerPullCommentEvent(body []byte) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, comment string, err error) {
var event bitbucketserver.CommentEvent
if err = json.Unmarshal(body, &event); err != nil {
err = errors.Wrap(err, "parsing json")
return
}
if err = validator.New().Struct(event); err != nil {
err = errors.Wrapf(err, "API response %q was missing fields", string(body))
return
}
pull, baseRepo, headRepo, user, err = e.parseCommonBitbucketServerEventData(event.CommonEventData)
comment = *event.Comment.Text
return
}
func (e *EventParser) parseCommonBitbucketServerEventData(event bitbucketserver.CommonEventData) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
var prState models.PullRequestState
switch *event.PullRequest.State {
case "OPEN":
prState = models.OpenPullState
case "MERGED":
prState = models.ClosedPullState
case "DECLINED":
prState = models.ClosedPullState
default:
err = fmt.Errorf("unable to determine pull request state from %q–this is a bug", *event.PullRequest.State)
return
}
headRepoSlug := *event.PullRequest.FromRef.Repository.Slug
headRepoFullname := fmt.Sprintf("%s/%s", *event.PullRequest.FromRef.Repository.Project.Name, headRepoSlug)
headRepoCloneURL := fmt.Sprintf("%s/scm/%s/%s.git", e.BitbucketServerURL, strings.ToLower(*event.PullRequest.FromRef.Repository.Project.Key), headRepoSlug)
headRepo, err = models.NewRepo(
models.BitbucketServer,
headRepoFullname,
headRepoCloneURL,
e.BitbucketUser,
e.BitbucketToken)
if err != nil {
return
}
baseRepoSlug := *event.PullRequest.ToRef.Repository.Slug
baseRepoFullname := fmt.Sprintf("%s/%s", *event.PullRequest.ToRef.Repository.Project.Name, baseRepoSlug)
baseRepoCloneURL := fmt.Sprintf("%s/scm/%s/%s.git", e.BitbucketServerURL, strings.ToLower(*event.PullRequest.ToRef.Repository.Project.Key), baseRepoSlug)
baseRepo, err = models.NewRepo(
models.BitbucketServer,
baseRepoFullname,
baseRepoCloneURL,
e.BitbucketUser,
e.BitbucketToken)
if err != nil {
return
}
pull = models.PullRequest{
Num: *event.PullRequest.ID,
HeadCommit: *event.PullRequest.FromRef.LatestCommit,
URL: fmt.Sprintf("%s/projects/%s/repos/%s/pull-requests/%d", e.BitbucketServerURL, *event.PullRequest.ToRef.Repository.Project.Key, *event.PullRequest.ToRef.Repository.Slug, *event.PullRequest.ID),
Branch: *event.PullRequest.FromRef.DisplayID,
Author: *event.Actor.Username,
State: prState,
BaseRepo: baseRepo,
}
user = models.User{
Username: *event.Actor.Username,
}
return
}
// ParseBitbucketServerPullEvent parses a pull request event from Bitbucket
// Server.
// See EventParsing for return value docs.
func (e *EventParser) ParseBitbucketServerPullEvent(body []byte) (pull models.PullRequest, baseRepo models.Repo, headRepo models.Repo, user models.User, err error) {
var event bitbucketserver.PullRequestEvent
if err = json.Unmarshal(body, &event); err != nil {
err = errors.Wrap(err, "parsing json")
return
}
if err = validator.New().Struct(event); err != nil {
err = errors.Wrapf(err, "API response %q was missing fields", string(body))
return
}
pull, baseRepo, headRepo, user, err = e.parseCommonBitbucketServerEventData(event.CommonEventData)
return
}