-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
github_actions.go
122 lines (113 loc) · 3.58 KB
/
github_actions.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
package cienv
import (
"encoding/json"
"errors"
"os"
)
// https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
type GitHubEvent struct {
PullRequest GitHubPullRequest `json:"pull_request"`
Repository struct {
Owner struct {
Login string `json:"login"`
} `json:"owner"`
Name string `json:"name"`
} `json:"repository"`
CheckSuite struct {
After string `json:"after"`
PullRequests []GitHubPullRequest `json:"pull_requests"`
} `json:"check_suite"`
HeadCommit struct {
ID string `json:"id"`
} `json:"head_commit"`
ActionName string `json:"-"` // this is defined as env GITHUB_EVENT_NAME
}
type GitHubRepo struct {
Owner struct {
ID int64 `json:"id"`
}
}
type GitHubPullRequest struct {
Number int `json:"number"`
Head struct {
Sha string `json:"sha"`
Ref string `json:"ref"`
Repo GitHubRepo `json:"repo"`
} `json:"head"`
Base struct {
Repo GitHubRepo `json:"repo"`
} `json:"base"`
}
// LoadGitHubEvent loads GitHubEvent if it's running in GitHub Actions.
func LoadGitHubEvent() (*GitHubEvent, error) {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
if eventPath == "" {
return nil, errors.New("GITHUB_EVENT_PATH not found")
}
return loadGitHubEventFromPath(eventPath)
}
func loadGitHubEventFromPath(eventPath string) (*GitHubEvent, error) {
f, err := os.Open(eventPath)
if err != nil {
return nil, err
}
defer f.Close()
var event GitHubEvent
if err := json.NewDecoder(f).Decode(&event); err != nil {
return nil, err
}
event.ActionName = os.Getenv("GITHUB_EVENT_NAME")
return &event, nil
}
func getBuildInfoFromGitHubAction() (*BuildInfo, bool, error) {
eventPath := os.Getenv("GITHUB_EVENT_PATH")
if eventPath == "" {
return nil, false, errors.New("GITHUB_EVENT_PATH not found")
}
return getBuildInfoFromGitHubActionEventPath(eventPath)
}
func getBuildInfoFromGitHubActionEventPath(eventPath string) (*BuildInfo, bool, error) {
event, err := loadGitHubEventFromPath(eventPath)
if err != nil {
return nil, false, err
}
info := &BuildInfo{
Owner: event.Repository.Owner.Login,
Repo: event.Repository.Name,
PullRequest: event.PullRequest.Number,
Branch: event.PullRequest.Head.Ref,
SHA: event.PullRequest.Head.Sha,
}
// For re-run check_suite event.
if info.PullRequest == 0 && len(event.CheckSuite.PullRequests) > 0 {
pr := event.CheckSuite.PullRequests[0]
info.PullRequest = pr.Number
info.Branch = pr.Head.Ref
info.SHA = pr.Head.Sha
}
if info.SHA == "" {
info.SHA = event.HeadCommit.ID
}
if info.SHA == "" {
info.SHA = os.Getenv("GITHUB_SHA")
}
return info, info.PullRequest != 0, nil
}
// IsInGitHubAction returns true if reviewdog is running in GitHub Actions.
func IsInGitHubAction() bool {
// https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
// > Always set to true when GitHub Actions is running the workflow.
// > You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
return os.Getenv("GITHUB_ACTIONS") != ""
}
// HasReadOnlyPermissionGitHubToken returns true if reviewdog is running in GitHub
// Actions and running for PullRequests from forked repository with read-only token.
// https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target
func HasReadOnlyPermissionGitHubToken() bool {
event, err := LoadGitHubEvent()
if err != nil {
return false
}
isForkedRepo := event.PullRequest.Head.Repo.Owner.ID != event.PullRequest.Base.Repo.Owner.ID
return isForkedRepo && event.ActionName != "pull_request_target"
}