-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_upstream_versions.go
205 lines (171 loc) · 5.23 KB
/
check_upstream_versions.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
package main
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"os"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)
type DockerStream struct {
Image string `json:"image"`
Tag string `json:"tag"`
Label string `json:"label"`
Stream
}
type GitHubStream struct {
Repo string `json:"repo"`
Branch string `json:"branch"`
Stream
}
type Stream struct {
Name string `json:"name"`
Type string `json:"type"`
}
type Project struct {
Name string `json:"name"`
ProjectType string `json:"type"`
Branch string `json:"branch"`
BuildWorkflow string `json:"build_workflow_filename"`
JsonUpstreams []map[string]interface{} `json:"upstreams"`
JsonDownstreams []map[string]interface{} `json:"downstreams"`
Upstreams []interface{}
Downstreams []interface{}
}
type Projects struct {
Projects []Project `json:"projects"`
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func readStreamsJson(filename string) Projects {
jsonFile, err := os.Open(filename)
check(err)
defer jsonFile.Close()
byteValue, err := ioutil.ReadAll(jsonFile)
check(err)
var projects, parsedProjects Projects
jsonErr := json.Unmarshal(byteValue, &projects)
check(jsonErr)
for _, project := range projects.Projects {
var upstreams, downstreams []interface{}
for _, upstream := range project.JsonUpstreams {
if upstream["type"] == "docker" {
var newUpstream DockerStream
newJson, err := json.Marshal(upstream)
check(err)
json.Unmarshal(newJson, &newUpstream)
upstreams = append(upstreams, newUpstream)
} else if upstream["type"] == "github" {
var newUpstream GitHubStream
newJson, err := json.Marshal(upstream)
check(err)
json.Unmarshal(newJson, &newUpstream)
upstreams = append(upstreams, newUpstream)
}
}
project.Upstreams = upstreams
for _, downstream := range project.JsonDownstreams {
if downstream["type"] == "docker" {
var newDownstream DockerStream
newJson, err := json.Marshal(downstream)
check(err)
json.Unmarshal(newJson, &newDownstream)
downstreams = append(downstreams, newDownstream)
} else if downstream["type"] == "github" {
var newDownstream GitHubStream
newJson, err := json.Marshal(downstream)
check(err)
json.Unmarshal(newJson, &newDownstream)
downstreams = append(downstreams, newDownstream)
}
}
project.Downstreams = downstreams
parsedProjects.Projects = append(parsedProjects.Projects, project)
}
return parsedProjects
}
func (image DockerStream) inspectDockerImageLabel() string {
ctx := context.Background()
imageString := image.Image + ":" + image.Tag
cli, err := client.NewClientWithOpts()
check(err)
log.Printf("Pulling image %s\n", imageString)
reader, err := cli.ImagePull(ctx, imageString, types.ImagePullOptions{})
check(err)
defer reader.Close()
_, err = ioutil.ReadAll(reader)
check(err)
log.Printf("Pulled image %s\n", imageString)
imageInspect, _, err := cli.ImageInspectWithRaw(ctx, imageString)
check(err)
if labelValue, ok := imageInspect.Config.Labels[image.Label]; ok {
return labelValue
}
log.Printf("Label %s not found in image %s.", image.Label, imageString)
return ""
}
func (repo GitHubStream) getLatestGitHubCommit() string {
ctx := context.Background()
client := github.NewClient(nil)
repo_info := strings.Split(repo.Repo, "/")
repo_owner := repo_info[0]
repo_name := repo_info[1]
branch, _, err := client.Repositories.GetBranch(ctx, repo_owner, repo_name, repo.Branch, true)
check(err)
return branch.GetCommit().GetSHA()
}
func main() {
projects := readStreamsJson("streams.json")
for _, project := range projects.Projects {
upstreamRefs := make(map[string]string)
for _, upstream := range project.Upstreams {
switch stream := upstream.(type) {
case DockerStream:
upstreamRefs[stream.Name] = stream.inspectDockerImageLabel()
case GitHubStream:
upstreamRefs[stream.Name] = stream.getLatestGitHubCommit()
}
}
downstreamRefs := make(map[string]string)
for _, downstream := range project.Downstreams {
switch stream := downstream.(type) {
case DockerStream:
downstreamRefs[stream.Name] = stream.inspectDockerImageLabel()
case GitHubStream:
downstreamRefs[stream.Name] = stream.getLatestGitHubCommit()
}
}
updateNeeded := false
for upstream, ref := range upstreamRefs {
if downstreamRefs[upstream] != ref {
updateNeeded = true
}
}
if updateNeeded {
projectInfo := strings.Split(project.Name, "/")
owner := projectInfo[0]
repo := projectInfo[1]
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GH_PAT")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
workflowReq := github.CreateWorkflowDispatchEventRequest{
Ref: project.Branch,
}
_, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), owner, repo, project.BuildWorkflow, workflowReq)
check(err)
log.Printf("%s: workflow \"%s\" successfully triggered.", project.Name, project.BuildWorkflow)
} else {
log.Printf("%s: no update needed!", project.Name)
}
}
}