forked from github/dependabot-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinputs.ts
143 lines (118 loc) · 3.61 KB
/
inputs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import fs from 'fs'
import path from 'path'
import * as core from '@actions/core'
import {Context} from '@actions/github/lib/context'
import {WorkflowDispatchEvent} from '@octokit/webhooks-types'
const DYNAMIC = 'dynamic'
const DEPENDABOT_ACTOR = 'dependabot[bot]'
// JobParameters are the Action inputs required to execute the job
export class JobParameters {
constructor(
readonly jobId: number,
readonly jobToken: string,
readonly credentialsToken: string,
readonly dependabotApiUrl: string,
readonly dependabotApiDockerUrl: string,
readonly updaterImage: string,
readonly workingDirectory: string
) {}
}
export function getJobParameters(ctx: Context): JobParameters | null {
checkEnvironmentAndContext(ctx)
if (ctx.actor !== DEPENDABOT_ACTOR) {
core.warning('This workflow can only be triggered by Dependabot.')
return null
}
if (
process.env.GITHUB_TRIGGERING_ACTOR &&
process.env.GITHUB_TRIGGERING_ACTOR !== DEPENDABOT_ACTOR
) {
core.warning(
'Dependabot workflows cannot be re-run. Retrigger this update via Dependabot instead.'
)
return null
}
if (ctx.eventName === DYNAMIC) {
return fromWorkflowInputs(ctx)
} else {
core.warning(
`Dependabot Updater Action does not support '${ctx.eventName}' events.`
)
return null
}
}
function checkEnvironmentAndContext(ctx: Context): boolean {
let valid = true
if (!ctx.actor) {
core.error('GITHUB_ACTOR is not defined')
valid = false
}
if (!ctx.eventName) {
core.error('GITHUB_EVENT_NAME is not defined')
valid = false
}
if (!process.env.GITHUB_WORKSPACE) {
core.error('GITHUB_WORKSPACE is not defined')
valid = false
}
if (!valid) {
throw new Error('Required Actions environment variables missing.')
} else {
return valid
}
}
function fromWorkflowInputs(ctx: Context): JobParameters {
const evt = ctx.payload as WorkflowDispatchEvent
if (!evt.inputs) {
throw new Error('Event payload has no inputs')
}
const dependabotApiDockerUrl =
evt.inputs.dependabotApiDockerUrl || evt.inputs.dependabotApiUrl
const workingDirectory = absoluteWorkingDirectory(
evt.inputs.workingDirectory as string
)
return new JobParameters(
parseInt(evt.inputs.jobId as string, 10),
evt.inputs.jobToken as string,
evt.inputs.credentialsToken as string,
evt.inputs.dependabotApiUrl as string,
dependabotApiDockerUrl as string,
evt.inputs.updaterImage as string,
workingDirectory
)
}
function absoluteWorkingDirectory(workingDirectory: string): string {
const workspace = process.env.GITHUB_WORKSPACE as string
if (!workingDirectory) {
throw new Error('The workingDirectory input must not be blank.')
}
if (!directoryExistsSync(workspace)) {
throw new Error('The GITHUB_WORKSPACE directory does not exist.')
}
const fullPath = path.join(workspace, workingDirectory)
if (!directoryExistsSync(fullPath)) {
throw new Error(
`The workingDirectory '${workingDirectory}' does not exist in GITHUB_WORKSPACE`
)
}
return fullPath
}
function directoryExistsSync(directoryPath: string): boolean {
let stats: fs.Stats
try {
stats = fs.statSync(directoryPath)
return stats.isDirectory()
} catch (error: unknown) {
if (isNodeError(error) && error.code === 'ENOENT') {
return false
} else if (error instanceof Error) {
throw new Error(
`Encountered an error when checking whether path '${directoryPath}' exists: ${error.message}`
)
}
}
return false
}
function isNodeError(error: any): error is NodeJS.ErrnoException {
return error instanceof Error
}