| 1 |
const Definition = require('@npmcli/config/lib/definitions/definition.js') |
| 2 |
const globalDefinitions = require('@npmcli/config/lib/definitions/definitions.js') |
| 3 |
const TrustCommand = require('../../trust-cmd.js') |
| 4 |
const { trustDefinitions } = require('../../trust-cmd.js') |
| 5 |
const path = require('node:path') |
| 6 |
|
| 7 |
class TrustGitLab extends TrustCommand { |
| 8 |
static description = 'Create a trusted relationship between a package and GitLab CI/CD' |
| 9 |
static name = 'gitlab' |
| 10 |
static positionals = 1 // expects at most 1 positional (package name) |
| 11 |
static providerName = 'GitLab CI/CD' |
| 12 |
static providerEntity = 'GitLab project' |
| 13 |
static providerFile = 'GitLab CI/CD Pipeline' |
| 14 |
static providerHostname = 'https://gitlab.com' |
| 15 |
|
| 16 |
// entity means project / repository |
| 17 |
static entityKey = 'project' |
| 18 |
|
| 19 |
static usage = [ |
| 20 |
'[package] --file [--project|--repo|--repository] [--env|--environment] [--allow-publish] [--allow-stage-publish] [-y|--yes]', |
| 21 |
] |
| 22 |
|
| 23 |
static definitions = [ |
| 24 |
new Definition('file', { |
| 25 |
default: null, |
| 26 |
type: String, |
| 27 |
required: true, |
| 28 |
description: 'Name of pipeline file (e.g., .gitlab-ci.yml)', |
| 29 |
}), |
| 30 |
new Definition('project', { |
| 31 |
default: null, |
| 32 |
type: String, |
| 33 |
description: 'Name of the project in the format group/project or group/subgroup/project', |
| 34 |
}), |
| 35 |
new Definition('environment', { |
| 36 |
default: null, |
| 37 |
type: String, |
| 38 |
description: 'CI environment name', |
| 39 |
alias: ['env'], |
| 40 |
}), |
| 41 |
trustDefinitions['allow-publish'], |
| 42 |
trustDefinitions['allow-stage-publish'], |
| 43 |
// globals are alphabetical |
| 44 |
globalDefinitions['dry-run'], |
| 45 |
globalDefinitions.json, |
| 46 |
globalDefinitions.registry, |
| 47 |
globalDefinitions.yes, |
| 48 |
] |
| 49 |
|
| 50 |
getEntityUrl ({ providerHostname, file, entity }) { |
| 51 |
if (file) { |
| 52 |
return new URL(`${entity}/-/blob/HEAD/${file}`, providerHostname).toString() |
| 53 |
} |
| 54 |
return new URL(entity, providerHostname).toString() |
| 55 |
} |
| 56 |
|
| 57 |
validateEntity (entity) { |
| 58 |
if (entity.split('/').length < 2) { |
| 59 |
throw new Error(`${this.constructor.providerEntity} must be specified in the format group/project or group/subgroup/project`) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
validateFile (file) { |
| 64 |
if (file !== path.basename(file)) { |
| 65 |
throw new Error('GitLab CI/CD pipeline file must be just a file not a path') |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
static optionsToBody (options) { |
| 70 |
const { file, project, environment } = options |
| 71 |
const trustConfig = { |
| 72 |
type: 'gitlab', |
| 73 |
claims: { |
| 74 |
project_path: project, |
| 75 |
// this looks off, but this is correct |
| 76 |
/** The ref path to the top-level pipeline definition, for example, gitlab.example.com/my-group/my-project//.gitlab-ci.yml@refs/heads/main. Introduced in GitLab 16.2. This claim is null unless the pipeline definition is located in the same project. */ |
| 77 |
ci_config_ref_uri: { |
| 78 |
file, |
| 79 |
}, |
| 80 |
...(environment) && { environment }, |
| 81 |
}, |
| 82 |
} |
| 83 |
return trustConfig |
| 84 |
} |
| 85 |
|
| 86 |
// Convert API response body to options |
| 87 |
static bodyToOptions (body) { |
| 88 |
const file = body.claims?.ci_config_ref_uri?.file |
| 89 |
const project = body.claims?.project_path |
| 90 |
const environment = body.claims?.environment |
| 91 |
return { |
| 92 |
...(body.id) && { id: body.id }, |
| 93 |
...(body.type) && { type: body.type }, |
| 94 |
...(file) && { file }, |
| 95 |
...(project) && { project }, |
| 96 |
...(environment) && { environment }, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
async exec (positionalArgs, flags) { |
| 101 |
await this.createConfigCommand({ |
| 102 |
positionalArgs, |
| 103 |
flags, |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
module.exports = TrustGitLab |