forked from runatlantis/atlantis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_status.go
79 lines (66 loc) · 1.68 KB
/
github_status.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
package events
import (
"fmt"
"strings"
"github.com/hootsuite/atlantis/server/events/github"
"github.com/hootsuite/atlantis/server/events/models"
)
type Status int
const (
statusContext = "Atlantis"
)
const (
Pending Status = iota
Success
Failure
Error
)
//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_github_status.go GHStatusUpdater
type GHStatusUpdater interface {
Update(repo models.Repo, pull models.PullRequest, status Status, cmd *Command) error
UpdateProjectResult(ctx *CommandContext, res CommandResponse) error
}
type GithubStatus struct {
Client github.Client
}
func (s Status) String() string {
switch s {
case Pending:
return "pending"
case Success:
return "success"
case Failure:
return "failure"
case Error:
return "error"
}
return "error"
}
func (g *GithubStatus) Update(repo models.Repo, pull models.PullRequest, status Status, cmd *Command) error {
description := fmt.Sprintf("%s %s", strings.Title(cmd.Name.String()), strings.Title(status.String()))
return g.Client.UpdateStatus(repo, pull, status.String(), description, statusContext)
}
func (g *GithubStatus) UpdateProjectResult(ctx *CommandContext, res CommandResponse) error {
var status Status
if res.Error != nil {
status = Error
} else if res.Failure != "" {
status = Failure
} else {
var statuses []Status
for _, p := range res.ProjectResults {
statuses = append(statuses, p.Status())
}
status = g.worstStatus(statuses)
}
return g.Update(ctx.BaseRepo, ctx.Pull, status, ctx.Command)
}
func (g *GithubStatus) worstStatus(ss []Status) Status {
worst := Success
for _, s := range ss {
if s > worst {
worst = s
}
}
return worst
}