Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # -*- coding: utf-8 -*-" |
| 3 | # |
| 4 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
| 8 | """Module containing methods interfacing with gerrit. |
| 9 | |
| 10 | i.e Create new bugfix change tickets, and reading metadata about a specific change. |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 11 | |
| 12 | Example CURL command that creates CL: |
| 13 | curl -b /home/chromeos_patches/.git-credential-cache/cookie \ |
| 14 | --header "Content-Type: application/json" \ |
| 15 | --data \ |
| 16 | '{"project":"chromiumos/third_party/kernel",\ |
| 17 | "subject":"test",\ |
| 18 | "branch":"chromeos-4.19",\ |
| 19 | "topic":"test_topic"}' https://chromium-review.googlesource.com/a/changes/ |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 23 | import json |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 24 | import http |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 25 | import os |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 26 | import requests |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 27 | |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 28 | import common |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 29 | |
| 30 | |
| 31 | def get_auth_cookie(): |
| 32 | """Load cookies in order to authenticate requests with gerrit/googlesource.""" |
| 33 | # This cookie should exist on GCE in order to perform GAIA authenticated requests |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 34 | gerrit_credentials_cookies = http.cookiejar.MozillaCookieJar(common.GIT_COOKIE_PATH, None, None) |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 35 | gerrit_credentials_cookies.load() |
| 36 | return gerrit_credentials_cookies |
| 37 | |
| 38 | def retrieve_and_parse_endpoint(endpoint_url): |
| 39 | """Retrieves Gerrit endpoint response and removes XSSI prefix )]}'""" |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 40 | try: |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 41 | resp = requests.get(endpoint_url, cookies=get_auth_cookie()) |
| 42 | resp.raise_for_status() |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 43 | resp_json = json.loads(resp.text[5:]) |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 44 | except requests.exceptions.HTTPError as e: |
| 45 | raise type(e)('Endpoint %s should have HTTP response 200' % endpoint_url) from e |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 46 | except json.decoder.JSONDecodeError as e: |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 47 | raise ValueError('Response should contain json )]} prefix to prevent XSSI attacks') from e |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 48 | |
| 49 | return resp_json |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 50 | |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 51 | def set_and_parse_endpoint(endpoint_url, payload): |
| 52 | """POST request to gerrit endpoint with specified payload.""" |
| 53 | try: |
| 54 | resp = requests.post(endpoint_url, json=payload, cookies=get_auth_cookie()) |
| 55 | resp.raise_for_status() |
| 56 | resp_json = json.loads(resp.text[5:]) |
| 57 | except requests.exceptions.HTTPError as e: |
| 58 | raise type(e)('Endpoint %s should have HTTP response 200' % endpoint_url) from e |
| 59 | except json.decoder.JSONDecodeError as e: |
| 60 | raise ValueError('Response should contain json )]} prefix to prevent XSSI attacks') from e |
| 61 | |
| 62 | return resp_json |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 63 | |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 64 | |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 65 | def get_commit(changeid): |
| 66 | """Retrieves current commit message for a change. |
| 67 | |
| 68 | May add some additional information to the fix patch for tracking purposes. |
| 69 | i.e attaching a tag |
| 70 | """ |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 71 | get_commit_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes', |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 72 | changeid, 'revisions/current/commit') |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 73 | resp = retrieve_and_parse_endpoint(get_commit_endpoint) |
| 74 | |
| 75 | try: |
| 76 | return resp['message'] |
| 77 | except KeyError as e: |
| 78 | raise type(e)('Gerrit API endpoint to get commit should contain message key') from e |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 79 | |
| 80 | |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 81 | def get_changeid_reviewers(changeid): |
| 82 | """Retrieves list of reviewer emails from gerrit given a chromeos changeid.""" |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 83 | list_reviewers_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes', |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 84 | changeid, 'reviewers') |
| 85 | |
| 86 | resp = retrieve_and_parse_endpoint(list_reviewers_endpoint) |
| 87 | |
| 88 | try: |
| 89 | return [reviewer_resp['email'] for reviewer_resp in resp] |
| 90 | except KeyError as e: |
| 91 | raise type(e)('Gerrit API endpoint to list reviewers should contain key email') from e |
| 92 | |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 93 | def set_reviewers(changeid, reviewer_emails): |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 94 | """Adds reviewers to a Gerrit CL.""" |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 95 | add_reviewer_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes', |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame] | 96 | changeid, 'reviewers') |
| 97 | |
| 98 | for email in reviewer_emails: |
| 99 | payload = {'reviewer': email} |
| 100 | set_and_parse_endpoint(add_reviewer_endpoint, payload) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 101 | |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 102 | def get_change(changeid): |
| 103 | """Retrieves ChangeInfo from gerrit using its changeid""" |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 104 | get_change_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes', |
| 105 | changeid) |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 106 | return retrieve_and_parse_endpoint(get_change_endpoint) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 107 | |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 108 | def set_hashtag(changeid): |
| 109 | """Set hashtag to be autogenerated indicating a robot generated CL.""" |
| 110 | set_hashtag_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes', |
| 111 | changeid, 'hashtags') |
| 112 | hashtag_input_payload = {'add' : ['autogenerated']} |
| 113 | set_and_parse_endpoint(set_hashtag_endpoint, hashtag_input_payload) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 114 | |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 115 | |
| 116 | # TODO(hirthanan) implement in seperate CL |
| 117 | def generate_fix_message(fixer_upstream_message): |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 118 | """Generates new commit message for a fix change. |
| 119 | |
| 120 | Use script ./contrib/from_upstream.py to generate new commit msg |
| 121 | Commit message should include essential information: |
| 122 | i.e: |
| 123 | FROMGIT, FROMLIST, ANDROID, CHROMIUM, etc. |
| 124 | commit message indiciating what is happening |
| 125 | BUG=... |
| 126 | TEST=... |
| 127 | tag for Fixes: <upstream-sha> |
| 128 | """ |
Hirthanan Subenderan | c44a0b3 | 2020-03-11 22:34:39 -0700 | [diff] [blame^] | 129 | print(fixer_upstream_message) |
| 130 | commit_message = '' |
| 131 | return commit_message |
| 132 | |
| 133 | |
| 134 | # Note: Stable patches won't have a fixee_change_id since they come into chromeos as merges |
| 135 | def create_change(fixer_upstream_commit_message, branch, fixee_changeid=None): |
| 136 | """Creates a Patch in gerrit given a ChangeInput object.""" |
| 137 | create_change_endpoint = os.path.join(common.CHROMIUM_REVIEW_BASEURL, 'changes') |
| 138 | |
| 139 | change_input_payload = {'project': common.CHROMEOS_KERNEL_DIR, |
| 140 | 'subject': generate_fix_message(fixer_upstream_commit_message), |
| 141 | 'branch': common.chromeos_branch(branch)} |
| 142 | |
| 143 | resp = set_and_parse_endpoint(create_change_endpoint, change_input_payload) |
| 144 | |
| 145 | fixer_changeid = None |
| 146 | try: |
| 147 | fixer_changeid = resp['_number'] |
| 148 | except KeyError as e: |
| 149 | raise type(e)('Gerrit API endpoint to create CL should contain key: _number') from e |
| 150 | |
| 151 | reviewers = None |
| 152 | if fixee_changeid: |
| 153 | # retrieve reviewers from gerrit for the relevant change |
| 154 | reviewers = get_changeid_reviewers(fixee_changeid) |
| 155 | else: |
| 156 | # TODO(hirthanan): find relevant mailing list/reviewers |
| 157 | # For now we will assign it to a default user like Guenter? |
| 158 | # This is for stable bug fix patches that don't have a direct fixee changeid |
| 159 | # since groups of stable commits get merged as one changeid |
| 160 | reviewers = ['groeck@chromium.org'] |
| 161 | |
| 162 | set_reviewers(fixer_changeid, reviewers) |
| 163 | set_hashtag(fixer_changeid) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 164 | |
| 165 | |
| 166 | def create_gerrit_change(reviewers, commit_msg): |
| 167 | """Uses gerrit api to handle creating gerrit change. |
| 168 | |
| 169 | Determines whether a change for a fix has already been created, |
| 170 | and avoids duplicate creations. |
| 171 | |
| 172 | May add some additional information to the fix patch for tracking purposes. |
| 173 | i.e attaching a tag, |
| 174 | """ |
| 175 | |
| 176 | # Call gerrit api to create new change if neccessary |
| 177 | print('Calling gerrit api', reviewers, commit_msg) |