generated from parrogo/gomod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
161 lines (134 loc) · 3.17 KB
/
args.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
package templatedir
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/google/go-github/v34/github"
"golang.org/x/oauth2"
)
// Args ...
type Args map[string]interface{}
func (a Args) String() string {
buf, err := json.MarshalIndent(a, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error in Args::String: %s\n", err.Error())
return ""
}
return string(buf)
}
// InitFromOSEnv ...
func (a *Args) InitFromOSEnv() {
if len(*a) == 0 {
*a = Args{}
}
for _, arg := range os.Environ() {
parts := strings.SplitN(arg, "=", 2)
argName := parts[0]
argValue := parts[1]
(*a)[argName] = argValue
}
}
// Author is {{.Author}}
// This repository is named {{.RepoName}}
// Local root of repository is {{.Root}}
// DefaultArgs ...
func DefaultArgs() (Args, error) {
var args Args
args.InitFromOSEnv()
info, ok := getRepoInfoFromGHActionEnv()
if !ok {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
info, err = getRepoInfoFromGit(cwd)
if err != nil {
return nil, err
}
}
args["Author"] = info.Author
args["RepoName"] = info.RepoName
args["Root"] = info.Root
user, repo, err := getGHInfos(info)
if err != nil {
return nil, err
}
if user == nil {
user = &github.User{}
}
if repo == nil {
repo = &github.Repository{}
}
args["User"] = user
args["Repo"] = repo
return args, nil
}
func getGHInfos(info repoInfo) (*github.User, *github.Repository, error) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, nil, nil
}
ctx, client, err := githubAuth(token)
if err != nil {
return nil, nil, err
}
user, _, err := client.Users.Get(ctx, info.Author)
if err != nil {
return nil, nil, err
}
repo, _, err := client.Repositories.Get(ctx, info.Author, info.RepoName)
if err != nil {
return nil, nil, err
}
return user, repo, nil
}
type repoInfo struct {
Author string
RepoName string
Root string
}
func getRepoInfoFromGHActionEnv() (repoInfo, bool) {
res := repoInfo{}
ghrepo := os.Getenv("GITHUB_REPOSITORY")
if ghrepo == "" {
return res, false
}
parts := strings.SplitN(ghrepo, "/", 2)
res.Author = parts[0]
res.RepoName = parts[1]
res.Root = os.Getenv("GITHUB_WORKSPACE")
return res, true
}
func getRepoInfoFromGit(dir string) (repoInfo, error) {
cmd := exec.Command("git", "remote", "get-url", "origin")
res := repoInfo{
Root: dir,
}
out, err := cmd.CombinedOutput()
if err != nil {
return res, fmt.Errorf("getRepoInfoFromGit error: %w\ngit command output:\n%s", err, out)
}
outs := strings.TrimRight(string(out), " \t\n\r")
ghPrefix := "https://github.com/"
if !strings.HasPrefix(outs, ghPrefix) {
return res, fmt.Errorf("getRepoInfoFromGit error: unknown provider %s", out)
}
outs = strings.TrimPrefix(outs, ghPrefix)
parts := strings.Split(outs, "/")
res.Author = parts[0]
res.RepoName = strings.TrimSuffix(parts[1], ".git")
return res, nil
}
// githubAuth returns a GitHub client and context.
func githubAuth(token string) (context.Context, *github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return ctx, client, nil
}