From d33d792b8b8f657ed9eb20326c4daca43a977a52 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 19 Oct 2020 15:35:54 +0200 Subject: [PATCH 1/3] fix(api): manage 404 on gerrit get change --- engine/api/purge/purge_run.go | 15 +++++++++++---- engine/vcs/gerrit/client_pull_request.go | 7 ++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/engine/api/purge/purge_run.go b/engine/api/purge/purge_run.go index 7ae06f10ca..f5ec97749a 100644 --- a/engine/api/purge/purge_run.go +++ b/engine/api/purge/purge_run.go @@ -30,6 +30,7 @@ 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" @@ -197,11 +198,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..ddd74f0234 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,12 @@ 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 { From ba8d0b11a0d1c5795bfde3f0b6d714affac5bacf Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 19 Oct 2020 16:07:08 +0200 Subject: [PATCH 2/3] fix: add new variable in list --- engine/api/purge/purge_run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/api/purge/purge_run.go b/engine/api/purge/purge_run.go index f5ec97749a..dd644ecefd 100644 --- a/engine/api/purge/purge_run.go +++ b/engine/api/purge/purge_run.go @@ -37,7 +37,7 @@ const ( ) 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 { From 4585539cbe260e74c3c26d8d0bd2c25573f5ba11 Mon Sep 17 00:00:00 2001 From: Steven Guiheux Date: Mon, 19 Oct 2020 16:08:33 +0200 Subject: [PATCH 3/3] fix: add new variable in list --- engine/vcs/gerrit/client_pull_request.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/engine/vcs/gerrit/client_pull_request.go b/engine/vcs/gerrit/client_pull_request.go index ddd74f0234..12d49e726d 100644 --- a/engine/vcs/gerrit/client_pull_request.go +++ b/engine/vcs/gerrit/client_pull_request.go @@ -16,8 +16,7 @@ func (c *gerritClient) PullRequest(_ context.Context, _ string, id string) (sdk. 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.WrapError(sdk.ErrNotFound, "unable to find change %s", id) } return sdk.VCSPullRequest{}, sdk.WithStack(err) }