Skip to content

Commit

Permalink
Merge d15a144 into 18dba49
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujian7 committed Jun 5, 2018
2 parents 18dba49 + d15a144 commit 9217e89
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/scm/provider/github.go
Expand Up @@ -357,7 +357,7 @@ func convertToGithubEvents(events []scm.EventType) []string {
case scm.PullRequestEventType:
ge = append(ge, "pull_request")
case scm.PullRequestCommentEventType:
ge = append(ge, "pull_request_review_comment")
ge = append(ge, "issue_comment")
case scm.PushEventType:
ge = append(ge, "push")
case scm.TagReleaseEventType:
Expand Down
52 changes: 49 additions & 3 deletions pkg/server/router/webhook.go
Expand Up @@ -99,6 +99,7 @@ func (router *router) handleGithubWebhook(request *restful.Request, response *re
Description: "Triggered by tag release",
Stages: scmTrigger.TagRelease.Stages,
}

log.Info("Triggered by Github release event")
case *github.PullRequestEvent:
// Only handle when the pull request are created.
Expand All @@ -117,8 +118,15 @@ func (router *router) handleGithubWebhook(request *restful.Request, response *re
Description: "Triggered by pull request",
Stages: scmTrigger.PullRequest.Stages,
}

log.Info("Triggered by Github pull request event")
case *github.PullRequestReviewCommentEvent:
case *github.IssueCommentEvent:
if event.Issue.PullRequestLinks == nil {
log.Infof("Only handle when issues type is pull request")
response.WriteHeaderAndEntity(http.StatusOK, "Only handle when issues type is pull request")
return
}

// Only handle when the pull request comments are created.
if *event.Action != "created" {
response.WriteHeaderAndEntity(http.StatusOK, "Only handle when pull request comment is created")
Expand All @@ -143,12 +151,28 @@ func (router *router) handleGithubWebhook(request *restful.Request, response *re

if trigger {
performParams = &api.PipelinePerformParams{
Ref: fmt.Sprintf(githubPullRefTemplate, *event.PullRequest.Number),
Ref: fmt.Sprintf(githubPullRefTemplate, *event.Issue.Number),
Description: "Triggered by pull request comments",
Stages: scmTrigger.PullRequestComment.Stages,
}
log.Info("Triggered by Github pull request review comment event")
}
case *github.PushEvent:
if scmTrigger.Push == nil {
response.WriteHeaderAndEntity(http.StatusOK, "Push trigger is not enabled")
return
}

performParams = &api.PipelinePerformParams{
Ref: fmt.Sprintf(*event.Ref),
Description: "Triggered by push",
Stages: scmTrigger.Push.Stages,
}

log.Info("Triggered by Github push event")
default:
log.Errorf("event type not support.")

}

if performParams != nil {
Expand Down Expand Up @@ -214,6 +238,7 @@ func (router *router) handleGitlabWebhook(request *restful.Request, response *re
Description: "Triggered by tag release",
Stages: scmTrigger.TagRelease.Stages,
}

log.Info("Triggered by Gitlab tag event")
case *gitlab.MergeEvent:
// Only handle when the pull request are created.
Expand All @@ -235,7 +260,13 @@ func (router *router) handleGitlabWebhook(request *restful.Request, response *re
}

log.Info("Triggered by Gitlab merge event")
case *gitlab.MergeCommentEvent:
case *gitlabuitl.MergeCommentEvent:
if event.MergeRequest == nil {
log.Infof("Only handle comments on merge request")
response.WriteHeaderAndEntity(http.StatusOK, "Only handle comments on merge request")
return
}

if scmTrigger.PullRequestComment == nil {
response.WriteHeaderAndEntity(http.StatusOK, "Pull request comment trigger is not enabled")
return
Expand All @@ -259,6 +290,21 @@ func (router *router) handleGitlabWebhook(request *restful.Request, response *re
}
log.Info("Triggered by Gitlab merge comment event")
}
case *gitlab.PushEvent:
if scmTrigger.Push == nil {
response.WriteHeaderAndEntity(http.StatusOK, "Push trigger is not enabled")
return
}

performParams = &api.PipelinePerformParams{
Ref: event.Ref,
Description: "Triggered by push",
Stages: scmTrigger.Push.Stages,
}

log.Info("Triggered by Gitlab push event")
default:
log.Errorf("event type not support.")
}

if performParams != nil {
Expand Down
11 changes: 10 additions & 1 deletion pkg/util/gitlab/gitlab.go
Expand Up @@ -16,18 +16,27 @@ const (
NoteHookEvent = "Note Hook"
MergeRequestHookEvent = "Merge Request Hook"
TagPushHookEvent = "Tag Push Hook"
PushHookEvent = "Push Hook"
)

// ParseWebHook parses the body from webhook requeset.
func ParseWebHook(r *http.Request) (payload interface{}, err error) {
eventType := r.Header.Get(gitlabEventTypeHeader)
switch eventType {
case NoteHookEvent:
payload = &gitlab.MergeCommentEvent{}
//payload = &gitlab.MergeCommentEvent{}
// can not unmarshal request body to gitlab.MergeCommentEvent{}
// due to gitlab.MergeCommentEvent.MergeRequest.CreatedAt's type(*time.Time),
// parsing time "2018-05-31 02:19:38 UTC" as "2006-01-02T15:04:05Z07:00" will fail.
payload = &MergeCommentEvent{}
case MergeRequestHookEvent:
payload = &gitlab.MergeEvent{}
case TagPushHookEvent:
payload = &gitlab.TagEvent{}
case PushHookEvent:
payload = &gitlab.PushEvent{}
default:
return nil, fmt.Errorf("event type %v not support", eventType)
}

body, err := ioutil.ReadAll(r.Body)
Expand Down
106 changes: 106 additions & 0 deletions pkg/util/gitlab/helper.go
@@ -0,0 +1,106 @@
package gitlab

type MergeCommentEvent struct {
ObjectKind string `json:"object_kind"`
//User *User `json:"user"`
ProjectID int `json:"project_id"`
Project struct {
Name string `json:"name"`
Description string `json:"description"`
AvatarURL string `json:"avatar_url"`
GitSSHURL string `json:"git_ssh_url"`
GitHTTPURL string `json:"git_http_url"`
Namespace string `json:"namespace"`
PathWithNamespace string `json:"path_with_namespace"`
DefaultBranch string `json:"default_branch"`
Homepage string `json:"homepage"`
URL string `json:"url"`
SSHURL string `json:"ssh_url"`
HTTPURL string `json:"http_url"`
WebURL string `json:"web_url"`
//VisibilityLevel VisibilityLevelValue `json:"visibility_level"`
} `json:"project"`
//Repository *Repository `json:"repository"`
ObjectAttributes struct {
ID int `json:"id"`
Note string `json:"note"`
NoteableType string `json:"noteable_type"`
AuthorID int `json:"author_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ProjectID int `json:"project_id"`
Attachment string `json:"attachment"`
LineCode string `json:"line_code"`
CommitID string `json:"commit_id"`
NoteableID int `json:"noteable_id"`
System bool `json:"system"`
//StDiff *Diff `json:"st_diff"`
URL string `json:"url"`
} `json:"object_attributes"`
MergeRequest *MergeRequest `json:"merge_request"`
}

type MergeRequest struct {
ID int `json:"id"`
IID int `json:"iid"`
ProjectID int `json:"project_id"`
Title string `json:"title"`
Description string `json:"description"`
WorkInProgress bool `json:"work_in_progress"`
State string `json:"state"`
//CreatedAt *time.Time `json:"created_at"`
//UpdatedAt *time.Time `json:"updated_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
TargetBranch string `json:"target_branch"`
SourceBranch string `json:"source_branch"`
Upvotes int `json:"upvotes"`
Downvotes int `json:"downvotes"`
Author struct {
Name string `json:"name"`
Username string `json:"username"`
ID int `json:"id"`
State string `json:"state"`
AvatarURL string `json:"avatar_url"`
} `json:"author"`
Assignee struct {
Name string `json:"name"`
Username string `json:"username"`
ID int `json:"id"`
State string `json:"state"`
AvatarURL string `json:"avatar_url"`
} `json:"assignee"`
SourceProjectID int `json:"source_project_id"`
TargetProjectID int `json:"target_project_id"`
Labels []string `json:"labels"`
Milestone struct {
ID int `json:"id"`
Iid int `json:"iid"`
ProjectID int `json:"project_id"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
//CreatedAt *time.Time `json:"created_at"`
//UpdatedAt *time.Time `json:"updated_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
DueDate string `json:"due_date"`
} `json:"milestone"`
MergeWhenBuildSucceeds bool `json:"merge_when_build_succeeds"`
MergeStatus string `json:"merge_status"`
Subscribed bool `json:"subscribed"`
UserNotesCount int `json:"user_notes_count"`
SouldRemoveSourceBranch bool `json:"should_remove_source_branch"`
ForceRemoveSourceBranch bool `json:"force_remove_source_branch"`
Changes []struct {
OldPath string `json:"old_path"`
NewPath string `json:"new_path"`
AMode string `json:"a_mode"`
BMode string `json:"b_mode"`
Diff string `json:"diff"`
NewFile bool `json:"new_file"`
RenamedFile bool `json:"renamed_file"`
DeletedFile bool `json:"deleted_file"`
} `json:"changes"`
WebURL string `json:"web_url"`
}

0 comments on commit 9217e89

Please sign in to comment.