Skip to content

Commit

Permalink
feat(api): add has_git_branch variable to purge (#5765)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored Mar 12, 2021
1 parent f6c9b1f commit a7cc2bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/content/docs/concepts/workflow/retention.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Retention policy is defined through a lua condition. This condition should be ev

You will be able to use these variables in conditions:
* **run_days_before** (number): count of days between Workflow creation date and now.
* **has_git_branch** (string: true|false): True if a *git.branch* variable is set **(added in 0.48.1)**.
* **git_branch_exist** (string: true|false): True if a *git.branch* variable is set and branch still exists on the git repository.
* **run_status** (string: Success|Fail|...): the Workflow Run status.
* **gerrit_change_merged** (string: true|false): to identify if the gerrit change has been merged.
Expand All @@ -41,6 +42,19 @@ Examples:
-- Keep Run for 365 days
return run_days_before < 365
````
```lua
-- Keep Run for 365 days if git_branch is set and exists in VCS or only 2 days for removed branches
-- Else keep Run for 365 days if no git_branch info is set
if(has_git_branch == "true") then
if(git_branch_exist == "true") then
return run_days_before < 365
else
return run_days_before < 2
end
else
return run_days_before < 365
end
```
```lua
-- Keep Run for ever
return true
Expand Down
13 changes: 9 additions & 4 deletions engine/api/purge/purge_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type MarkAsDeleteOptions struct {
const (
RunStatus = "run_status"
RunDaysBefore = "run_days_before"
RunHasGitBranch = "has_git_branch"
RunGitBranchExist = "git_branch_exist"
RunChangeExist = "gerrit_change_exist"
RunChangeMerged = "gerrit_change_merged"
Expand All @@ -36,7 +37,7 @@ const (
)

func GetRetentionPolicyVariables() []string {
return []string{RunDaysBefore, RunStatus, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist}
return []string{RunDaysBefore, RunStatus, RunHasGitBranch, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist}
}

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

// If we have a branch in payload, check if it exists on repository branches list
if b, has := vars["git.branch"]; has {
_, exist := branchesMap[b]
vars[RunGitBranchExist] = strconv.FormatBool(exist)
b, has := vars["git.branch"]
var exist bool
if has {
_, exist = branchesMap[b]
}
vars[RunHasGitBranch] = strconv.FormatBool(has)
vars[RunGitBranchExist] = strconv.FormatBool(exist)

vars[RunStatus] = run.Status

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

0 comments on commit a7cc2bc

Please sign in to comment.