From 7b4b04895c5b82a3476c8eb974cc32931cc85ede Mon Sep 17 00:00:00 2001 From: "Ivana Yovcheva (VMware)" Date: Mon, 2 Apr 2018 00:13:25 +0300 Subject: [PATCH] Add PR branch validation This checks if the branch a pull request is raised from is non-master. Validates, as well, if a pull request is raised against the default branch. Signed-off-by: Ivana Yovcheva (VMware) --- pullRequestHandler.go | 64 ++++++++++++++++++++++++++++++++++++------- types/types.go | 15 ++++++++-- 2 files changed, 66 insertions(+), 13 deletions(-) diff --git a/pullRequestHandler.go b/pullRequestHandler.go index d16bc5a..a9705ed 100644 --- a/pullRequestHandler.go +++ b/pullRequestHandler.go @@ -32,6 +32,8 @@ func handlePullRequest(req types.PullRequestOuter) { client := auth.MakeClient(ctx, token) + doBranchValidation(req, client, ctx) + hasUnsignedCommits, err := hasUnsigned(req, client) if err != nil { @@ -57,16 +59,7 @@ func handlePullRequest(req types.PullRequestOuter) { body := `Thank you for your contribution. I've just checked and your commit doesn't appear to be signed-off. That's something we need before your Pull Request can be merged. Please see our [contributing guide](` + link + `).` - comment := &github.IssueComment{ - Body: &body, - } - - comment, resp, err := client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment) - if err != nil { - log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, resp.Limit, resp.Remaining) - log.Fatal(err) - } - fmt.Println(comment, resp.Rate) + comment(req, body, client, ctx) } } else { fmt.Println("Things look OK right now.") @@ -126,6 +119,57 @@ func hasUnsigned(req types.PullRequestOuter, client *github.Client) (bool, error return hasUnsigned, err } +func doBranchValidation(req types.PullRequestOuter, client *github.Client, ctx Context) { + if isMasterHeadBranch(req) || !isAgainstDefaultBranch(req) { + body := `Thank you for your contribution. You have opened a pull request from master branch or against the non-default branch. + Please rename your branch to a non-master and reopen the pull request against the default branch. Thank you! + Closing. ` + comment(req, body, client, ctx) + + closePullRequest(req) + } +} + +func getDefaultBranch(req types.PullRequestOuter) string { + return req.Repository.DefaultBranch +} + +func getBaseBranch(req types.PullRequestOuter) string { + return req.BaseBranch.Name +} + +func getHeadBranch(req types.PullRequestOuter) string { + return req.HeadBranch.Name +} + +func isMasterHeadBranch(req types.PullRequestOuter) bool { + return req.HeadBranch.Name == "master" +} + +func isAgainstDefaultBranch(req types.PullRequestOuter) bool { + return getDefaultBranch(req) == getBaseBranch(req) +} + func isSigned(msg string) bool { return strings.Contains(msg, "Signed-off-by:") } + +/* + * TODO check how to close with go-github or the GitHUb API + */ +func closePullRequest(req types.PullRequestOuter) { + req.PullRequest.Status = "closed" +} + +func comment(req types.PullRequestOuter, body string, client *github.Client, ctx Context) { + comment := &github.IssueComment{ + Body: &body, + } + + comment, resp, err := client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment) + if err != nil { + log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, resp.Limit, resp.Remaining) + log.Fatal(err) + } + fmt.Println(comment, resp.Rate) +} diff --git a/types/types.go b/types/types.go index 3bf70ed..26827c3 100644 --- a/types/types.go +++ b/types/types.go @@ -1,8 +1,14 @@ package types type Repository struct { - Owner Owner `json:"owner"` - Name string `json:"name"` + Owner Owner `json:"owner"` + Name string `json:"name"` + DefaultBranch string `json:"default_branch"` +} + +type Branch struct { + Repository Repository `json:"repository"` + Name string `json:"ref"` } type Owner struct { @@ -11,7 +17,8 @@ type Owner struct { } type PullRequest struct { - Number int `json:"number"` + Number int `json:"number"` + State string `json:"state"` } type InstallationRequest struct { @@ -25,6 +32,8 @@ type ID struct { type PullRequestOuter struct { Repository Repository `json:"repository"` PullRequest PullRequest `json:"pull_request"` + BaseBranch Branch `json:"base"` + HeadBranch Branch `json:"head"` Action string `json:"action"` InstallationRequest }