Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): manage change not found in gerrit #5514

Merged
merged 3 commits into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions engine/api/purge/purge_run.go
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 @@ -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
Expand Down
7 changes: 6 additions & 1 deletion engine/vcs/gerrit/client_pull_request.go
Expand Up @@ -2,6 +2,7 @@ package gerrit

import (
"context"
"net/http"

"github.com/andygrunwald/go-gerrit"

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