8000 feat(api): add git hash short variable auto (#4119) · ovh/cds@2369134 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 2369134

Browse files
richardltyesnault
authored andcommitted
feat(api): add git hash short variable auto (#4119)
1 parent f93b278 commit 2369134

File tree

7 files changed

+39
-10
lines changed

7 files changed

+39
-10
lines changed

docs/content/docs/concepts/variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ echo $CDS_PARENT_APPLICATION
9898
Here is the list of git variables:
9999

100100
- `{{.git.hash}}`
101+
- `{{.git.hash.short}}`
101102
- `{{.git.url}}`
102103
- `{{.git.http_url}}`
103104
- `{{.git.branch}}`

engine/api/suggest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func (api *API) getVariablesHandler() service.Handler {
135135
// add git variable
136136
gitVar := []string{
137137
"{{.git.hash}}",
138+
"{{.git.hash.short}}",
138139
"{{.git.branch}}",
139140
"{{.git.tag}}",
140141
"{{.git.author}}",

engine/api/workflow/process_parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func GetNodeBuildParameters(proj *sdk.Project, w *sdk.Workflow, runContext nodeR
9898
e.ExtraFields.Type = false
9999
tmpVars, errdump := e.ToStringMap(payload)
100100
if errdump != nil {
101-
return nil, sdk.WrapError(errdump, "GetNodeBuildParameters> do-dump error")
101+
return nil, sdk.WrapError(errdump, "do-dump error")
102102
}
103103

104104
//Merge the dumped payload with vars
@@ -206,7 +206,7 @@ func getNodeRunBuildParameters(ctx context.Context, proj *sdk.Project, wr *sdk.W
206206
//Get node build parameters
207207
params, errparam := GetNodeBuildParameters(proj, &wr.Workflow, runContext, run.PipelineParameters, run.Payload, run.HookEvent)
208208
if errparam != nil {
209-
return nil, sdk.WrapError(errparam, "getNodeRunParameters> Unable to compute node build parameters")
209+
return nil, sdk.WrapError(errparam, "unable to compute node build parameters")
210210
}
211211

212212
errm := &sdk.MultiError{}

engine/api/workflow_trigger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (api *API) getWorkflowTriggerConditionHandler() service.Handler {
9090
data.ConditionNames = append(data.ConditionNames, "git.message")
9191
data.ConditionNames = append(data.ConditionNames, "git.author")
9292
data.ConditionNames = append(data.ConditionNames, "git.hash")
93+
data.ConditionNames = append(data.ConditionNames, "git.hash.short")
9394
data.ConditionNames = append(data.ConditionNames, "git.tag")
9495
}
9596
}

engine/hooks/poller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func fillPayload(pushEvent sdk.VCSPushEvent) map[string]string {
1818
payload["git.author.email"] = pushEvent.Commit.Author.Email
1919
payload["git.branch"] = strings.TrimPrefix(strings.TrimPrefix(pushEvent.Branch.DisplayID, "refs/heads/"), "refs/tags/")
2020
payload["git.hash"] = pushEvent.Commit.Hash
21+
if len(pushEvent.Commit.Hash) >= 7 {
22+
payload["git.hash.short"] = pushEvent.Commit.Hash[:7]
23+
}
2124
payload["git.repository"] = pushEvent.Repo
2225
payload["cds.triggered_by.username"] = pushEvent.Commit.Author.DisplayName
2326
payload["cds.triggered_by.fullname"] = pushEvent.Commit.Author.Name

engine/hooks/webhook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.WorkflowNodeRunHookEv
6161
}
6262
payload["git.hash.before"] = pushEvent.Before
6363
payload["git.hash"] = pushEvent.After
64+
if len(pushEvent.After) >= 7 {
65+
payload["git.hash.short"] = pushEvent.After[:7]
66+
}
6467
payload["git.repository"] = pushEvent.Repository.FullName
6568
payload["cds.triggered_by.username"] = pushEvent.HeadCommit.Author.Username
6669
payload["cds.triggered_by.fullname"] = pushEvent.HeadCommit.Author.Name
@@ -99,6 +102,9 @@ func executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.WorkflowNodeRunHookEv
99102
}
100103
payload["git.hash.before"] = pushEvent.Before
101104
payload["git.hash"] = pushEvent.After
105+
if len(pushEvent.After) >= 7 {
106+
payload["git.hash.short"] = pushEvent.After[:7]
107+
}
102108
payload["git.repository"] = pushEvent.Project.PathWithNamespace
103109

104110
payload["cds.triggered_by.username"] = pushEvent.UserUsername
@@ -139,6 +145,9 @@ func executeRepositoryWebHook(t *sdk.TaskExecution) ([]sdk.WorkflowNodeRunHookEv
139145
}
140146
payload["git.hash.before"] = pushChange.FromHash
141147
payload["git.hash"] = pushChange.ToHash
148+
if len(pushChange.ToHash) >= 7 {
149+
payload["git.hash.short"] = pushChange.ToHash[:7]
150+
}
142151
payload["git.repository"] = fmt.Sprintf("%s/%s", pushEvent.Repository.Project.Key, pushEvent.Repository.Slug)
143152

144153
payload["cds.triggered_by.username"] = pushEvent.Actor.Name

engine/worker/builtin_gitclone.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ func runGitClone(w *currentWorker) BuiltInAction {
160160
var opts = &git.CloneOpts{
161161
Recursive: true,
162162
NoStrictHostKeyChecking: true,
163-
Depth: 50,
164-
Tag: tag,
163+
Depth: 50,
164+
Tag: tag,
165165
}
166166
if branch != nil {
167167
opts.Branch = branch.Value
@@ -377,14 +377,28 @@ func extractInfo(w *currentWorker, dir string, params *[]sdk.Parameter, tag, bra
377377

378378
if commit == "" || commit == "{{.git.hash}}" {
379379
if info.Hash != "" {
380-
gitHash := sdk.Variable{
381-
Name: "git.hash",
382-
Type: sdk.StringVariable,
383-
Value: info.Hash,
380+
if _, err := w.addVariableInPipelineBuild(
381+
sdk.Variable{
382+
Name: "git.hash",
383+
Type: sdk.StringVariable,
384+
Value: info.Hash,
385+
},
386+
params,
387+
); err != nil {
388+
return fmt.Errorf("Error on addVariableInPipelineBuild (hash): %s", err)
384389
}
385390

386-
if _, err := w.addVariableInPipelineBuild(gitHash, params); err != nil {
387-
return fmt.Errorf("Error on addVariableInPipelineBuild (hash): %s", err)
391+
if len(info.Hash) >= 7 {
392+
if _, err := w.addVariableInPipelineBuild(
393+
sdk.Variable{
394+
Name: "git.hash.short",
395+
Type: sdk.StringVariable,
396+
Value: info.Hash[:7],
397+
},
398+
params,
399+
); err != nil {
400+
return fmt.Errorf("Error on addVariableInPipelineBuild (hash): %s", err)
401+
}
388402
}
389403
sendLog(fmt.Sprintf("git.hash: %s", info.Hash))
390404
} else {

0 commit comments

Comments
 (0)
0