forked from KevinSnyderCodes/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_command.go
79 lines (64 loc) · 1.5 KB
/
check_command.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 resource
import (
"fmt"
"io"
"sort"
"strconv"
)
type CheckCommand struct {
github GitHub
writer io.Writer
}
func NewCheckCommand(github GitHub, writer io.Writer) *CheckCommand {
return &CheckCommand{
github: github,
writer: writer,
}
}
func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
fmt.Fprintln(c.writer, "getting deployments list")
deployments, etag, err := c.github.ListDeployments(request.Version.ETag)
if err != nil {
return []Version{}, err
}
if etag != "" && etag == request.Version.ETag {
return []Version{request.Version}, nil
}
var latestVersions []Version
for _, deployment := range deployments {
if len(request.Source.Environments) > 0 {
found := false
for _, env := range request.Source.Environments {
if env == *deployment.Environment {
found = true
}
}
if !found {
continue
}
}
id := *deployment.ID
lastID, err := strconv.ParseInt(request.Version.ID, 10, 64)
if err != nil || id >= lastID {
latestVersions = append(latestVersions, Version{
ID: strconv.FormatInt(id, 10),
ETag: etag,
})
}
}
if len(latestVersions) == 0 {
return []Version{}, nil
}
sort.Slice(latestVersions[:], func(i, j int) bool {
iID, _ := strconv.Atoi(latestVersions[i].ID)
jID, _ := strconv.Atoi(latestVersions[j].ID)
return iID < jID
})
latestVersion := latestVersions[len(latestVersions)-1]
if request.Version.ID == "" {
return []Version{
latestVersion,
}, nil
}
return latestVersions, nil
}