-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
102 lines (95 loc) · 2.98 KB
/
run.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
package githubrun
import (
"github.com/google/go-github/v32/github"
"io/ioutil"
"os"
"strconv"
"strings"
)
// Run represents a Github run.
//
// https://help.github.com/en/actions/reference/events-that-trigger-workflows
type Run struct {
Env Env
Payload interface{}
Owner string
Repository string
}
// Env contains the default environment variables.
//
// https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables
// https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token
type Env struct {
CI bool
Home string
GithubWorkflow string
GithubRunID int64
GithubRunNumber int64
GithubAction string
GithubActions bool
GithubActor string
GithubRepository string
GithubEventName string
GithubEventPath string
GithubWorkspace string
GithubSHA string
GithubRef string
GithubHeadRef string
GithubBaseRef string
GithubToken string
}
// ParseRun parses the event payload and default environment variables. A struct containing the env variables,
// and payload of the corresponding struct type will be returned.
func ParseRun() (Run, error) {
r := new(Run)
ci, err := strconv.ParseBool(os.Getenv("CI"))
if err != nil {
return Run{}, err
}
r.Env.CI = ci
r.Env.Home = os.Getenv("HOME")
r.Env.GithubWorkflow = os.Getenv("GITHUB_WORKFLOW")
runID, err := strconv.ParseInt(os.Getenv("GITHUB_RUN_ID"), 10, 64)
if err != nil {
return Run{}, err
}
r.Env.GithubRunID = runID
runNumber, err := strconv.ParseInt(os.Getenv("GITHUB_RUN_NUMBER"), 10, 64)
if err != nil {
return Run{}, err
}
r.Env.GithubRunNumber = runNumber
r.Env.GithubAction = os.Getenv("GITHUB_ACTION")
actions, err := strconv.ParseBool(os.Getenv("GITHUB_ACTIONS"))
if err != nil {
return Run{}, err
}
r.Env.GithubActions = actions
r.Env.GithubActor = os.Getenv("GITHUB_ACTOR")
r.Env.GithubRepository = os.Getenv("GITHUB_REPOSITORY")
r.Env.GithubEventName = os.Getenv("GITHUB_EVENT_NAME")
r.Env.GithubEventPath = os.Getenv("GITHUB_EVENT_PATH")
r.Env.GithubWorkspace = os.Getenv("GITHUB_WORKSPACE")
r.Env.GithubSHA = os.Getenv("GITHUB_SHA")
r.Env.GithubRef = os.Getenv("GITHUB_REF")
r.Env.GithubHeadRef = os.Getenv("GITHUB_HEAD_REF")
r.Env.GithubBaseRef = os.Getenv("GITHUB_BASE_REF")
r.Env.GithubToken = os.Getenv("GITHUB_TOKEN")
// Split "owner/repository" string into more useful owner and repository values
repository := strings.Split(os.Getenv("GITHUB_REPOSITORY"), "/")
r.Owner = repository[0]
r.Repository = repository[1]
// Parse the webhook event payload, For recognized event types,
// a value of the corresponding struct type will be returned.
jsonFile, err := os.Open(os.Getenv("GITHUB_EVENT_PATH"))
if err != nil {
return Run{}, err
}
byteValue, _ := ioutil.ReadAll(jsonFile)
payload, err := github.ParseWebHook(os.Getenv("GITHUB_EVENT_NAME"), byteValue)
if err != nil {
return Run{}, err
}
r.Payload = payload
return *r, nil
}