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

feat(server): auto cancel previous builds triggered by PR events #1552

Merged
merged 2 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 1 addition & 18 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

**What type of PR is this?**

/area cicd
<!--
Add one or more of the following kinds:
/kind api-change
Expand All @@ -14,24 +15,6 @@ Add one or more of the following kinds:
/kind regression
/kind upgrade
/kind denpendency

Also with one of following areas:
/area auth
/area resource
/area apps
/area cicd
/area cargo
/area insight
/area loadbalancer
/area network
/area cli
/area data
/area serving
/area workflow
/area tutorial
/area solution
/area training
/area model
-->

**What this PR does / why we need it**:
Expand Down
6 changes: 3 additions & 3 deletions pkg/meta/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const (
// AnnotationDescription is the annotation key used to describe resources
AnnotationDescription = "cyclone.dev/description"

// AnnotationOwner is the annotation key used to indicate the owner of resources.
AnnotationOwner = "cyclone.dev/owner"

// AnnotationStageName is annotation applied to pod to indicate which stage it related to
AnnotationStageName = "stage.cyclone.dev/name"

Expand All @@ -25,6 +22,9 @@ const (
// AnnotationWorkflowRunSCMEvent is the annotation key used to indicate the SCM event data to trigger workflowruns.
AnnotationWorkflowRunSCMEvent = "workflowrun.cyclone.dev/scm-event"

// AnnotationWorkflowRunPRUpdatedAt is the annotation key used to indicate the time that SCM event gets triggered.
AnnotationWorkflowRunPRUpdatedAt = "workflowrun.cyclone.dev/scm-pr-updated-at"

// AnnotationTenantInfo is the annotation key used for namespace to relate tenant information
AnnotationTenantInfo = "tenant.cyclone.dev/info"

Expand Down
3 changes: 3 additions & 0 deletions pkg/meta/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const (
// LabelWorkflowName is the label key used to indicate the workflow which the resources belongs to
LabelWorkflowName = "workflow.cyclone.dev/name"

// LabelWorkflowRunPRRef is the label key used to indicate the ref of PR that workflowrun belongs to
LabelWorkflowRunPRRef = "workflowrun.cyclone.dev/scm-pr-ref"

// LabelWorkflowRunName is the label key used to indicate the workflowrun which the resources belongs to
LabelWorkflowRunName = "workflowrun.cyclone.dev/name"

Expand Down
4 changes: 4 additions & 0 deletions pkg/server/biz/scm/bitbucket/server/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/caicloud/nirvana/log"

Expand Down Expand Up @@ -107,13 +108,15 @@ func ParseEvent(request *http.Request) *scm.EventData {
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
Branch: payload.PullRequest.ToRef.DisplayID,
CreatedAt: time.Unix(payload.PullRequest.CreatedDate/1000, 0),
}
case PrFromRefUpdated, PrModified:
return &scm.EventData{
Type: scm.PullRequestEventType,
Repo: fmt.Sprintf("%s/%s", strings.ToLower(payload.PullRequest.ToRef.Repository.Project.Key), payload.PullRequest.ToRef.Repository.Slug),
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
CreatedAt: time.Unix(payload.PullRequest.UpdatedDate/1000, 0),
}
case PrCommentAdded:
return &scm.EventData{
Expand All @@ -122,6 +125,7 @@ func ParseEvent(request *http.Request) *scm.EventData {
Ref: fmt.Sprintf(pullRefTemplate, payload.PullRequest.ID),
Comment: payload.Comment.Text,
CommitSHA: payload.PullRequest.FromRef.LatestCommit,
CreatedAt: time.Unix(payload.PullRequest.CreatedDate/1000, 0),
}
default:
log.Warningln("Skip unsupported Bitbucket Server event")
Expand Down
2 changes: 2 additions & 0 deletions pkg/server/biz/scm/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ func ParseEvent(scmCfg *v1alpha1.SCMSource, request *http.Request) *scm.EventDat
Ref: fmt.Sprintf(pullRefTemplate, *event.PullRequest.Number),
CommitSHA: commitSHA,
Branch: *event.PullRequest.Base.Ref,
CreatedAt: event.GetPullRequest().GetUpdatedAt(),
}
case *github.IssueCommentEvent:
if event.Issue.PullRequestLinks == nil {
Expand Down Expand Up @@ -635,6 +636,7 @@ func ParseEvent(scmCfg *v1alpha1.SCMSource, request *http.Request) *scm.EventDat
Ref: fmt.Sprintf(pullRefTemplate, issueNumber),
Comment: *event.Comment.Body,
CommitSHA: commitSHA,
CreatedAt: event.GetComment().GetCreatedAt(),
}
case *github.PushEvent:
if event.After != nil && *event.After == "0000000000000000000000000000000000000000" {
Expand Down
13 changes: 13 additions & 0 deletions pkg/server/biz/scm/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net/http"
"reflect"
"strings"
"time"

"github.com/caicloud/nirvana/log"
"github.com/xanzy/go-gitlab"
Expand Down Expand Up @@ -459,6 +460,16 @@ func parseWebhook(r *http.Request) (payload interface{}, err error) {
return payload, nil
}

const timeLayout = "2006-01-02 15:04:05 MST"

func parseTime(s string) time.Time {
t, err := time.Parse(timeLayout, s)
if err != nil {
log.Warningf("Failed to parse time(%s): %v", s, err)
}
return t
}

// ParseEvent parses data from Gitlab events.
func ParseEvent(request *http.Request) *scm.EventData {
event, err := parseWebhook(request)
Expand Down Expand Up @@ -491,6 +502,7 @@ func ParseEvent(request *http.Request) *scm.EventData {
Ref: fmt.Sprintf(mergeRefTemplate, objectAttributes.Iid, objectAttributes.TargetBranch),
CommitSHA: objectAttributes.LastCommit.ID,
Branch: objectAttributes.TargetBranch,
CreatedAt: parseTime(objectAttributes.UpdatedAt),
}
case *MergeCommentEvent:
if event.MergeRequest == nil {
Expand All @@ -503,6 +515,7 @@ func ParseEvent(request *http.Request) *scm.EventData {
Ref: fmt.Sprintf(mergeRefTemplate, event.MergeRequest.IID, event.MergeRequest.TargetBranch),
Comment: event.ObjectAttributes.Note,
CommitSHA: event.MergeRequest.LastCommit.ID,
CreatedAt: parseTime(event.ObjectAttributes.CreatedAt),
}
case *v3.PushEvent:
if event.After == "0000000000000000000000000000000000000000" {
Expand Down
15 changes: 9 additions & 6 deletions pkg/server/biz/scm/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package scm
import (
"fmt"
"strings"
"time"

"github.com/caicloud/nirvana/log"

Expand Down Expand Up @@ -172,12 +173,14 @@ const (

// EventData represents the data parsed from SCM events.
type EventData struct {
Type EventType
Repo string
Ref string
Branch string
Comment string
CommitSHA string
Type EventType
Repo string
Ref string
Branch string
Comment string
CommitSHA string
// CreatedAt is the time this event gets triggered at.
CreatedAt time.Time
ChangedFiles []string
}

Expand Down