blob: d58f434395f5a91aa7cd253605f831ab6112339e [file] [log] [blame]
Dean Birch62c4f082020-01-17 16:13:26 +00001#!/usr/bin/env python3
2"""
3Posts a verification to Gerrit verify-status plugin.
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 check_plugins(base_url, auth):
23 """
24 Checks if the verify-status plugin is installed
25 """
26 plugin_url = "{}/a/plugins/".format(base_url)
27 headers = {"Content-Type": "application/json; charset=UTF-8"}
28 try:
29 request = requests.get(plugin_url, auth=auth, headers=headers)
30 except requests.exceptions.RequestException as exception:
31 print("Error checking plugins: {}".format(str(exception)), file=sys.stderr)
Dean Birch58e38a22020-05-29 16:42:39 +010032 sys.exit(0)
Dean Birch62c4f082020-01-17 16:13:26 +000033 if request.status_code != 200:
34 print("Could not check if verify-status plugin is installed")
35 return
36 json_plugins = json.loads(request.text.replace(")]}'",""))
37 if "verify-status" not in json_plugins:
38 print("verify-status plugin not installed.")
39 sys.exit(0)
40
41
42def submit_verification(base_url, auth, changeset, patchset_revision, verify_details):
43 """
44 Handles the actual post request.
45 """
46 check_plugins(base_url, auth)
47 post_data = {
48 "verifications": {
49 verify_details["verify_name"]: {
50 "url": verify_details["job_url"],
51 "value": verify_details["value"],
52 "abstain": verify_details["abstain"],
53 "reporter": verify_details["reporter"],
54 "comment": verify_details["comment"],
55 "category": verify_details["category"],
56 "duration": verify_details["duration"],
57 }
58 }
59 }
60 submit_url = "{}/a/changes/{}/revisions/{}/verify-status~verifications".format(
61 base_url, changeset, patchset_revision
62 )
63 headers = {"Content-Type": "application/json; charset=UTF-8"}
64 post = None
65 try:
66 post = requests.post(
67 submit_url, data=json.dumps(post_data), auth=auth, headers=headers,
68 )
69 except requests.exceptions.RequestException as exception:
70 print("Error posting to verify-status:", file=sys.stderr)
71 print(str(exception), file=sys.stderr)
Dean Birch58e38a22020-05-29 16:42:39 +010072 sys.exit(0)
Dean Birch62c4f082020-01-17 16:13:26 +000073 if post.status_code == 204:
74 print("Gerrit verify-status posted successfully.")
75 else:
76 print(
77 "Error posting to verify-status: {}".format(post.status_code),
78 file=sys.stderr,
79 )
80 print(post.text, file=sys.stderr)
Dean Birch58e38a22020-05-29 16:42:39 +010081 sys.exit(0)
Dean Birch62c4f082020-01-17 16:13:26 +000082
83
84if __name__ == "__main__":
85 PARSER = argparse.ArgumentParser(
86 description="Submits a job verification to verify-status plugin of Gerrit"
87 )
88 PARSER.add_argument("--host", help="Gerrit Host", default=os.getenv("GERRIT_HOST"))
89 PARSER.add_argument("--job-url", help="Job URL.", default=os.getenv("BUILD_URL"))
90 PARSER.add_argument("--value", help="Verification value.")
91 PARSER.add_argument(
92 "--changeset",
93 help="Changeset in Gerrit to verify.",
94 default=os.getenv("GERRIT_CHANGE_NUMBER"),
95 )
96 PARSER.add_argument(
97 "--patchset-revision",
98 help="Commit SHA of revision in Gerrit to verify.",
99 default=os.getenv("GERRIT_PATCHSET_REVISION"),
100 )
101 PARSER.add_argument(
Nicola Mazzucato935f9cb2025-05-16 17:21:07 +0100102 "--verify-name", help="Name to give the job verification message."
Dean Birch62c4f082020-01-17 16:13:26 +0000103 )
104 PARSER.add_argument(
105 "--user", help="Username to authenticate as.", default=os.getenv("VERIFY_USER")
106 )
107 PARSER.add_argument(
108 "--password",
109 help="Password or token to authenticate as. "
110 "Defaults to VERIFY_PASSWORD environment variable.",
111 default=os.getenv("VERIFY_PASSWORD"),
112 )
113 PARSER.add_argument(
114 "--abstain",
115 help="Whether this should affect the final voting value.",
116 action="store_true",
117 )
118 PARSER.add_argument("--reporter", help="Metadata for reporter.", default="")
119 PARSER.add_argument("--comment", help="Metadata for comment.", default="")
120 PARSER.add_argument("--category", help="Metadata for category.", default="")
121 PARSER.add_argument("--duration", help="Duration of the job.", default="")
122 PARSER.add_argument("--protocol", help="Protocol to use.", default="https")
123 PARSER.add_argument("--port", help="Port to use.", default=None)
124 ARGS = PARSER.parse_args()
125 submit_verification(
126 "{}://{}{}".format(ARGS.protocol, ARGS.host, ":{}".format(ARGS.port) if ARGS.port else ""),
127 (ARGS.user, ARGS.password),
128 ARGS.changeset,
129 ARGS.patchset_revision,
130 {
131 "verify_name": ARGS.verify_name,
132 "job_url": ARGS.job_url,
133 "value": int(ARGS.value),
134 "abstain": bool(ARGS.abstain),
135 "reporter": ARGS.reporter,
136 "comment": ARGS.comment,
137 "category": ARGS.category,
138 "duration": ARGS.duration,
139 },
140 )