Skip to content

Commit a7cc2bc

Browse files
authored
feat(api): add has_git_branch variable to purge (#5765)
1 parent f6c9b1f commit a7cc2bc

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

docs/content/docs/concepts/workflow/retention.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Retention policy is defined through a lua condition. This condition should be ev
2929

3030
You will be able to use these variables in conditions:
3131
* **run_days_before** (number): count of days between Workflow creation date and now.
32+
* **has_git_branch** (string: true|false): True if a *git.branch* variable is set **(added in 0.48.1)**.
3233
* **git_branch_exist** (string: true|false): True if a *git.branch* variable is set and branch still exists on the git repository.
3334
* **run_status** (string: Success|Fail|...): the Workflow Run status.
3435
* **gerrit_change_merged** (string: true|false): to identify if the gerrit change has been merged.
@@ -41,6 +42,19 @@ Examples:
4142
-- Keep Run for 365 days
4243
return run_days_before < 365
4344
````
45+
```lua
46+
-- Keep Run for 365 days if git_branch is set and exists in VCS or only 2 days for removed branches
47+
-- Else keep Run for 365 days if no git_branch info is set
48+
if(has_git_branch == "true") then
49+
if(git_branch_exist == "true") then
50+
return run_days_before < 365
51+
else
52+
return run_days_before < 2
53+
end
54+
else
55+
return run_days_before < 365
56+
end
57+
```
4458
```lua
4559
-- Keep Run for ever
4660
return true

engine/api/purge/purge_run.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type MarkAsDeleteOptions struct {
2828
const (
2929
RunStatus = "run_status"
3030
RunDaysBefore = "run_days_before"
31+
RunHasGitBranch = "has_git_branch"
3132
RunGitBranchExist = "git_branch_exist"
3233
RunChangeExist = "gerrit_change_exist"
3334
RunChangeMerged = "gerrit_change_merged"
@@ -36,7 +37,7 @@ const (
3637
)
3738

3839
func GetRetentionPolicyVariables() []string {
39-
return []string{RunDaysBefore, RunStatus, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist}
40+
return []string{RunDaysBefore, RunStatus, RunHasGitBranch, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist}
4041
}
4142

4243
func markWorkflowRunsToDelete(ctx context.Context, store cache.Store, db *gorp.DbMap, workflowRunsMarkToDelete *stats.Int64Measure) error {
@@ -218,10 +219,14 @@ func purgeComputeVariables(ctx context.Context, luaCheck *luascript.Check, run s
218219
}
219220

220221
// If we have a branch in payload, check if it exists on repository branches list
221-
if b, has := vars["git.branch"]; has {
222-
_, exist := branchesMap[b]
223-
vars[RunGitBranchExist] = strconv.FormatBool(exist)
222+
b, has := vars["git.branch"]
223+
var exist bool
224+
if has {
225+
_, exist = branchesMap[b]
224226
}
227+
vars[RunHasGitBranch] = strconv.FormatBool(has)
228+
vars[RunGitBranchExist] = strconv.FormatBool(exist)
229+
225230
vars[RunStatus] = run.Status
226231

227232
varsFloats[RunDaysBefore] = math.Floor(time.Now().Sub(run.LastModified).Hours() / 24)

0 commit comments

Comments
 (0)