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 | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 25 | import requests |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame^] | 26 | import os |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 27 | |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 28 | from common import CHROMIUM_REVIEW_BASEURL, GIT_COOKIE_PATH |
| 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 |
| 34 | gerrit_credentials_cookies = http.cookiejar.MozillaCookieJar(GIT_COOKIE_PATH, None, None) |
| 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 | |
| 64 | def get_commit(changeid): |
| 65 | """Retrieves current commit message for a change. |
| 66 | |
| 67 | May add some additional information to the fix patch for tracking purposes. |
| 68 | i.e attaching a tag |
| 69 | """ |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame^] | 70 | get_commit_endpoint = os.path.join(CHROMIUM_REVIEW_BASEURL, 'changes', |
| 71 | changeid, 'revisions/current/commit') |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 72 | return retrieve_and_parse_endpoint(get_commit_endpoint) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 73 | |
| 74 | |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame^] | 75 | def get_changeid_reviewers(changeid): |
| 76 | """Retrieves list of reviewer emails from gerrit given a chromeos changeid.""" |
| 77 | list_reviewers_endpoint = os.path.join(CHROMIUM_REVIEW_BASEURL, 'changes', |
| 78 | changeid, 'reviewers') |
| 79 | |
| 80 | resp = retrieve_and_parse_endpoint(list_reviewers_endpoint) |
| 81 | |
| 82 | try: |
| 83 | return [reviewer_resp['email'] for reviewer_resp in resp] |
| 84 | except KeyError as e: |
| 85 | raise type(e)('Gerrit API endpoint to list reviewers should contain key email') from e |
| 86 | |
| 87 | def set_changeid_reviewers(changeid, reviewer_emails): |
| 88 | """Adds reviewers to a Gerrit CL.""" |
| 89 | add_reviewer_endpoint = os.path.join(CHROMIUM_REVIEW_BASEURL, 'changes', |
| 90 | changeid, 'reviewers') |
| 91 | |
| 92 | for email in reviewer_emails: |
| 93 | payload = {'reviewer': email} |
| 94 | set_and_parse_endpoint(add_reviewer_endpoint, payload) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 95 | |
| 96 | |
| 97 | def get_change(changeid): |
| 98 | """Retrieves ChangeInfo from gerrit using its changeid""" |
Hirthanan Subenderan | 3f02911 | 2020-03-11 12:33:05 -0700 | [diff] [blame^] | 99 | get_change_endpoint = os.path.join(CHROMIUM_REVIEW_BASEURL, 'changes', changeid) |
Hirthanan Subenderan | 4036800 | 2020-03-10 15:36:48 -0700 | [diff] [blame] | 100 | return retrieve_and_parse_endpoint(get_change_endpoint) |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def generate_fix_commit_message(old_changeid): |
| 104 | """Generates new commit message for a fix change. |
| 105 | |
| 106 | Use script ./contrib/from_upstream.py to generate new commit msg |
| 107 | Commit message should include essential information: |
| 108 | i.e: |
| 109 | FROMGIT, FROMLIST, ANDROID, CHROMIUM, etc. |
| 110 | commit message indiciating what is happening |
| 111 | BUG=... |
| 112 | TEST=... |
| 113 | tag for Fixes: <upstream-sha> |
| 114 | """ |
| 115 | old_commit_msg = get_commit(old_changeid) |
| 116 | print(old_commit_msg) |
| 117 | |
| 118 | |
| 119 | def create_gerrit_change(reviewers, commit_msg): |
| 120 | """Uses gerrit api to handle creating gerrit change. |
| 121 | |
| 122 | Determines whether a change for a fix has already been created, |
| 123 | and avoids duplicate creations. |
| 124 | |
| 125 | May add some additional information to the fix patch for tracking purposes. |
| 126 | i.e attaching a tag, |
| 127 | """ |
| 128 | |
| 129 | # Call gerrit api to create new change if neccessary |
| 130 | print('Calling gerrit api', reviewers, commit_msg) |