Skip to content

Commit

Permalink
fix(*): do not update PR status if wfr is canceled automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Jian Zeng authored and caicloud-bot committed Dec 31, 2020
1 parent 0aeb2b2 commit f5ff885
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
11 changes: 11 additions & 0 deletions pkg/apis/cyclone/v1alpha1/workflow_run.go
Expand Up @@ -170,6 +170,17 @@ type PodInfo struct {
Namespace string `json:"namespace"`
}

const (
// ReasonAutoCancelPreviousBuild means this WorkflowRun is terminated because there is new WorkflowRun for the same PR.
ReasonAutoCancelPreviousBuild = "AutoCancelPreviousBuild"
// ReasonManuallyStop means this WorkflowRun is stopped manually.
ReasonManuallyStop = "ManuallyStop"
// ReasonManuallyPause means this WorkflowRun is paused manually.
ReasonManuallyPause = "ManuallyPause"
// ReasonManuallyResume means this WorkflowRun is resumed manually.
ReasonManuallyResume = "ManuallyResume"
)

// Status of a Stage in a WorkflowRun or the whole WorkflowRun.
// +k8s:deepcopy-gen=true
type Status struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/server/biz/scm/github/github.go
Expand Up @@ -424,6 +424,7 @@ func (g *Github) GetPullRequestSHA(repoURL string, number int) (string, error) {
pr, _, err := g.client.PullRequests.Get(g.ctx, owner, repo, number)
if err != nil {
log.Error(err)
return "", err
}

return *pr.Head.SHA, nil
Expand Down
12 changes: 8 additions & 4 deletions pkg/server/handler/v1alpha1/notification.go
Expand Up @@ -25,12 +25,16 @@ import (

// HandleWorkflowRunNotification handles workflowrun finished notification from workflow engine.
func HandleWorkflowRunNotification(ctx context.Context, wfr *v1alpha1.WorkflowRun) (interface{}, error) {
err := updatePullRequestStatus(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to update SCM status: ", err)
if wfr.Status.Overall.Reason != v1alpha1.ReasonAutoCancelPreviousBuild {
err := updatePullRequestStatus(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to update SCM status: ", err)
}
} else {
log.WithField("wfr", wfr.Name).WithField("namespace", wfr.Namespace).Infof("Skip auto-canceled workflowRun")
}

err = sendNotifications(wfr)
err := sendNotifications(wfr)
if err != nil {
log.WithField("wfr", wfr.Name).Error("Failed to send notifications: ", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/handler/v1alpha1/workflowrun.go
Expand Up @@ -211,7 +211,7 @@ func StopWorkflowRun(ctx context.Context, project, workflow, workflowrun, tenant
return nil, cerr.ConvertK8sError(err)
}

wfr, err = stopWorkflowRun(ctx, wfr, "ManuallyStop")
wfr, err = stopWorkflowRun(ctx, wfr, v1alpha1.ReasonManuallyStop)
if err != nil {
log.Errorf("Stop WorkflowRun %s error %s", workflowrun, err)
return nil, cerr.ConvertK8sError(err)
Expand All @@ -236,7 +236,7 @@ func stopWorkflowRun(ctx context.Context, wfr *v1alpha1.WorkflowRun, reason stri

// PauseWorkflowRun updates the workflowrun overall status to Waiting.
func PauseWorkflowRun(ctx context.Context, project, workflow, workflowrun, tenant string) (*v1alpha1.WorkflowRun, error) {
data, err := handler.BuildWfrStatusPatch(v1alpha1.StatusWaiting, "ManuallyPause")
data, err := handler.BuildWfrStatusPatch(v1alpha1.StatusWaiting, v1alpha1.ReasonManuallyPause)
if err != nil {
log.Errorf("pause workflowrun %s error %s", workflowrun, err)
return nil, err
Expand All @@ -249,7 +249,7 @@ func PauseWorkflowRun(ctx context.Context, project, workflow, workflowrun, tenan

// ResumeWorkflowRun updates the workflowrun overall status to Running.
func ResumeWorkflowRun(ctx context.Context, project, workflow, workflowrun, tenant string) (*v1alpha1.WorkflowRun, error) {
data, err := handler.BuildWfrStatusPatch(v1alpha1.StatusRunning, "ManuallyResume")
data, err := handler.BuildWfrStatusPatch(v1alpha1.StatusRunning, v1alpha1.ReasonManuallyResume)
if err != nil {
log.Errorf("continue workflowrun %s error %s", workflowrun, err)
return nil, err
Expand Down

0 comments on commit f5ff885

Please sign in to comment.