-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
241 lines (202 loc) · 4.63 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"archive/zip"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"strconv"
"github.com/google/go-github/v40/github"
"golang.org/x/oauth2"
)
const owner = "GitJournal"
const repoName = "GitJournal"
const artifactName = "APK"
const artifactsDir = "./repo"
const processedArtifactsFile = "processed_artifacts.json"
func main() {
token := flag.String("token", "", "GitHub Access Token")
flag.Parse()
if token == nil || len(*token) == 0 {
val := os.Getenv("GITHUB_TOKEN")
if len(val) == 0 {
log.Fatal(("Missing GitHub Access Token"))
}
token = &val
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: *token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
artifacts, _, err := client.Actions.ListArtifacts(ctx, owner, repoName, nil)
if err != nil {
log.Fatal(err)
}
artifactsZipDir, err := ioutil.TempDir(os.TempDir(), "artifacts")
if err != nil {
log.Fatal("ioutil.TempDir: %w", err)
}
defer os.RemoveAll(artifactsZipDir)
err = os.MkdirAll(artifactsDir, 0755)
if err != nil {
log.Fatal(err)
}
processedArtifacts, err := readProcessedArtifacts()
if err != nil {
log.Fatal("readProcesssedArtifacts: %w", err)
}
for _, artifact := range artifacts.Artifacts {
if artifact.GetName() != artifactName {
continue
}
if artifact.GetExpired() {
continue
}
id := strconv.Itoa(int(artifact.GetID()))
contains := false
for _, aID := range processedArtifacts {
if id == aID {
contains = true
break
}
}
if contains {
continue
}
fileName := artifactName + id + ".zip"
fileName = path.Join(artifactsZipDir, fileName)
_, err := os.Stat(fileName)
if !os.IsNotExist(err) {
continue
}
fmt.Println("Downloading", fileName)
err = DownloadArtifact(ctx, client, artifact, fileName)
if err != nil {
log.Fatal(err)
}
err = Unzip(fileName, artifactsDir)
if err != nil {
log.Fatal(err)
}
}
processedArtifacts = []string{}
for _, artifact := range artifacts.Artifacts {
id := strconv.Itoa(int(artifact.GetID()))
processedArtifacts = append(processedArtifacts, id)
}
err = writeProcessedArtifacts(processedArtifacts)
if err != nil {
log.Fatal(err)
}
}
func DownloadArtifact(ctx context.Context, client *github.Client, arifact *github.Artifact, filepath string) error {
req, err := client.NewRequest("GET", arifact.GetArchiveDownloadURL(), nil)
if err != nil {
return fmt.Errorf("DownloadArtifact Build Req: %w", err)
}
// Create the file
out, err := os.Create(filepath)
if err != nil {
return fmt.Errorf("DownloadArtifact os create: %w", err)
}
defer out.Close()
// Get the data
resp, err := client.Do(ctx, req, out)
if err != nil {
return fmt.Errorf("DownloadArtifact Http Get: %w", err)
}
defer resp.Body.Close()
return nil
}
func Unzip(src, dest string) error {
dest = filepath.Clean(dest) + string(os.PathSeparator)
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
path := filepath.Join(dest, f.Name)
// Check for ZipSlip: https://snyk.io/research/zip-slip-vulnerability
if !strings.HasPrefix(path, dest) {
return fmt.Errorf("%s: illegal file path", path)
}
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}
func readProcessedArtifacts() ([]string, error) {
var data []string
file, err := ioutil.ReadFile(processedArtifactsFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return data, nil
}
return []string{}, err
}
err = json.Unmarshal(file, &data)
if err != nil {
return []string{}, err
}
return data, nil
}
func writeProcessedArtifacts(list []string) error {
bytes, err := json.Marshal(list)
if err != nil {
return err
}
err = ioutil.WriteFile(processedArtifactsFile, bytes, 0644)
if err != nil {
return err
}
return nil
}