Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | /* eslint-disable no-unused-vars */ |
| 6 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 7 | const MAX_COMPONENT_PAGE_SIZE = 100; |
| 8 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 9 | /** |
| 10 | * Creates a ComponentDef. |
| 11 | * @param {string} projectName The resource name of the parent project. |
| 12 | * @param {string} value The name of the component |
| 13 | * e.g. "Triage" or "Triage>Security". |
| 14 | * @param {string=} docstring Short description of the ComponentDef. |
| 15 | * @param {Array<string>=} admins Array of User resource names to set as admins. |
| 16 | * @param {Array<string>=} ccs Array of User resources names to set as auto-ccs. |
| 17 | * @param {Array<string>=} labels Array of labels. |
| 18 | * @return {ComponentDef} |
| 19 | */ |
| 20 | function createComponentDef( |
| 21 | projectName, value, docstring, admins, ccs, labels) { |
| 22 | const componentDef = { |
| 23 | 'value': value, |
| 24 | 'docstring': docstring, |
| 25 | }; |
| 26 | if (admins) { |
| 27 | componentDef['admins'] = admins; |
| 28 | } |
| 29 | if (ccs) { |
| 30 | componentDef['ccs'] = ccs; |
| 31 | } |
| 32 | if (labels) { |
| 33 | componentDef['labels'] = labels; |
| 34 | } |
| 35 | const message = { |
| 36 | 'parent': projectName, |
| 37 | 'componentDef': componentDef, |
| 38 | }; |
| 39 | const url = URL + 'monorail.v3.Projects/CreateComponentDef'; |
| 40 | return run_(url, message); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Deletes a ComponentDef. |
| 45 | * @param {string} componentName Resource name of the ComponentDef to delete. |
| 46 | * @return {EmptyProto} |
| 47 | */ |
| 48 | function deleteComponentDef(componentName) { |
| 49 | const message = { |
| 50 | 'name': componentName, |
| 51 | }; |
| 52 | const url = URL + 'monorail.v3.Projects/DeleteComponentDef'; |
| 53 | return run_(url, message); |
| 54 | } |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Lists all ComponentDefs for a project. Automatically traverses through pages |
| 58 | * to get all components. |
| 59 | * @param {string=} projectName Resource name of the project to fetch components for. |
| 60 | * @param {string=} includeDeprecated Where to include deprecated components. |
| 61 | * @return {Array<ComponentDef>} |
| 62 | */ |
| 63 | function listComponentDefs(projectName = 'projects/chromium', includeDeprecated=false) { |
| 64 | const url = URL + 'monorail.v3.Projects/ListComponentDefs'; |
| 65 | let components = []; |
| 66 | |
| 67 | let response; |
| 68 | do { |
| 69 | const message = { |
| 70 | 'parent': projectName, |
| 71 | 'pageSize': MAX_COMPONENT_PAGE_SIZE, |
| 72 | 'pageToken': response ? response.nextPageToken : undefined, |
| 73 | }; |
| 74 | response = run_(url, message); |
| 75 | components = [...components, ...response.componentDefs]; |
| 76 | } while (response.nextPageToken); |
| 77 | |
| 78 | if (!includeDeprecated) { |
| 79 | components = components.filter((c) => c.state === 'ACTIVE'); |
| 80 | } |
| 81 | return components; |
| 82 | } |