Skip to content

Commit

Permalink
fix(api): manage change not found in gerrit (#5514)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Oct 20, 2020
1 parent 4b4b725 commit 9f00a81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 12 additions & 5 deletions engine/api/purge/purge_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion engine/vcs/gerrit/client_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gerrit

import (
"context"
"net/http"

"github.com/andygrunwald/go-gerrit"

Expand All @@ -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 {
Expand Down

0 comments on commit 9f00a81

Please sign in to comment.