Skip to content

Commit

Permalink
fix(webhook): only set PRRef label if wfr is trigger by PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Jian Zeng committed Jan 22, 2021
1 parent f3d6761 commit 731adb9
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions pkg/server/handler/v1alpha1/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ func sanitizeRef(ref string) string {

func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.EventData) error {
ns := wft.Namespace
cancelPrevious := false
var err error
var project string
if wft.Labels != nil {
Expand All @@ -154,6 +153,7 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev
return fmt.Errorf("workflow reference of workflowtrigger is empty")
}

triggeredByPR := false
trigger := false
var tag string
st := wft.Spec.SCM
Expand Down Expand Up @@ -187,22 +187,22 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev
// Always trigger if Branches are not specified
if len(st.PullRequest.Branches) == 0 {
trigger = true
cancelPrevious = true
triggeredByPR = true
break
}

for _, branch := range st.PullRequest.Branches {
if branch == data.Branch {
trigger = true
cancelPrevious = true
triggeredByPR = true
break
}
}
case scm.PullRequestCommentEventType:
for _, comment := range st.PullRequestComment.Comments {
if comment == data.Comment {
trigger = true
cancelPrevious = true
triggeredByPR = true
break
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev

var currentWfrs []v1alpha1.WorkflowRun

if cancelPrevious {
if triggeredByPR {
wfrs, err := cycloneClient.WorkflowRuns(ns).List(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s,%s=%s,%s=%s",
meta.LabelProjectName, project,
Expand Down Expand Up @@ -281,7 +281,7 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev
}
} else {
log.Infof("The update time of PR %s/%s is unknown. Turn off canceling previous builds.", data.Repo, data.Ref)
cancelPrevious = false
triggeredByPR = false
}
}

Expand All @@ -306,11 +306,13 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev
meta.LabelProjectName: project,
meta.LabelWorkflowName: wfName,
meta.LabelWorkflowRunAcceleration: wft.Labels[meta.LabelWorkflowRunAcceleration],
meta.LabelWorkflowRunPRRef: ref,
},
},
Spec: wft.Spec.WorkflowRunSpec,
}
if triggeredByPR {
wfr.ObjectMeta.Labels[meta.LabelWorkflowRunPRRef] = ref
}

wfr.Annotations, err = setSCMEventData(wfr.Annotations, data)
if err != nil {
Expand Down Expand Up @@ -354,7 +356,7 @@ func createWorkflowRun(tenant string, wft v1alpha1.WorkflowTrigger, data *scm.Ev
}
}(wfr.DeepCopy())

if !cancelPrevious {
if !triggeredByPR {
return nil
}

Expand Down

0 comments on commit 731adb9

Please sign in to comment.