-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
177 lines (159 loc) · 7.96 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"runtime"
"strings"
"github.com/alecthomas/kingpin"
foundation "github.com/estafette/estafette-foundation"
"github.com/rs/zerolog/log"
)
var (
appgroup string
app string
version string
branch string
revision string
buildDate string
goVersion = runtime.Version()
)
var (
// flags
gitSource = kingpin.Flag("git-source", "The source of the repository.").Envar("ESTAFETTE_GIT_SOURCE").Required().String()
gitOwner = kingpin.Flag("git-owner", "The owner of the repository.").Envar("ESTAFETTE_GIT_OWNER").Required().String()
gitName = kingpin.Flag("git-name", "The owner plus repository name.").Envar("ESTAFETTE_GIT_NAME").Required().String()
gitBranch = kingpin.Flag("git-branch", "The branch to clone.").Envar("ESTAFETTE_GIT_BRANCH").Required().String()
gitRevision = kingpin.Flag("git-revision", "The revision to check out.").Envar("ESTAFETTE_GIT_REVISION").String()
shallowClone = kingpin.Flag("shallow-clone", "Shallow clone git repository for improved clone time.").Default("true").OverrideDefaultFromEnvar("ESTAFETTE_EXTENSION_SHALLOW").Bool()
shallowCloneDepth = kingpin.Flag("shallow-clone-depth", "Depth for shallow clone git repository for improved clone time.").Default("50").OverrideDefaultFromEnvar("ESTAFETTE_EXTENSION_DEPTH").Int()
overrideRepo = kingpin.Flag("override-repo", "Set other repository name to clone from same owner.").Envar("ESTAFETTE_EXTENSION_REPO").String()
overrideBranch = kingpin.Flag("override-branch", "Set other repository branch to clone from same owner.").Envar("ESTAFETTE_EXTENSION_BRANCH").String()
overrideRevision = kingpin.Flag("override-revision", "Set other repository revision to clone from same owner.").Envar("ESTAFETTE_EXTENSION_REVISION").String()
overrideSubdirectory = kingpin.Flag("override-directory", "Set other repository directory to clone from same owner.").Envar("ESTAFETTE_EXTENSION_SUBDIR").String()
bitbucketAPITokenPath = kingpin.Flag("bitbucket-api-token-path", "Path to file with Bitbucket api token credentials configured at the CI server, passed in to this trusted extension.").Default("/credentials/bitbucket_api_token.json").String()
githubAPITokenPath = kingpin.Flag("github-api-token-path", "Path to file with Github api token credentials configured at the CI server, passed in to this trusted extension.").Default("/credentials/github_api_token.json").String()
cloudsourceAPITokenPath = kingpin.Flag("cloudsource-api-token-path", "Path to file with Cloud Source api token credentials configured at the CI server, passed in to this trusted extension.").Default("/credentials/cloudsource_api_token.json").String()
)
func main() {
// parse command line parameters
kingpin.Parse()
// init log format from envvar ESTAFETTE_LOG_FORMAT
applicationInfo := foundation.ApplicationInfo{
AppGroup: appgroup,
App: app,
Version: version,
Branch: branch,
Revision: revision,
BuildDate: buildDate,
}
foundation.InitLoggingFromEnv(applicationInfo)
// create context to cancel commands on sigterm
ctx := foundation.InitCancellationContext(context.Background())
// get api token from injected credentials
bitbucketAPIToken := ""
// use mounted credential file if present instead of relying on an envvar
if runtime.GOOS == "windows" {
*bitbucketAPITokenPath = "C:" + *bitbucketAPITokenPath
}
if foundation.FileExists(*bitbucketAPITokenPath) {
var credentials []APITokenCredentials
log.Info().Msgf("Reading credentials from file at path %v...", *bitbucketAPITokenPath)
credentialsFileContent, err := ioutil.ReadFile(*bitbucketAPITokenPath)
if err != nil {
log.Fatal().Msgf("Failed reading credential file at path %v.", *bitbucketAPITokenPath)
}
err = json.Unmarshal(credentialsFileContent, &credentials)
if err != nil {
log.Fatal().Err(err).Msg("Failed unmarshalling injected credentials")
}
bitbucketAPIToken = credentials[0].AdditionalProperties.Token
}
githubAPIToken := ""
// use mounted credential file if present instead of relying on an envvar
if runtime.GOOS == "windows" {
*githubAPITokenPath = "C:" + *githubAPITokenPath
}
if foundation.FileExists(*githubAPITokenPath) {
var credentials []APITokenCredentials
log.Info().Msgf("Reading credentials from file at path %v...", *githubAPITokenPath)
credentialsFileContent, err := ioutil.ReadFile(*githubAPITokenPath)
if err != nil {
log.Fatal().Msgf("Failed reading credential file at path %v.", *githubAPITokenPath)
}
err = json.Unmarshal(credentialsFileContent, &credentials)
if err != nil {
log.Fatal().Err(err).Msg("Failed unmarshalling injected credentials")
}
githubAPIToken = credentials[0].AdditionalProperties.Token
}
cloudsourceAPIToken := ""
// use mounted credential file if present instead of relying on an envvar
if runtime.GOOS == "windows" {
*cloudsourceAPITokenPath = "C:" + *cloudsourceAPITokenPath
}
if foundation.FileExists(*cloudsourceAPITokenPath) {
var credentials []APITokenCredentials
log.Info().Msgf("Reading credentials from file at path %v...", *cloudsourceAPITokenPath)
credentialsFileContent, err := ioutil.ReadFile(*cloudsourceAPITokenPath)
if err != nil {
log.Fatal().Msgf("Failed reading credential file at path %v.", *cloudsourceAPITokenPath)
}
err = json.Unmarshal(credentialsFileContent, &credentials)
if err != nil {
log.Fatal().Err(err).Msg("Failed unmarshalling injected credentials")
}
cloudsourceAPIToken = credentials[0].AdditionalProperties.Token
}
if *overrideRepo != "" {
if *overrideBranch == "" {
*overrideBranch = "master"
}
if *overrideSubdirectory == "" {
*overrideSubdirectory = *overrideRepo
}
overrideGitURL := fmt.Sprintf("https://%v/%v/%v", *gitSource, *gitOwner, *overrideRepo)
if strings.HasPrefix(*overrideRepo, "https://") {
// this allows for any public repo to be cloned
overrideGitURL = *overrideRepo
} else {
if bitbucketAPIToken != "" {
overrideGitURL = fmt.Sprintf("https://x-token-auth:%v@%v/%v/%v", bitbucketAPIToken, *gitSource, *gitOwner, *overrideRepo)
}
if githubAPIToken != "" {
overrideGitURL = fmt.Sprintf("https://x-access-token:%v@%v/%v/%v", githubAPIToken, *gitSource, *gitOwner, *overrideRepo)
}
if cloudsourceAPIToken != "" {
overrideGitURL = fmt.Sprintf("https://estafette:%v@%v/p/%v/r/%v", cloudsourceAPIToken, *gitSource, *gitOwner, *overrideRepo)
}
}
// git clone the specified repository branch to the specific directory
err := gitCloneOverride(ctx, *overrideRepo, overrideGitURL, *overrideBranch, *overrideRevision, *overrideSubdirectory, *shallowClone, *shallowCloneDepth)
if err != nil {
log.Fatal().Err(err).Msgf("Error cloning git repository %v to branch %v into subdir %v", *overrideRepo, *overrideBranch, *overrideSubdirectory)
}
return
}
gitURL := fmt.Sprintf("https://%v/%v/%v", *gitSource, *gitOwner, *gitName)
if bitbucketAPIToken != "" {
gitURL = fmt.Sprintf("https://x-token-auth:%v@%v/%v/%v", bitbucketAPIToken, *gitSource, *gitOwner, *gitName)
ctx = context.WithValue(ctx, "source", "bitbucket")
ctx = context.WithValue(ctx, "token", bitbucketAPIToken)
}
if githubAPIToken != "" {
gitURL = fmt.Sprintf("https://x-access-token:%v@%v/%v/%v", githubAPIToken, *gitSource, *gitOwner, *gitName)
ctx = context.WithValue(ctx, "source", "github")
ctx = context.WithValue(ctx, "token", githubAPIToken)
}
if cloudsourceAPIToken != "" {
gitURL = fmt.Sprintf("https://estafette:%v@%v/p/%v/r/%v", cloudsourceAPIToken, *gitSource, *gitOwner, *gitName)
ctx = context.WithValue(ctx, "source", "cloudsource")
ctx = context.WithValue(ctx, "token", cloudsourceAPIToken)
}
// git clone to specific branch and revision
err := gitCloneRevision(ctx, *gitName, gitURL, *gitBranch, *gitRevision, *shallowClone, *shallowCloneDepth)
if err != nil {
log.Fatal().Err(err).Msgf("Error cloning git repository %v to branch %v and revision %v with shallow clone is %v", *gitName, *gitBranch, *gitRevision, *shallowClone)
}
}