-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
62 lines (54 loc) · 1.2 KB
/
github.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
package main
import (
"context"
"github.com/google/go-github/v39/github"
"golang.org/x/oauth2"
)
const (
sourceOwner = "diov"
sourceRepo = "gadio-rss"
targetRepo = "wiki"
commitBranch = "main"
)
var (
tempFilePath = "target/artifact.zip"
gitMgr *gitManager
)
type gitManager struct {
client *github.Client
}
func setupGitManager(token string) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(context.Background(), ts)
client := github.NewClient(tc)
gitMgr = &gitManager{client: client}
}
func (m *gitManager) getPreviousArtifact() error {
if nil == m {
return nil
}
opts := &github.ListOptions{Page: 1}
artifacts, _, err := m.client.Actions.ListArtifacts(context.Background(), sourceOwner, sourceRepo, opts)
if nil != err {
return err
}
if artifacts.GetTotalCount() <= 0 {
return nil
}
artifact := artifacts.Artifacts[0]
url, _, err := m.client.Actions.DownloadArtifact(context.Background(), sourceOwner, sourceRepo, artifact.GetID(), true)
if nil != err {
return err
}
err = downloadFile(url.String(), tempFilePath)
if nil != err {
return err
}
err = unzipFile(tempFilePath)
if nil != err {
return err
}
return err
}