Debian Codegraph @

lib/commands/trust/github.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 path = require('node:path')
6
7 class TrustGitHub extends TrustCommand {
8 static description = 'Create a trusted relationship between a package and GitHub Actions'
9 static name = 'github'
10 static positionals = 1 // expects at most 1 positional (package name)
11 static providerName = 'GitHub Actions'
12 static providerEntity = 'GitHub repository'
13 static providerFile = 'GitHub Actions Workflow'
14 static providerHostname = 'https://github.com'
15
16 // entity means project / repository
17 static entityKey = 'repository'
18
19 static usage = [
20 '[package] --file [--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 workflow file within a repositories .GitHub folder (must end in yaml, yml)',
29 }),
30 new Definition('repository', {
31 default: null,
32 type: String,
33 description: 'Name of the repository in the format owner/repo',
34 alias: ['repo'],
35 }),
36 new Definition('environment', {
37 default: null,
38 type: String,
39 description: 'CI environment name',
40 alias: ['env'],
41 }),
42 trustDefinitions['allow-publish'],
43 trustDefinitions['allow-stage-publish'],
44 // globals are alphabetical
45 globalDefinitions['dry-run'],
46 globalDefinitions.json,
47 globalDefinitions.registry,
48 globalDefinitions.yes,
49 ]
50
51 getEntityUrl ({ providerHostname, file, entity }) {
52 if (file) {
53 return new URL(`${entity}/blob/HEAD/.github/workflows/${file}`, providerHostname).toString()
54 }
55 return new URL(entity, providerHostname).toString()
56 }
57
58 validateEntity (entity) {
59 if (entity.split('/').length !== 2) {
60 throw new Error(`${this.constructor.providerEntity} must be specified in the format owner/repository`)
61 }
62 }
63
64 validateFile (file) {
65 if (file !== path.basename(file)) {
66 throw new Error('GitHub Actions workflow must be just a file not a path')
67 }
68 }
69
70 static optionsToBody (options) {
71 const { file, repository, environment } = options
72 const trustConfig = {
73 type: 'github',
74 claims: {
75 repository,
76 workflow_ref: {
77 file,
78 },
79 ...(environment) && { environment },
80 },
81 }
82 return trustConfig
83 }
84
85 // Convert API response body to options
86 static bodyToOptions (body) {
87 const file = body.claims?.workflow_ref?.file
88 const repository = body.claims?.repository
89 const environment = body.claims?.environment
90 return {
91 ...(body.id) && { id: body.id },
92 ...(body.type) && { type: body.type },
93 ...(file) && { file },
94 ...(repository) && { repository },
95 ...(environment) && { environment },
96 }
97 }
98
99 async exec (positionalArgs, flags) {
100 await this.createConfigCommand({
101 positionalArgs,
102 flags,
103 })
104 }
105 }
106
107 module.exports = TrustGitHub