Skip to content

Commit

Permalink
Update PR branch validation to apply labels,not close PR
Browse files Browse the repository at this point in the history
This updates pull requests branch validation to apply labes to pull
requests: "attention/target-branch" if the target branch is not the
default and  "attention/source-branch" if the source branch is named
"master". The source branch validation leaves a warning comment as
well, suggesting  to raise a new pull request from a named branch.

Signed-off-by: Ivana Yovcheva <[email protected]>
  • Loading branch information
ivanayov committed Apr 5, 2018
1 parent 60d85be commit 9760461
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
50 changes: 21 additions & 29 deletions pullRequestHandler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"bytes"
"context"
"fmt"
"strings"
Expand Down Expand Up @@ -31,9 +30,8 @@ func handlePullRequest(req types.PullRequestOuter) {

client := auth.MakeClient(ctx, token)

if !passBranchValidation(ctx, req, client) {
return
}
headBranchValidation(ctx, req, client)
baseBranchValidation(ctx, req, client)

hasUnsignedCommits, err := hasUnsigned(req, client)

Expand Down Expand Up @@ -129,21 +127,28 @@ func hasUnsigned(req types.PullRequestOuter, client *github.Client) (bool, error
return hasUnsigned, err
}

func passBranchValidation(ctx context.Context, req types.PullRequestOuter, client *github.Client) bool {
if !isValidHeadAndBaseBranch(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. `
func headBranchValidation(ctx context.Context, req types.PullRequestOuter, client *github.Client) bool {
if isMasterHeadBranch(req) {
body := `Thank you for your contribution. It appears that you are submitting changes directly from your master branch,
we encourage you to raise a new pull request from a named branch i.e. git checkout -b my_feature. `

comment := &github.IssueComment{
Body: &body,
}

client.Issues.CreateComment(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, comment)

closePullRequest(req)
applyLabel(ctx, "attention/source-branch", req, client)
}
return !isMasterHeadBranch(req)
}

func baseBranchValidation(ctx context.Context, req types.PullRequestOuter, client *github.Client) bool {
if !isAgainstDefaultBranch(req) {
applyLabel(ctx, "attention/target-branch", req, client)
}
return isValidHeadAndBaseBranch(req)

return isAgainstDefaultBranch(req)
}

func isValidHeadAndBaseBranch(req types.PullRequestOuter) bool {
Expand Down Expand Up @@ -174,23 +179,10 @@ func isSigned(msg string) bool {
return strings.Contains(msg, "Signed-off-by:")
}

func closePullRequest(req types.PullRequestOuter) (string, error) {
var buffer bytes.Buffer

newState, validTransition := checkTransition(closeConstant, req.PullRequest.State)

if !validTransition {
buffer.WriteString(fmt.Sprintf("Cannot close pull request with a %s state", req.PullRequest.State))
return buffer.String(), nil
}

input := &github.PullRequest{State: &newState}

client, ctx := makeClient(req.Installation.ID)

_, _, err := client.PullRequests.Edit(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, input)
if err != nil {
return buffer.String(), err
func applyLabel(ctx context.Context, label string, req types.PullRequestOuter, client *github.Client) {
fmt.Println("Applying label")
_, res, assignLabelErr := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, []string{label})
if assignLabelErr != nil {
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, res.Limit, res.Remaining)
}
return buffer.String(), nil
}
3 changes: 1 addition & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type Owner struct {
}

type PullRequest struct {
Number int `json:"number"`
State string `json:"state"`
Number int `json:"number"`
}

type InstallationRequest struct {
Expand Down

0 comments on commit 9760461

Please sign in to comment.