##// END OF EJS Templates
notify-script: allow to comment also on single commit
marcink -
r353:d5c4e7ee default
parent child Browse files
Show More
@@ -1,78 +1,91 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import argparse
21 import argparse
22
22
23 from rhodecode_api import RhodeCodeAPI
23 from rhodecode_api import RhodeCodeAPI
24
24
25
25
26 def add_api_pr_args(parser):
26 def add_api_pr_args(parser):
27 parser.add_argument("--url", help="Rhodecode host url", required=True)
27 parser.add_argument("--token", help="Rhodecode user token", required=True)
28 parser.add_argument("--token", help="Rhodecode user token", required=True)
28 parser.add_argument("--url", help="Rhodecode host url", required=True)
29 parser.add_argument("--repoid", help="Repo id", required=True)
29 parser.add_argument("--repoid", help="Repo id", required=True)
30 parser.add_argument("--prid", help="Pull request id", required=True)
31 parser.add_argument("--message", help="Comment to add", required=True)
30 parser.add_argument("--message", help="Comment to add", required=True)
32
33 parser.add_argument("--status", help="Status to set (approved|rejected)")
31 parser.add_argument("--status", help="Status to set (approved|rejected)")
34 parser.add_argument(
32 parser.add_argument(
33 "--prid",
34 help="(Optional) Pull request id")
35 parser.add_argument(
35 "--commit-id",
36 "--commit-id",
36 help="Expected commit ID for source repo")
37 help="(Optional) Expected commit ID for source repo")
37
38
38
39
39 def get_pr_head(args):
40 def get_pr_head(args):
40 rc = RhodeCodeAPI(args.url, args.token)
41 rc = RhodeCodeAPI(args.url, args.token)
41 pr_info = rc.get_pull_request(
42 pr_info = rc.get_pull_request(
42 repoid=args.repoid,
43 repoid=args.repoid,
43 pullrequestid=args.prid
44 pullrequestid=args.prid
44 )
45 )
45 if pr_info['error']:
46 if pr_info['error']:
46 return None
47 return None
47
48
48 return pr_info['result']['source']['reference']['commit_id']
49 return pr_info['result']['source']['reference']['commit_id']
49
50
50
51
51 def pr_has_same_head(args):
52 def pr_has_same_head(args):
52 head_id = get_pr_head(args)
53 head_id = get_pr_head(args)
53 return head_id == args.commit_id
54 return head_id == args.commit_id
54
55
55
56
56 if __name__ == "__main__":
57 if __name__ == "__main__":
57 parser = argparse.ArgumentParser()
58 parser = argparse.ArgumentParser()
58 add_api_pr_args(parser)
59 add_api_pr_args(parser)
59 args = parser.parse_args()
60 args = parser.parse_args()
60 status = args.status
61 status = args.status
61
62
62 if args.commit_id and status:
63 rc = RhodeCodeAPI(args.url, args.token)
63 if not pr_has_same_head(args):
64
64 # PR updated, don't set status
65 if not (args.prid or args.commit_id):
65 status = None
66 raise AttributeError('You must specify --prid or --commit-id')
67
68 if args.prid:
69 if args.commit_id and status:
70 if not pr_has_same_head(args):
71 # PR updated, don't set status
72 status = None
66
73
67 rc = RhodeCodeAPI(args.url, args.token)
74 resp = rc.comment_pull_request(
68 resp = rc.comment_pull_request(
75 repoid=args.repoid,
69 repoid=args.repoid,
76 pullrequestid=int(args.prid),
70 pullrequestid=int(args.prid),
77 message=args.message,
71 message=args.message,
78 status=status
72 status=status
79 )
73 )
80 elif args.commit_id:
81 resp = rc.comment_commit(
82 repoid=args.repoid,
83 commit_id=args.commit_id,
84 message=args.message,
85 status=status,
86 )
74
87
75 if resp['error']:
88 if resp['error']:
76 raise Exception(resp['error'])
89 raise Exception(resp['error'])
77 else:
90 else:
78 print resp
91 print resp
General Comments 0
You need to be logged in to leave comments. Login now