Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Posts 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 | |
| 15 | import argparse |
| 16 | import json |
| 17 | import os |
| 18 | import sys |
| 19 | import requests |
| 20 | |
| 21 | |
| 22 | def 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 Birch | 58e38a2 | 2020-05-29 16:42:39 +0100 | [diff] [blame] | 32 | sys.exit(0) |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 33 | 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 | |
| 42 | def 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 Birch | 58e38a2 | 2020-05-29 16:42:39 +0100 | [diff] [blame] | 72 | sys.exit(0) |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 73 | 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 Birch | 58e38a2 | 2020-05-29 16:42:39 +0100 | [diff] [blame] | 81 | sys.exit(0) |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | if __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 Mazzucato | 935f9cb | 2025-05-16 17:21:07 +0100 | [diff] [blame^] | 102 | "--verify-name", help="Name to give the job verification message." |
Dean Birch | 62c4f08 | 2020-01-17 16:13:26 +0000 | [diff] [blame] | 103 | ) |
| 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 | ) |