|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {AuthenticatedGitClient} from '../../../utils/git/authenticated-git-client'; |
| 10 | +import {Log} from '../../../utils/logging'; |
| 11 | +import {getRepoConfigValue, getRepoConfigValueDefinition} from '../get/index'; |
| 12 | + |
| 13 | +export async function setRepoConfigValue( |
| 14 | + key: string, |
| 15 | + value: string, |
| 16 | + git: Promise<AuthenticatedGitClient> | AuthenticatedGitClient = AuthenticatedGitClient.get(), |
| 17 | +) { |
| 18 | + git = await git; |
| 19 | + const currentValue = await getRepoConfigValue(key, git); |
| 20 | + if (currentValue === value) { |
| 21 | + Log.debug( |
| 22 | + 'Skipping update of repository configuration value as it is already set to the provided value', |
| 23 | + ); |
| 24 | + return false; |
| 25 | + } |
| 26 | + const {value_type, allowed_values} = await getRepoConfigValueDefinition(key, git); |
| 27 | + |
| 28 | + if (value_type !== 'single_select') { |
| 29 | + throw Error( |
| 30 | + `Unable to update ${key} as its type is ${value_type}, currently the only supported ` + |
| 31 | + `configuration type is single_select`, |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (!allowed_values!.includes(value)) { |
| 36 | + throw Error( |
| 37 | + `Unable to update ${key}. The value provided must use one of: ${allowed_values!.join(', ')}\n` + |
| 38 | + `But "${value}" was provided as the value`, |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + await git.github.repos.customPropertiesForReposCreateOrUpdateRepositoryValues({ |
| 43 | + owner: git.remoteConfig.owner, |
| 44 | + repo: git.remoteConfig.name, |
| 45 | + properties: [{value, property_name: key}], |
| 46 | + }); |
| 47 | + |
| 48 | + return true; |
| 49 | +} |
0 commit comments