blob: edd1aebb28ac09ccb2a6eb65a68925c4344a4a3f [file] [log] [blame]
Dean Birchd0f9f8c2020-03-26 11:10:33 +00001#!/usr/bin/env python3
2"""
3Posts a comment to Gerrit.
4"""
5
6__copyright__ = """
7/*
8 * Copyright (c) 2020, Arm Limited. All rights reserved.
9 *
10 * SPDX-License-Identifier: BSD-3-Clause
11 *
12 */
13 """
14
15import argparse
16import json
17import os
18import sys
19import requests
20
21
22def submit_comment(base_url, auth, changeset, patchset_revision, comment):
23 post_data = {"message": comment}
24 comment_url = "{}/a/changes/{}/revisions/{}/review".format(
25 base_url, changeset, patchset_revision
26 )
27 headers = {"Content-Type": "application/json; charset=UTF-8"}
28 post = None
29 try:
30 post = requests.post(
31 comment_url, data=json.dumps(post_data), auth=auth, headers=headers,
32 )
33 except requests.exceptions.RequestException as exception:
34 print("Error posting comment to Gerrit.")
35 sys.exit(0)
36 if post.status_code == 200:
37 print("Posted comment to Gerrit successfully.")
38 else:
39 print(
40 "Could not post comment to Gerrit. Error: {} {}".format(
41 post.status_code, post.text
42 )
43 )
44
45
46if __name__ == "__main__":
47 PARSER = argparse.ArgumentParser(description="Submits a comment to a Gerrit change")
48 PARSER.add_argument("--host", help="Gerrit Host", default=os.getenv("GERRIT_HOST"))
49 PARSER.add_argument(
50 "--changeset",
51 help="Changeset in Gerrit to comment on.",
52 default=os.getenv("GERRIT_CHANGE_NUMBER"),
53 )
54 PARSER.add_argument(
55 "--patchset-revision",
56 help="Commit SHA of revision in Gerrit to comment on.",
57 default=os.getenv("GERRIT_PATCHSET_REVISION"),
58 )
59 PARSER.add_argument(
60 "--user", help="Username to authenticate as.", default=os.getenv("GERRIT_USER")
61 )
62 PARSER.add_argument(
63 "--password",
64 help="Password or token to authenticate as. "
65 "Defaults to GERRIT_PASSWORD environment variable.",
66 default=os.getenv("GERRIT_PASSWORD"),
67 )
68 PARSER.add_argument("--protocol", help="Protocol to use.", default="https")
69 PARSER.add_argument("--port", help="Port to use.", default=None)
70 PARSER.add_argument("--comment", help="Comment to send.")
71 ARGS = PARSER.parse_args()
72 submit_comment(
73 "{}://{}{}".format(
74 ARGS.protocol, ARGS.host, ":{}".format(ARGS.port) if ARGS.port else ""
75 ),
76 (ARGS.user, ARGS.password),
77 ARGS.changeset,
78 ARGS.patchset_revision,
79 ARGS.comment,
80 )