-
-
Notifications
You must be signed in to change notification settings - Fork 69
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
Add pr_content_check to features #104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,29 +18,25 @@ import ( | |
"github.com/google/go-github/github" | ||
) | ||
|
||
const ( | ||
prDescriptionRequiredLabel = "invalid" | ||
openedPRAction = "opened" | ||
) | ||
|
||
func HandlePullRequest(req types.PullRequestOuter, contributingURL string, config config.Config) { | ||
ctx := context.Background() | ||
token, tokenErr := getAccessToken(config, req.Installation.ID) | ||
|
||
token := os.Getenv("personal_access_token") | ||
if len(token) == 0 { | ||
|
||
newToken, tokenErr := auth.MakeAccessTokenForInstallation( | ||
config.ApplicationID, | ||
req.Installation.ID, | ||
config.PrivateKey) | ||
|
||
if tokenErr != nil { | ||
log.Fatalln(tokenErr.Error()) | ||
} | ||
|
||
token = newToken | ||
if tokenErr != nil { | ||
fmt.Printf("Error getting installation token: %s\n", tokenErr.Error()) | ||
return | ||
} | ||
|
||
client := factory.MakeClient(ctx, token, config) | ||
|
||
hasUnsignedCommits, err := hasUnsigned(req, client) | ||
|
||
if req.Action == "opened" { | ||
if req.Action == openedPRAction { | ||
if req.PullRequest.FirstTimeContributor() == true { | ||
_, res, assignLabelErr := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, []string{"new-contributor"}) | ||
if assignLabelErr != nil { | ||
|
@@ -101,6 +97,63 @@ That's something we need before your Pull Request can be merged. Please see our | |
} | ||
} | ||
|
||
// VerifyPullRequestDescription checks that the PR has anything in the body. | ||
// If there is no body, a label is added and comment posted to the PR with a link to the contributing guide. | ||
func VerifyPullRequestDescription(req types.PullRequestOuter, contributingURL string, config config.Config) { | ||
ctx := context.Background() | ||
token, tokenErr := getAccessToken(config, req.Installation.ID) | ||
|
||
if tokenErr != nil { | ||
fmt.Printf("Error getting installation token: %s\n", tokenErr.Error()) | ||
return | ||
} | ||
|
||
client := factory.MakeClient(ctx, token, config) | ||
|
||
if req.Action == openedPRAction { | ||
if !hasDescription(req.PullRequest) { | ||
fmt.Printf("Applying label: %s", prDescriptionRequiredLabel) | ||
_, res, assignLabelErr := client.Issues.AddLabelsToIssue(ctx, req.Repository.Owner.Login, req.Repository.Name, req.PullRequest.Number, []string{prDescriptionRequiredLabel}) | ||
if assignLabelErr != nil { | ||
log.Fatalf("%s limit: %d, remaining: %d", assignLabelErr, res.Limit, res.Remaining) | ||
} | ||
|
||
body := `Thank you for your contribution. I've just checked and your Pull Request doesn't appear to have any description. | ||
That's something we need before your Pull Request can be merged. Please see our [contributing guide](` + contributingURL + `).` | ||
|
||
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) | ||
} | ||
} | ||
} | ||
|
||
func getAccessToken(config config.Config, installationID int) (string, error) { | ||
token := os.Getenv("personal_access_token") | ||
if len(token) == 0 { | ||
|
||
installationToken, tokenErr := auth.MakeAccessTokenForInstallation( | ||
config.ApplicationID, | ||
installationID, | ||
config.PrivateKey) | ||
|
||
if tokenErr != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably better to bubble up an error and handle properly than blow up. I know this may have been refactored out, but that now means it's getting eyes on it. My hope is to remove all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't quite sure how the error should be handled. For now, just writing the error to stdout and returning before the token is used to prevent a panic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about returning an error from the function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that returning the error and letting the I think the change to return the error should be in a follow up PR to make that change across the entire file. It would make for a cleaner commit/change log and keep this change set focused on checking the PR description. |
||
return "", tokenErr | ||
} | ||
|
||
token = installationToken | ||
} | ||
|
||
return token, nil | ||
} | ||
|
||
func hasNoDcoLabel(issue *github.Issue) bool { | ||
if issue != nil { | ||
for _, label := range issue.Labels { | ||
|
@@ -143,3 +196,7 @@ func hasUnsigned(req types.PullRequestOuter, client *github.Client) (bool, error | |
func isSigned(msg string) bool { | ||
return strings.Contains(msg, "Signed-off-by:") | ||
} | ||
|
||
func hasDescription(pr types.PullRequest) bool { | ||
return len(strings.TrimSpace(pr.Body)) > 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems consistent with the previous messages we used.
@rgee0 do you have any input on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite have that English "personality", so I'd appreciate input into proper phrasing 😄
I know Derek's personality is one of his strong points!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two comments are very similar. Is there a way we can extract the main part and just update the 'problem detail' when using it?
Thank you for your contribution. I've just checked and <problem detail>. That's something we need before your Pull Request can be merged. Please see our [contributing guide](
+ contributingURL +).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's ok, we could do this in a follow up PR. I think there's more to think about than this one instance.
I may have a solution you can see here: https://play.golang.org/p/FByvDuUR4g2
Not sure where the
struct
would be defined though. I was thinking in a new file within thehandlers
package so that it could be used in other handlers.