Skip to content

Commit

Permalink
Batch reasons for not merging.
Browse files Browse the repository at this point in the history
Part of the feedback in coq#95.
  • Loading branch information
Zimmi48 committed Sep 17, 2020
1 parent e1bf69b commit ccaf0c6
Showing 1 changed file with 124 additions and 152 deletions.
276 changes: 124 additions & 152 deletions src/actions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -462,172 +462,144 @@ let coq_bug_minimizer_results_action ~bot_info ~coq_minimizer_repo_token ~key

let merge_pull_request_action ~bot_info ~comment_info =
let pr = comment_info.issue in
if String.equal comment_info.author pr.user then
GitHub_mutations.post_comment ~bot_info
~message:
(f "@%s: You can't merge the PR because you are the author."
comment_info.author)
~id:pr.id
else
match
List.find comment_info.issue.labels ~f:(fun label ->
string_match ~regexp:"needs:.*" label)
with
| Some l ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR cannot be merged because there is still a `%s` \
label."
comment_info.author l)
~id:pr.id
| None -> (
if
not
(List.exists comment_info.issue.labels ~f:(fun label ->
string_match ~regexp:"kind:.*" label))
then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR cannot be merged because there is no `kind:` \
label."
comment_info.author)
~id:pr.id
else if not comment_info.issue.milestoned then
GitHub_mutations.post_comment ~bot_info
~message:
(f "@%s: This PR cannot be merged because no milestone is set."
comment_info.author)
~id:pr.id
else
GitHub_queries.get_pull_request_reviews_refs ~bot_info
~owner:pr.issue.owner ~repo:pr.issue.repo ~number:pr.issue.number
>>= function
| Ok reviews_info -> (
let comment =
List.find reviews_info.last_comments ~f:(fun c ->
String.equal comment_info.id c.id)
in
if (not comment_info.review_comment) && Option.is_none comment
then
let reasons_for_not_merging =
List.filter_opt
[ ( if String.equal comment_info.author pr.user then
Some "you are the author"
else if
List.exists
~f:(String.equal comment_info.author)
comment_info.issue.assignees
then None
else Some "you're not among the assignees" )
; comment_info.issue.labels
|> List.find ~f:(fun label -> string_match ~regexp:"needs:.*" label)
|> Option.map ~f:(fun l -> f "there is still a `%s` label" l)
; ( if
comment_info.issue.labels
|> List.exists ~f:(fun label -> string_match ~regexp:"kind:.*" label)
then None
else Some "there is no `kind:` label" )
; ( if comment_info.issue.milestoned then None
else Some "no milestone is set" ) ]
in
match reasons_for_not_merging with
| _ :: _ ->
let reasons = reasons_for_not_merging |> String.concat ~sep:" and " in
GitHub_mutations.post_comment ~bot_info
~message:
(f "@%s: You can't merge the PR because %s." comment_info.author
reasons)
~id:pr.id
| [] -> (
GitHub_queries.get_pull_request_reviews_refs ~bot_info
~owner:pr.issue.owner ~repo:pr.issue.repo ~number:pr.issue.number
>>= function
| Ok reviews_info -> (
let comment =
List.find reviews_info.last_comments ~f:(fun c ->
String.equal comment_info.id c.id)
in
if (not comment_info.review_comment) && Option.is_none comment then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Could not find merge comment because too many \
comments were posted since."
comment_info.author)
~id:pr.id
else if
(not comment_info.review_comment)
&& (Option.value_exn comment).created_by_email
(* Option.value_exn doesn't raise an exception because comment isn't None at this point*)
then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Merge requests sent over e-mail are not accepted \
because this put less guarantee on the authenticity of the \
author of the request."
comment_info.author)
~id:pr.id
else if not (String.equal reviews_info.baseRef "master") then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR targets branch `%s` instead of `master`. Only \
release managers can merge in release branches. Merging \
with the bot is not supported."
comment_info.author reviews_info.baseRef)
~id:pr.id
else
match reviews_info.review_decision with
| NONE | REVIEW_REQUIRED ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Could not find merge comment because too many \
comments were posted since."
"@%s: You can't merge the PR because it hasn't been \
approved yet."
comment_info.author)
~id:pr.id
else if
(not comment_info.review_comment)
&& (Option.value_exn comment).created_by_email
(* Option.value_exn doesn't raise an exception because comment isn't None at this point*)
then
| CHANGES_REQUESTED ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Merge requests sent over e-mail are not accepted \
because this put less guarantee on the authenticity of \
the author of the request."
"@%s: You can't merge the PR because some changes are \
requested."
comment_info.author)
~id:pr.id
else if not (String.equal reviews_info.baseRef "master") then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR targets branch `%s` instead of `master`. \
Only release managers can merge in release branches. \
Merging with the bot is not supported."
comment_info.author reviews_info.baseRef)
~id:pr.id
else
match reviews_info.review_decision with
| NONE | REVIEW_REQUIRED ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR cannot be merged because it hasn't \
been approved yet."
comment_info.author)
~id:pr.id
| CHANGES_REQUESTED ->
| APPROVED -> (
GitHub_queries.get_team_membership ~bot_info ~org:"coq"
~team:"pushers" ~user:comment_info.author
>>= function
| Ok _ -> (
GitHub_mutations.merge_pull_request ~bot_info ~pr_id:pr.id
~commit_headline:
(f "Merge PR #%d: %s" pr.issue.number
comment_info.issue.title)
~commit_body:
( List.fold_left reviews_info.approved_reviews ~init:""
~f:(fun s r -> s ^ f "Reviewed-by: %s\n" r)
^ List.fold_left reviews_info.comment_reviews ~init:""
~f:(fun s r -> s ^ f "Ack-by: %s\n" r) )
~merge_method:MERGE
>>= fun () ->
match
List.fold_left ~init:[] reviews_info.files
~f:(fun acc f ->
if
string_match ~regexp:"dev/ci/user-overlays/\\(.*\\)"
f
then Str.matched_group 1 f :: acc
else acc)
with
| [] ->
Lwt.return ()
| overlays ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Please take care of the following overlays:\n\
%s"
comment_info.author
(List.fold_left overlays ~init:"" ~f:(fun s o ->
s ^ f "- %s\n" o)))
~id:pr.id )
| Error _ ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: This PR cannot be merged because some changes \
are requested."
"@%s: You can't merge this PR because you're not a \
member of the `@coq/pushers` team."
comment_info.author)
~id:pr.id
| APPROVED -> (
if
not
(List.exists comment_info.issue.assignees
~f:(fun login ->
String.equal login comment_info.author))
then
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: You can't merge the PR because you're not \
among the assignees."
comment_info.author)
~id:pr.id
else
GitHub_queries.get_team_membership ~bot_info ~org:"coq"
~team:"pushers" ~user:comment_info.author
>>= function
| Ok _ -> (
GitHub_mutations.merge_pull_request ~bot_info
~pr_id:pr.id
~commit_headline:
(f "Merge PR #%d: %s" pr.issue.number
comment_info.issue.title)
~commit_body:
( List.fold_left reviews_info.approved_reviews
~init:"" ~f:(fun s r ->
s ^ f "Reviewed-by: %s\n" r)
^ List.fold_left reviews_info.comment_reviews
~init:"" ~f:(fun s r ->
s ^ f "Ack-by: %s\n" r) )
~merge_method:MERGE
>>= fun () ->
match
List.fold_left ~init:[] reviews_info.files
~f:(fun acc f ->
if
string_match
~regexp:"dev/ci/user-overlays/\\(.*\\)" f
then Str.matched_group 1 f :: acc
else acc)
with
| [] ->
Lwt.return ()
| overlays ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Please take care of the following \
overlays:\n\
%s"
comment_info.author
(List.fold_left overlays ~init:""
~f:(fun s o -> s ^ f "- %s\n" o)))
~id:pr.id )
| Error _ ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: You can't merge this PR because you're \
not a member of the `@coq/pushers` team."
comment_info.author)
~id:pr.id ) )
| Error e ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Something unexpected happend: %s\n\
cc @coq/coqbot-maintainers" comment_info.author e)
~id:pr.id )
~id:pr.id ) )
| Error e ->
GitHub_mutations.post_comment ~bot_info
~message:
(f
"@%s: Something unexpected happend: %s\n\
cc @coq/coqbot-maintainers" comment_info.author e)
~id:pr.id )

let update_pr ~bot_info (pr_info : issue_info pull_request_info) ~gitlab_mapping
~github_mapping =
Expand Down

0 comments on commit ccaf0c6

Please sign in to comment.