diff --git a/engine/api/purge/purge_run.go b/engine/api/purge/purge_run.go index 47b556711b..c5f12d0873 100644 --- a/engine/api/purge/purge_run.go +++ b/engine/api/purge/purge_run.go @@ -30,13 +30,14 @@ const ( RunStatus = "run_status" RunDaysBefore = "run_days_before" RunGitBranchExist = "git_branch_exist" + RunChangeExist = "gerrit_change_exist" RunChangeMerged = "gerrit_change_merged" RunChangeAbandoned = "gerrit_change_abandoned" RunChangeDayBefore = "gerrit_change_days_before" ) func GetRetetionPolicyVariables() []string { - return []string{RunDaysBefore, RunStatus, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore} + return []string{RunDaysBefore, RunStatus, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist} } func markWorkflowRunsToDelete(ctx context.Context, store cache.Store, db *gorp.DbMap, workflowRunsMarkToDelete *stats.Int64Measure) error { @@ -198,11 +199,17 @@ func purgeComputeVariables(ctx context.Context, luaCheck *luascript.Check, run s if changeID, ok := vars["gerrit.change.id"]; ok && vcsClient != nil { ch, err := vcsClient.PullRequest(ctx, app.RepositoryFullname, changeID) if err != nil { - return err + if !sdk.ErrorIs(err, sdk.ErrNotFound) { + return err + } + vars[RunChangeExist] = "false" + } else { + vars[RunChangeExist] = "true" + vars[RunChangeMerged] = strconv.FormatBool(ch.Merged) + vars[RunChangeAbandoned] = strconv.FormatBool(ch.Closed) + varsFloats[RunChangeDayBefore] = math.Floor(time.Now().Sub(ch.Updated).Hours()) } - vars[RunChangeMerged] = strconv.FormatBool(ch.Merged) - vars[RunChangeAbandoned] = strconv.FormatBool(ch.Closed) - varsFloats[RunChangeDayBefore] = math.Floor(time.Now().Sub(ch.Updated).Hours()) + } // If we have a branch in payload, check if it exists on repository branches list diff --git a/engine/vcs/gerrit/client_pull_request.go b/engine/vcs/gerrit/client_pull_request.go index 42f187a6f2..12d49e726d 100644 --- a/engine/vcs/gerrit/client_pull_request.go +++ b/engine/vcs/gerrit/client_pull_request.go @@ -2,6 +2,7 @@ package gerrit import ( "context" + "net/http" "github.com/andygrunwald/go-gerrit" @@ -12,8 +13,11 @@ import ( // PullRequest Get a gerrit change func (c *gerritClient) PullRequest(_ context.Context, _ string, id string) (sdk.VCSPullRequest, error) { - change, _, err := c.client.Changes.GetChange(id, nil) + change, resp, err := c.client.Changes.GetChange(id, nil) if err != nil { + if resp != nil && resp.StatusCode == http.StatusNotFound { + return sdk.VCSPullRequest{}, sdk.WrapError(sdk.ErrNotFound, "unable to find change %s", id) + } return sdk.VCSPullRequest{}, sdk.WithStack(err) } if change == nil {