e71409fe5e3d34938d16dc566b35569c884e0b84
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Query the Github PR API to perform a merge
18 import github
::github_curl
24 fun json_as_a
: JsonArray do return self.as(JsonArray)
26 fun json_as_map
: JsonObject do return self.as(JsonObject)
29 redef class GithubCurl
30 # Get a given pull request (PR)
31 fun getpr
(repo
: String, number
: Int): nullable JsonObject
33 var ir
= get_and_check
("https://api.github.com/repos/{repo}/issues/{number}")
34 var irm
= ir
.json_as_map
35 if not irm
.has_key
("pull_request") then return null
36 var pr
= get_and_check
("https://api.github.com/repos/{repo}/pulls/{number}")
37 var prm
= pr
.json_as_map
38 var sha
= prm
["head"].json_as_map
["sha"].to_s
39 var statuses
= get_and_check
("https://api.github.com/repos/{repo}/statuses/{sha}")
40 prm
["statuses"] = statuses
41 print
"{prm["title"].to_s}: by {prm["user"].json_as_map["login"].to_s} (# {prm["number"].to_s})"
42 var mergeable
= prm
["mergeable"]
43 if mergeable
!= null then
44 print
"\tmergeable: {mergeable.to_s}"
46 print
"\tmergeable: unknown"
48 var st
= prm
["statuses"].json_as_a
49 if not st
.is_empty
then
50 print
"\tstatus: {st[0].json_as_map["state"].to_s}"
52 print
"\tstatus: not tested"
57 # Get reviewers of a PR
58 fun getrev
(repo
: String, pr
: JsonObject): Array[String]
60 var number
= pr
["number"].as(Int)
61 var user
= pr
["user"].json_as_map
["login"].as(String)
62 var comments
= new Array[nullable Object]
63 comments
.add_all
(get_and_check
("https://api.github.com/repos/{repo}/issues/{number}/comments").json_as_a
)
64 comments
.add_all
(get_and_check
("https://api.github.com/repos/{repo}/pulls/{number}/comments").json_as_a
)
65 var logins
= new Array[String]
67 var cm
= c
.json_as_map
68 var l
= cm
["user"].json_as_map
["login"]
70 if l
!= user
and not logins
.has
(l
) then logins
.add
(l
)
72 var res
= new Array[String]
74 var u
= get_and_check
("https://api.github.com/users/{l}").json_as_map
75 if not u
.has_key
("name") or u
["name"] == null or not u
.has_key
("email")or u
["email"] == null then
76 print
"No public name/email for user {l}"
79 var r
= "{u["name"].to_s} <{u["email"].to_s}>"
88 if "NIT_TESTING".environ
== "true" then exit
0
90 var opt_repo
= new OptionString("Repository (e.g. nitlang/nit)", "-r", "--repo")
91 var opt_auth
= new OptionString("OAuth token", "--auth")
92 var opt_query
= new OptionString("Query to get issues (e.g. label=ok_will_merge)", "-q", "--query")
93 var opt_keepgoing
= new OptionBool("Skip merge conflicts", "-k", "--keep-going")
94 var opt_all
= new OptionBool("Merge all", "-a", "--all")
95 var opts
= new OptionContext
96 opts
.add_option
(opt_repo
, opt_auth
, opt_query
, opt_all
, opt_keepgoing
)
101 var auth
= opt_auth
.value
or else ""
102 if auth
== "" then auth
= get_github_oauth
104 print
"Warning: no github oauth token, you can configure one with"
105 print
" git config --add github.oauthtoken MYOAUTHTOKEN"
108 var repo
= opt_repo
.value
or else "nitlang/nit"
110 var query
= opt_query
.value
or else "labels=ok_will_merge"
112 var curl
= new GithubCurl(auth
, "Merge-o-matic (nitlang/nit)")
114 if args
.is_empty
then
115 # Without args, list `ok_will_merge`
116 var x
= curl
.get_and_check
("https://api.github.com/repos/{repo}/issues?{query}")
117 var list
= new Array[String]
118 for y
in x
.json_as_a
do
119 var number
= y
.json_as_map
["number"].as(Int)
120 var pr
= curl
.getpr
(repo
, number
)
121 if pr
== null then continue
125 if not opt_all
.value
then return
130 # With a arg, merge the PR
131 var number
= arg
.to_i
132 var pr
= curl
.getpr
(repo
, number
)
134 print
"Not a PR: {number}"
137 var revs
= curl
.getrev
(repo
, pr
)
139 var mergemsg
= new Template
140 mergemsg
.add
"Merge: {pr["title"].to_s}\n\n"
141 mergemsg
.add
"{pr["body"].to_s}\n\n"
142 mergemsg
.add
"Pull-Request: #{pr["number"].to_s}\n"
144 mergemsg
.add
"Reviewed-by: {r}\n"
146 mergemsg
.write_to_file
("mergemsg")
148 var sha
= pr
["head"].json_as_map
["sha"].as(String)
149 if system
("git show -s --pretty=format:%h {sha}") != 0 then
150 print
"Commit {sha} not in local repository; did you fetch github?"
153 if system
("git merge-base --is-ancestor {sha} HEAD") == 0 then
154 print
"Is already merged."
157 if system
("git merge --no-ff --no-commit {sha}") != 0 then
158 if opt_keepgoing
.value
then
159 system
("git reset --merge")
162 system
("cp mergemsg `git rev-parse --git-dir`/MERGE_MSG")
163 print
"Problem during merge... Let's do the commit manually."
166 system
("git commit -F mergemsg")
167 print
"The merge is made"
168 mergemsg
.write_to
(stdout
)