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
23 fun json_as_a
: Array[nullable Object] do return self.as(Array[nullable Object])
25 fun json_as_map
: Map[String, nullable Object] do return self.as(Map[String, nullable Object])
28 redef class GithubCurl
29 # Get a given pull request (PR)
30 fun getpr
(number
: Int): Map[String, nullable Object]
32 var pr
= get_and_check
("https://api.github.com/repos/privat/nit/pulls/{number}")
33 var prm
= pr
.json_as_map
34 var sha
= prm
["head"].json_as_map
["sha"].to_s
35 var statuses
= get_and_check
("https://api.github.com/repos/privat/nit/statuses/{sha}")
36 prm
["statuses"] = statuses
37 print
"{prm["title"].to_s}: by {prm["user"].json_as_map["login"].to_s} (# {prm["number"].to_s})"
38 print
"\tmergable: {prm["mergeable"].to_s}"
39 var st
= prm
["statuses"].json_as_a
40 if not st
.is_empty
then
41 print
"\tstatus: {st[0].json_as_map["state"].to_s}"
43 print
"\tstatus: not tested"
48 # Get reviewers of a PR
49 fun getrev
(pr
: Map[String, nullable Object]): Array[String]
51 var number
= pr
["number"].as(Int)
52 var user
= pr
["user"].json_as_map
["login"].as(String)
53 var comments
= new Array[nullable Object]
54 comments
.add_all
(get_and_check
("https://api.github.com/repos/privat/nit/issues/{number}/comments").json_as_a
)
55 comments
.add_all
(get_and_check
("https://api.github.com/repos/privat/nit/pulls/{number}/comments").json_as_a
)
56 var logins
= new Array[String]
58 var cm
= c
.json_as_map
59 var l
= cm
["user"].json_as_map
["login"]
61 if l
!= user
and not logins
.has
(l
) then logins
.add
(l
)
63 var res
= new Array[String]
65 var u
= get_and_check
("https://api.github.com/users/{l}").json_as_map
66 if not u
.has_key
("name") then
67 print
"No public name for user {l}"
70 var r
= "{u["name"].to_s} <{u["email"].to_s}>"
79 if "NIT_TESTING".environ
== "true" then exit
0
81 var auth
= get_github_oauth
84 print
"Not github token, please configure one with"
85 print
" git config --add github.oauthtoken MYOAUTHTOKEN"
89 var curl
= new GithubCurl(auth
, "Merge-o-matic (privat/nit)")
91 if args
.length
!= 1 then
92 # Without args, list `ok_will_merge`
93 var x
= curl
.get_and_check
("https://api.github.com/repos/privat/nit/issues?labels=ok_will_merge")
94 for y
in x
.json_as_a
do
95 var number
= y
.json_as_map
["number"].as(Int)
96 var pr
= curl
.getpr
(number
)
99 # With a arg, merge the PR
100 var number
= args
.first
.to_i
101 var pr
= curl
.getpr
(number
)
102 var revs
= curl
.getrev
(pr
)
104 var mergemsg
= new Template
105 mergemsg
.add
"Merge: {pr["title"].to_s}\n\n"
106 mergemsg
.add
"{pr["body"].to_s}\n\n"
107 mergemsg
.add
"Pull-Request: #{pr["number"].to_s}\n"
109 mergemsg
.add
"Reviewed-by: {r}\n"
111 mergemsg
.write_to_file
("mergemsg")
113 var sha
= pr
["head"].json_as_map
["sha"].as(String)
114 if system
("git show -s --pretty=format:%h {sha}") != 0 then
115 print
"Commit {sha} not in local repository; did you fetch github?"
118 if system
("git merge --no-commit {sha}") != 0 then
119 system
("cp mergemsg `git rev-parse --git-dir`/MERGE_MSG")
120 print
"Problem during merge... Let's do the commit manually."
123 system
("git commit -F mergemsg")
124 print
"The merge is made"