-
Notifications
You must be signed in to change notification settings - Fork 2
/
scm.go
178 lines (152 loc) · 5.04 KB
/
scm.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
package main
import (
"os"
"path"
)
type Scm interface {
OTELAttributesContributor
}
// ScmContext represent the execution context in which the SCM is used
type ScmContext struct {
// Branch the name of the branch, which will be calculated
// reading the environment variables for each supported context
Branch string
// ChangeRequest if the SCM context is for a change request
ChangeRequest bool
// Commit the commit hash for the SCM context
Commit string
// Provider the provider of the SCM context: Github, Gitlab, Jenkins, Other, etc.
Provider string
// TargetBranch the name of the branch in the case the SCM context represents a
// change request. In the case ChangeRequest is false, it won't be considered
TargetBranch string
}
// checkGitContext identifies the head sha and target branch from the environment variables that are
// populated from a Git provider, such as Github or Gitlab. If no proprietary env vars are set, then it will
// look up this tool-specific variable for the target branch.
func checkGitContext() *ScmContext {
// in local branches, we are not in pull/merge requests
localContext := FromLocal()
if localContext != nil {
return localContext
}
// is Github?
githubContext := FromGithub()
if githubContext != nil {
return githubContext
}
// is Jenkins?
jenkinsContext := FromJenkins()
if jenkinsContext != nil {
return jenkinsContext
}
// is Gitlab?
gitlabContext := FromGitlab()
if gitlabContext != nil {
return gitlabContext
}
// SCM context not supported
return nil
}
// GetTargetBranch returns the target branch for change requests, or branches in any other case
func (ctx *ScmContext) GetTargetBranch() string {
if ctx.ChangeRequest {
return ctx.TargetBranch
}
return ctx.Branch
}
// FromGithub returns an SCM context for Github, reading the right environment variables, as described
// in their docs
func FromGithub() *ScmContext {
if os.Getenv("GITHUB_SHA") == "" {
return nil
}
sha := os.Getenv("GITHUB_SHA")
branchName := os.Getenv("GITHUB_REF_NAME")
baseRef := os.Getenv("GITHUB_BASE_REF") // only present for pull requests on Github Actions
headRef := os.Getenv("GITHUB_HEAD_REF") // only present for pull requests on Github Actions
isChangeRequest := (baseRef != "" && headRef != "")
return &ScmContext{
ChangeRequest: isChangeRequest,
Commit: sha,
Branch: branchName,
Provider: "Github",
TargetBranch: baseRef,
}
}
// FromGitlab returns an SCM context for Gitlab, reading the right environment variables, as described
// in their docs
func FromGitlab() *ScmContext {
if os.Getenv("CI_COMMIT_REF_NAME") == "" {
return nil
}
sha := os.Getenv("CI_MERGE_REQUEST_SOURCE_BRANCH_SHA") // only present on merge requests on Gitlab CI
commitBranch := os.Getenv("CI_COMMIT_BRANCH") // only present on branches on Gitlab CI
headRef := os.Getenv("CI_COMMIT_REF_NAME") // only present on branches on Gitlab CI
baseRef := os.Getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME") // only present on merge requests on Gitlab CI
isChangeRequest := (commitBranch == "")
return &ScmContext{
ChangeRequest: isChangeRequest,
Commit: sha,
Branch: headRef,
Provider: "Gitlab",
TargetBranch: baseRef,
}
}
// FromJenkins returns an SCM context for Jenkins, reading the right environment variables, as described
// in their docs
func FromJenkins() *ScmContext {
if os.Getenv("JENKINS_URL") == "" {
return nil
}
isPR := os.Getenv("CHANGE_ID") != "" // only present on multibranch pipelines on Jenkins
headRef := os.Getenv("BRANCH_NAME") // only present on multibranch pipelines on Jenkins
sha := os.Getenv("GIT_COMMIT") // only present on multibranch pipelines on Jenkins
baseRef := os.Getenv("CHANGE_TARGET") // only present on multibranch pipelines on Jenkins
if isPR {
return &ScmContext{
ChangeRequest: isPR,
Commit: sha,
Branch: headRef,
Provider: "Jenkins",
TargetBranch: baseRef,
}
} else {
return &ScmContext{
ChangeRequest: isPR,
Commit: sha,
Branch: headRef,
Provider: "Jenkins",
TargetBranch: headRef,
}
}
}
// FromLocal returns an SCM context for local, using TARGET_BRANCH and BRANCH as the variables controlling
// if the SCM context represents a change request. BRANCH is mandatory, otherwise an empty context will be retrieved.
// If TARGET_BRANCH is not empty, it will represent a change request
func FromLocal() *ScmContext {
baseRef := os.Getenv("TARGET_BRANCH")
headRef := os.Getenv("BRANCH")
if headRef == "" {
return nil
}
isPR := (baseRef != "")
return &ScmContext{
ChangeRequest: isPR,
Commit: "",
Branch: headRef,
Provider: "",
TargetBranch: baseRef,
}
}
// GetScm checks if the underlying filesystem repository is a Git repository
// checking the existence of the .git directory in the current workspace
func GetScm(repoDir string) Scm {
// if .git file exists
_, err := os.Stat(path.Join(repoDir, ".git"))
if os.IsNotExist(err) {
return nil
}
// .git exists
return NewGitScm(repoDir)
}