-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanup.go
More file actions
173 lines (135 loc) · 3.9 KB
/
cleanup.go
File metadata and controls
173 lines (135 loc) · 3.9 KB
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
package command
import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"github.com/google/go-github/github"
cli "gopkg.in/urfave/cli.v1"
)
func CmdCleanup(c *cli.Context) (err error) {
listScript := c.String("list-script")
selectedPullRequest := c.StringSlice("pull-request")
ignoreMissing := c.Bool("ignore-missing")
if listScript == "" && len(selectedPullRequest) == 0 {
return errors.New("`--list-script` or `--pull-request` missing." +
"You have to define one or the other to select the PRs to cleanup")
}
var toUndeploy []string
ctx := context.Background()
ghCli := githubClient(ctx, c)
owner, repo := githubSlug(c)
if len(selectedPullRequest) > 0 {
// undeploy only selected pull requests
toUndeploy = selectedPullRequest
} else {
// undeploy all closed pull requests
var deployed []string
deployed, err = listDeployedPullRequests(listScript)
if err != nil {
return err
}
// Get the list of open PRs
prs, _, err := ghCli.PullRequests.List(ctx, owner, repo, &github.PullRequestListOptions{
State: "open",
})
if err != nil {
return err
}
openPRs := make([]string, len(prs))
for i, pr := range prs {
openPRs[i] = fmt.Sprintf("pr-%d", *pr.Number)
}
log.Println("open PRs:", openPRs)
// Now get a list of all the deployed PRs that are not open
for _, name := range deployed {
if !contains(name, openPRs) {
toUndeploy = append(toUndeploy, name)
}
}
}
log.Println("to undeploy:", toUndeploy)
var lastErr error
for _, name := range toUndeploy {
log.Println("Undeploying", name)
pullRequestID, err := strconv.Atoi(name)
if err != nil {
log.Println("Unable to parse pull request id: ", name)
lastErr = err
continue
}
cmd := exec.Command(c.Args().Get(0), c.Args()[1:]...) //#nosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println("undeploy error: ", err)
lastErr = err
continue
}
destroyGitHubDeployments(ctx, ghCli, owner, repo, pullRequestID, ignoreMissing)
}
return lastErr
}
func contains(item string, list []string) bool {
for _, entry := range list {
if item == entry {
return true
}
}
return false
}
// Get the list of deployed Pull request based on given script.
func listDeployedPullRequests(listScript string) ([]string, error) {
var stdout strings.Builder
cmd := exec.Command(listScript)
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return nil, err
}
lines := strings.Split(stdout.String(), "\n")
deployed := make([]string, 0, len(lines))
for _, line := range lines {
if line == "" {
continue
}
deployed = append(deployed, line)
}
log.Println("deployed:", deployed)
return deployed, nil
}
// Destroy deployments related to a PR by marking them.
func destroyGitHubDeployments(ctx context.Context, ghCli *github.Client, owner string, repo string, pullRequestID int,
ignoreMissing bool,
) {
// Look for existing deployments related to the pull request by filtering deployments
// by the environment name that matches the pattern 'pr-{pullRequestID}' (as the 'deploy'
// action creates deployments with such names).
deployments, _, err := ghCli.Repositories.ListDeployments(ctx, owner, repo, &github.DeploymentsListOptions{
Task: TaskName,
Environment: fmt.Sprintf("pr-%d", pullRequestID),
})
if err != nil {
log.Fatalf("Error while listing deployments for PR %d", pullRequestID)
}
if len(deployments) == 0 {
if ignoreMissing {
log.Println("No deployments found for PR ", pullRequestID)
} else {
log.Fatalf("unable to find deployments related to PR %d", pullRequestID)
}
}
for _, deployment := range deployments {
_, _, err := ghCli.Repositories.CreateDeploymentStatus(ctx, owner, repo,
*deployment.ID, &github.DeploymentStatusRequest{
State: refString("inactive"),
})
if err != nil {
log.Println("Error while inactivating deployment for PR ", pullRequestID)
}
}
}