This repository has been archived by the owner on Nov 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
status.go
84 lines (67 loc) · 1.97 KB
/
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
80
81
82
83
84
package tugboat
import (
"encoding/json"
"strings"
"github.com/google/go-github/github"
)
// statusUpdater is an interface that's used to notify external services about
// the status of a deployment.
type statusUpdater interface {
// UpdateStatus should update an external service about the new status
// of the Deployment.
UpdateStatus(*Deployment) error
}
// pusherUpdater is a statusUpdater implementation that notifies pusher.
type pusherUpdater struct {
pusher Pusher
}
func (u *pusherUpdater) UpdateStatus(d *Deployment) error {
channel := deploymentChannel(d.ID)
data := struct {
ID string `json:"id"`
Status string `json:"status"`
}{
ID: d.ID,
Status: d.Status.String(),
}
raw, err := json.Marshal(&data)
if err != nil {
return err
}
return u.pusher.Publish(string(raw), "status", channel)
}
type githubClient interface {
CreateDeploymentStatus(owner, repo string, deployment int, request *github.DeploymentStatusRequest) (*github.DeploymentStatus, *github.Response, error)
}
// maps a DeploymentStatus to a github deployment status string.
var githubStatus = map[DeploymentStatus]string{
StatusStarted: "pending",
StatusSucceeded: "success",
StatusErrored: "error",
StatusFailed: "failure",
}
// githubUpdater is a statusUpdater implementation that creates deployment
// statuses on github for the commit.
type githubUpdater struct {
github githubClient
}
func (u *githubUpdater) UpdateStatus(d *Deployment) error {
sp := strings.Split(d.Repo, "/")
owner := sp[0]
repo := sp[1]
_, _, err := u.github.CreateDeploymentStatus(owner, repo, int(d.GitHubID), &github.DeploymentStatusRequest{
State: github.String(githubStatus[d.Status]),
TargetURL: github.String(d.URL()),
Description: github.String(d.Provider),
})
return err
}
type multiUpdater []statusUpdater
func (u multiUpdater) UpdateStatus(d *Deployment) error {
for _, updater := range u {
if err := updater.UpdateStatus(d); err != nil {
return err
}
}
return nil
}