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