Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Constants and util methods to interact with skylab inventory repo.""" |
| 6 | |
| 7 | import logging |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 8 | import re |
| 9 | |
| 10 | import common |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 11 | |
| 12 | from autotest_lib.client.common_lib import revision_control |
Mike Frysinger | 714c5b0 | 2020-09-04 23:22:54 -0400 | [diff] [blame] | 13 | from autotest_lib.utils.frozen_chromite.lib import gob_util |
Ningning Xia | 9c188b9 | 2018-04-27 15:34:23 -0700 | [diff] [blame] | 14 | |
| 15 | try: |
| 16 | from skylab_inventory import text_manager |
| 17 | except ImportError: |
| 18 | pass |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 19 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 20 | |
| 21 | INTERNAL_GERRIT_HOST = 'chrome-internal-review.googlesource.com' |
| 22 | INTERNAL_GERRIT_HOST_URL = 'https://%s' % INTERNAL_GERRIT_HOST |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 23 | # The git url of the internal skylab_inventory |
| 24 | INTERNAL_INVENTORY_REPO_URL = ('https://chrome-internal.googlesource.com/' |
| 25 | 'chromeos/infra_internal/skylab_inventory.git') |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 26 | INTERNAL_INVENTORY_CHANGE_PATTERN = ( |
Prathmesh Prabhu | c44db20 | 2018-06-22 14:12:17 -0700 | [diff] [blame] | 27 | r'https://chrome-internal-review.googlesource.com/c/chromeos/' |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 28 | 'infra_internal/skylab_inventory/\\+/([0-9]*)') |
Ningning Xia | ea02ab1 | 2018-05-11 15:08:57 -0700 | [diff] [blame] | 29 | MSG_INVALID_IN_SKYLAB = 'This is currently not supported with --skylab.' |
Allen Li | 75e3d34 | 2018-08-06 19:04:31 +0000 | [diff] [blame] | 30 | MSG_ONLY_VALID_IN_SKYLAB = 'This only applies to actions on skylab inventory.' |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 31 | |
| 32 | |
Ningning Xia | 9c188b9 | 2018-04-27 15:34:23 -0700 | [diff] [blame] | 33 | class SkylabInventoryNotImported(Exception): |
| 34 | """skylab_inventory is not imported.""" |
| 35 | |
| 36 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 37 | class InventoryRepoChangeNotFound(Exception): |
| 38 | """Error raised when no inventory repo change number is found.""" |
| 39 | |
| 40 | |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 41 | class InventoryRepoDirNotClean(Exception): |
| 42 | """Error raised when the given inventory_repo_dir contains local changes.""" |
| 43 | |
| 44 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 45 | def get_cl_url(change_number): |
| 46 | return INTERNAL_GERRIT_HOST_URL + '/' + str(change_number) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 47 | |
| 48 | |
Ningning Xia | ef35cb5 | 2018-05-04 17:58:20 -0700 | [diff] [blame] | 49 | def get_cl_message(change_number): |
| 50 | return ('Please submit the CL at %s to make the change effective.' % |
| 51 | get_cl_url(change_number)) |
| 52 | |
| 53 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 54 | def construct_commit_message(subject, bug=None, test=None): |
| 55 | """Construct commit message for skylab inventory repo commit. |
| 56 | |
| 57 | @param subject: Commit message subject. |
| 58 | @param bug: Bug number of the commit. |
| 59 | @param test: Tests of the commit. |
| 60 | |
| 61 | @return: A commit message string. |
| 62 | """ |
| 63 | return '\n'.join([subject, '', 'BUG=%s' % bug, 'TEST=%s' % test]) |
| 64 | |
| 65 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 66 | def extract_inventory_change(output): |
| 67 | """Extract the change number from the output. |
| 68 | |
| 69 | @param output: The git command output containing the change gerrit url. |
| 70 | |
| 71 | @return: The change number (int) of the inventory change. |
| 72 | """ |
| 73 | m = re.search(INTERNAL_INVENTORY_CHANGE_PATTERN, output) |
| 74 | |
| 75 | if not m: |
Prathmesh Prabhu | c44db20 | 2018-06-22 14:12:17 -0700 | [diff] [blame] | 76 | raise InventoryRepoChangeNotFound( |
| 77 | 'Could not extract CL number from "%r"' % output) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 78 | |
| 79 | return int(m.group(1)) |
| 80 | |
| 81 | |
| 82 | def submit_inventory_change(change_number): |
| 83 | """Set review labels and submit the inventory change. |
| 84 | |
| 85 | @param change_number: The change number (int) of the inventory change. |
| 86 | """ |
| 87 | logging.info('Setting review labels for %s.', |
| 88 | get_cl_url(change_number)) |
| 89 | gob_util.SetReview( |
| 90 | INTERNAL_GERRIT_HOST, |
| 91 | change=change_number, |
| 92 | labels={'Code-Review': 2, 'Verified': 1}, |
| 93 | msg='Set TBR by "atest --skylab"', |
| 94 | notify='OWNER') |
| 95 | |
| 96 | logging.info('Submitting the change.') |
| 97 | gob_util.SubmitChange( |
| 98 | INTERNAL_GERRIT_HOST, |
| 99 | change=change_number) |
| 100 | |
| 101 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 102 | class InventoryRepo(object): |
| 103 | """Class to present a inventory repository.""" |
| 104 | |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 105 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 106 | def __init__(self, inventory_repo_dir): |
| 107 | self.inventory_repo_dir = inventory_repo_dir |
| 108 | self.git_repo = None |
| 109 | |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 110 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 111 | def initialize(self): |
| 112 | """Initialize inventory repo at the given dir.""" |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 113 | self.git_repo = revision_control.GitRepo( |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 114 | self.inventory_repo_dir, |
| 115 | giturl=INTERNAL_INVENTORY_REPO_URL, |
| 116 | abs_work_tree=self.inventory_repo_dir) |
| 117 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 118 | if self.git_repo.is_repo_initialized(): |
Ningning Xia | d23cf2f | 2018-06-04 17:50:35 -0700 | [diff] [blame] | 119 | if self.git_repo.status(): |
| 120 | raise InventoryRepoDirNotClean( |
| 121 | 'The inventory_repo_dir "%s" contains uncommitted ' |
| 122 | 'changes. Please clean up the local repo directory or ' |
| 123 | 'use another clean directory.' % self.inventory_repo_dir) |
| 124 | |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 125 | logging.info('Inventory repo was already initialized, start ' |
| 126 | 'pulling.') |
Derek Beckett | 57c77c0 | 2021-05-21 11:25:11 -0700 | [diff] [blame] | 127 | self.git_repo.checkout('main') |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 128 | self.git_repo.pull() |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 129 | else: |
| 130 | logging.info('No inventory repo was found, start cloning.') |
Prathmesh Prabhu | af1bd7b | 2018-06-22 14:11:51 -0700 | [diff] [blame] | 131 | self.git_repo.clone(shallow=True) |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 132 | |
| 133 | |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 134 | def get_data_dir(self, data_subdir='skylab'): |
Ningning Xia | 84190b8 | 2018-04-16 15:01:40 -0700 | [diff] [blame] | 135 | """Get path to the data dir.""" |
Ningning Xia | e871405 | 2018-04-30 18:58:54 -0700 | [diff] [blame] | 136 | return text_manager.get_data_dir(self.inventory_repo_dir, data_subdir) |
| 137 | |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 138 | |
| 139 | def upload_change(self, commit_message, draft=False, dryrun=False, |
| 140 | submit=False): |
| 141 | """Commit and upload the change to gerrit. |
| 142 | |
| 143 | @param commit_message: Commit message of the CL to upload. |
| 144 | @param draft: Boolean indicating whether to upload the CL as a draft. |
| 145 | @param dryrun: Boolean indicating whether to run upload as a dryrun. |
| 146 | @param submit: Boolean indicating whether to submit the CL directly. |
| 147 | |
| 148 | @return: Change number (int) of the CL if it's uploaded to Gerrit. |
| 149 | """ |
| 150 | self.git_repo.commit(commit_message) |
| 151 | |
Ningning Xia | ee3e3a9 | 2018-05-22 17:38:51 -0700 | [diff] [blame] | 152 | remote = self.git_repo.remote() |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 153 | output = self.git_repo.upload_cl( |
Derek Beckett | 57c77c0 | 2021-05-21 11:25:11 -0700 | [diff] [blame] | 154 | remote, 'main', draft=draft, dryrun=dryrun) |
Ningning Xia | a043aad | 2018-04-23 15:07:09 -0700 | [diff] [blame] | 155 | |
| 156 | if not dryrun: |
| 157 | change_number = extract_inventory_change(output) |
| 158 | |
| 159 | if submit: |
| 160 | submit_inventory_change(change_number) |
| 161 | |
| 162 | return change_number |