-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
98 lines (85 loc) · 2.32 KB
/
context.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
package main
import (
"fmt"
"strings"
"github.com/libgit2/git2go/v34"
)
type Context struct {
BuildNumber uint64
BuildkitePipelineSlug string
BuildkiteOrganizationSlug string
BuildkiteAgentAccessToken string
BuildkiteAgentEndpointURL string
BuildkiteAPIAccessToken string
BuildkiteJobId string
BuildkiteBuildId string
ConfigFilename string
BranchName string
BuildMessage string
RepoURL string
InPullRequest bool
BuildEnvironment string
CodeVersion string
SourceGitCommitId string
ArtifactsFromBuildNumber string
OverrideDeployEnvironmentName string
}
type StepContext struct {
EnvironmentName string
QueueName string
EmojiName string
Cautious bool
// use concurrency and concurrency_group to force only one to run at a time
PreventConcurrency bool
}
// CodebaseName tries to infer a name for the codebase from the repository
// URL.
//
// It does this by extracting the final slash-separated portion of the URL,
// and removing a .git suffix if present.
//
// For example, git@github.com:example/foo.git would return "foo".
func (c *Context) CodebaseName() string {
repoUrl := c.RepoURL
slashIndex := strings.LastIndex(repoUrl, "/")
lastPart := repoUrl[slashIndex+1:]
if strings.HasSuffix(lastPart, ".git") {
lastPart = lastPart[:len(lastPart)-4]
}
return lastPart
}
func (c *Context) DoMessageMagic() {
{
matchParts := rollbackMessageRegexp.FindStringSubmatch(c.BuildMessage)
if len(matchParts) == 3 {
c.ArtifactsFromBuildNumber = matchParts[2]
return
}
}
{
matchParts := envOverrideMessageRegexp.FindStringSubmatch(c.BuildMessage)
if len(matchParts) == 5 {
c.ArtifactsFromBuildNumber = matchParts[2]
c.OverrideDeployEnvironmentName = matchParts[4]
return
}
}
}
func (c *Context) SetGitCommit(commit *git.Commit) {
commitId := commit.Id()
commitTime := commit.Committer().When.UTC()
c.CodeVersion = fmt.Sprintf(
"%s-%s-%06d",
commitTime.Format("2006-01-02-150405"),
commitId.String()[:7],
c.BuildNumber,
)
c.SourceGitCommitId = commitId.String()
}
func (c *StepContext) CautiousStr() string {
if c.Cautious {
return "1"
} else {
return "0"
}
}