Debian Codegraph @

lib/commands/trust/circleci.js

raw
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 { validateUUID } = require('../../utils/validate-uuid.js')
6
7 class TrustCircleCI extends TrustCommand {
8 static description = 'Create a trusted relationship between a package and CircleCI'
9 static name = 'circleci'
10 static positionals = 1 // expects at most 1 positional (package name)
11 static providerName = 'CircleCI'
12 static providerEntity = 'CircleCI pipeline'
13
14 static usage = [
15 '[package] --org-id <uuid> --project-id <uuid> --pipeline-definition-id <uuid> --vcs-origin <origin> [--context-id <uuid>...] [--allow-publish] [--allow-stage-publish] [-y|--yes]',
16 ]
17
18 static definitions = [
19 new Definition('org-id', {
20 default: null,
21 type: String,
22 required: true,
23 description: 'CircleCI organization UUID',
24 }),
25 new Definition('project-id', {
26 default: null,
27 type: String,
28 required: true,
29 description: 'CircleCI project UUID',
30 }),
31 new Definition('pipeline-definition-id', {
32 default: null,
33 type: String,
34 required: true,
35 description: 'CircleCI pipeline definition UUID',
36 }),
37 new Definition('vcs-origin', {
38 default: null,
39 type: String,
40 required: true,
41 description: "CircleCI repository origin in format 'provider/owner/repo'",
42 }),
43 new Definition('context-id', {
44 default: null,
45 type: [null, String, Array],
46 description: 'CircleCI context UUID to match',
47 }),
48 trustDefinitions['allow-publish'],
49 trustDefinitions['allow-stage-publish'],
50 // globals are alphabetical
51 globalDefinitions['dry-run'],
52 globalDefinitions.json,
53 globalDefinitions.registry,
54 globalDefinitions.yes,
55 ]
56
57 validateUuid (value, fieldName) {
58 validateUUID(value, fieldName)
59 }
60
61 validateVcsOrigin (value) {
62 // Expected format: provider/owner/repo (e.g., github.com/owner/repo, bitbucket.org/owner/repo)
63 if (value.includes('://')) {
64 throw new Error("vcs-origin must not include a scheme (e.g., use 'github.com/owner/repo' not 'https://github.com/owner/repo')")
65 }
66 const parts = value.split('/')
67 if (parts.length < 3) {
68 throw new Error("vcs-origin must be in format 'provider/owner/repo'")
69 }
70 }
71
72 // Generate a URL from vcs-origin (e.g., github.com/npm/repo -> https://github.com/npm/repo)
73 getVcsOriginUrl (vcsOrigin) {
74 if (!vcsOrigin) {
75 return null
76 }
77 // vcs-origin format: github.com/owner/repo or bitbucket.org/owner/repo
78 return `https://${vcsOrigin}`
79 }
80
81 static optionsToBody (options) {
82 const { orgId, projectId, pipelineDefinitionId, vcsOrigin, contextIds } = options
83 const trustConfig = {
84 type: 'circleci',
85 claims: {
86 'oidc.circleci.com/org-id': orgId,
87 'oidc.circleci.com/project-id': projectId,
88 'oidc.circleci.com/pipeline-definition-id': pipelineDefinitionId,
89 'oidc.circleci.com/vcs-origin': vcsOrigin,
90 },
91 }
92 if (contextIds && contextIds.length > 0) {
93 trustConfig.claims['oidc.circleci.com/context-ids'] = contextIds
94 }
95 return trustConfig
96 }
97
98 static bodyToOptions (body) {
99 return {
100 ...(body.id) && { id: body.id },
101 ...(body.type) && { type: body.type },
102 ...(body.claims?.['oidc.circleci.com/org-id']) && { orgId: body.claims['oidc.circleci.com/org-id'] },
103 ...(body.claims?.['oidc.circleci.com/project-id']) && { projectId: body.claims['oidc.circleci.com/project-id'] },
104 ...(body.claims?.['oidc.circleci.com/pipeline-definition-id']) && {
105 pipelineDefinitionId: body.claims['oidc.circleci.com/pipeline-definition-id'],
106 },
107 ...(body.claims?.['oidc.circleci.com/vcs-origin']) && { vcsOrigin: body.claims['oidc.circleci.com/vcs-origin'] },
108 ...(body.claims?.['oidc.circleci.com/context-ids']) && { contextIds: body.claims['oidc.circleci.com/context-ids'] },
109 }
110 }
111
112 // Override flagsToOptions since CircleCI doesn't use file/entity pattern
113 async flagsToOptions ({ positionalArgs, flags }) {
114 const content = await this.optionalPkgJson()
115 const pkgName = positionalArgs[0] || content.name
116
117 if (!pkgName) {
118 throw new Error('Package name must be specified either as an argument or in package.json file')
119 }
120
121 const orgId = flags['org-id']
122 const projectId = flags['project-id']
123 const pipelineDefinitionId = flags['pipeline-definition-id']
124 const vcsOrigin = flags['vcs-origin']
125 const contextIds = flags['context-id']
126
127 // Validate required flags
128 if (!orgId) {
129 throw new Error('org-id is required')
130 }
131 if (!projectId) {
132 throw new Error('project-id is required')
133 }
134 if (!pipelineDefinitionId) {
135 throw new Error('pipeline-definition-id is required')
136 }
137 if (!vcsOrigin) {
138 throw new Error('vcs-origin is required')
139 }
140
141 // Validate formats
142 this.validateUuid(orgId, 'org-id')
143 this.validateUuid(projectId, 'project-id')
144 this.validateUuid(pipelineDefinitionId, 'pipeline-definition-id')
145 this.validateVcsOrigin(vcsOrigin)
146 if (contextIds?.length > 0) {
147 for (const contextId of contextIds) {
148 this.validateUuid(contextId, 'context-id')
149 }
150 }
151
152 return {
153 values: {
154 package: pkgName,
155 orgId,
156 projectId,
157 pipelineDefinitionId,
158 vcsOrigin,
159 ...(contextIds?.length > 0 && { contextIds }),
160 },
161 fromPackageJson: {},
162 warnings: [],
163 urls: {
164 package: this.getFrontendUrl({ pkgName }),
165 vcsOrigin: this.getVcsOriginUrl(vcsOrigin),
166 },
167 }
168 }
169
170 async exec (positionalArgs, flags) {
171 await this.createConfigCommand({
172 positionalArgs,
173 flags,
174 })
175 }
176 }
177
178 module.exports = TrustCircleCI