-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
164 lines (135 loc) · 3.52 KB
/
main.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
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"github.com/cavaliercoder/grab"
"github.com/google/go-github/v24/github"
"go.transparencylog.com/tl/config"
"go.transparencylog.com/tl/sumdb"
)
func main() {
p := os.Getenv("GITHUB_EVENT_PATH")
if len(p) == 0 {
log.Fatalf("GITHUB_EVENT_PATH must be set")
}
f, err := os.Open(p)
if err != nil {
log.Fatalf("error opening event file %s: %v\n", p, err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("error reading file %s: %v\n", p, err)
}
e := github.ReleaseEvent{}
json.Unmarshal(b, &e)
var assets []string
for _, v := range e.Release.Assets {
assets = append(assets, *v.BrowserDownloadURL)
}
assets = append(assets, *e.Release.ZipballURL)
assets = append(assets, *e.Release.TarballURL)
assets = append(assets, archiveURLs(*e.Repo.Owner.Login, *e.Repo.Name, *e.Release.TagName)...)
assets = append(assets, additionalURLs()...)
var failed []string
var verified []string
for _, v := range assets {
err := get(v)
if err != nil {
fmt.Printf("%s: failed: %v", v, err)
failed = append(failed, v)
continue
}
verified = append(verified, v)
}
fmt.Printf("::set-output name=verified::%v\n", verified)
fmt.Printf("::set-output name=failed::%v\n", failed)
// Signal GitHub actions that this job should fail
if len(failed) > 0 {
os.Exit(1)
}
}
func get(durl string) error {
u, err := url.Parse(durl)
if err != nil {
panic(err)
}
key := u.Host + u.Path
cache := config.ClientCache()
// create download request
req, err := grab.NewRequest("", durl)
if err != nil {
return errors.New("failed to request URL")
}
req.NoCreateDirectories = true
req.SkipExisting = true
req.AfterCopy = func(resp *grab.Response) (err error) {
var f *os.File
f, err = os.Open(resp.Filename)
if err != nil {
return
}
defer func() {
f.Close()
}()
h := sha256.New()
_, err = io.Copy(h, f)
if err != nil {
return err
}
fileSum := h.Sum(nil)
// Download the tlog entry for the URL
want := "h1:" + base64.StdEncoding.EncodeToString(fileSum)
client := sumdb.NewClient(cache)
_, data, err := client.LookupOpts(key, sumdb.LookupOpts{Digest: want})
if err != nil {
return err
}
fmt.Printf("fetched note: %s/lookup/%s\n", config.ServerURL, key)
for _, line := range strings.Split(string(data), "\n") {
if line == want {
break
}
if strings.HasPrefix(line, "h1:") {
return errors.New("digest mismatch")
}
}
fmt.Printf("validated file sha256sum: %x\n", fileSum)
req.SetChecksum(sha256.New(), fileSum, true)
return
}
// download and validate file
resp := grab.DefaultClient.Do(req)
if err := resp.Err(); err != nil {
return err
}
return nil
}
// archiveURLs generates source archive URLs for a GitHub repo tag
// e.g. https://github.com/philips/releases-test/archive/v1.0.zip and
// https://github.com/philips/releases-test/archive/v1.0.tar.gz
func archiveURLs(owner, repo, tag string) (urls []string) {
u := url.URL{
Scheme: "https",
Host: "github.com",
Path: fmt.Sprintf("/%s/%s/archive/%s", owner, repo, tag),
}
urls = append(urls, u.String()+".tar.gz")
urls = append(urls, u.String()+".zip")
return
}
// additionalURLs parses inputs.additionalURLs and returns the []string list.
//
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
func additionalURLs() (urls []string) {
l := os.Getenv("INPUT_ADDITIONALURLS")
return strings.Split(l, " ")
}