-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathparams.ts
More file actions
220 lines (189 loc) · 6.28 KB
/
params.ts
File metadata and controls
220 lines (189 loc) · 6.28 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import * as github from '@actions/github'
import * as core from '@actions/core'
import { AppFile, validateMappingFile } from './app_file'
import { PushEvent } from '@octokit/webhooks-definitions/schema'
export type Params = {
projectId: string
apiKey: string
apiUrl: string
name: string
appFilePath: string
mappingFile: string | null
workspaceFolder: string | null
branchName: string
commitSha?: string
repoName: string
repoOwner: string
pullRequestId?: string
env?: { [key: string]: string }
async?: boolean
androidApiLevel?: number
iOSVersion?: number
includeTags: string[]
excludeTags: string[]
appBinaryId: string
deviceLocale?: string
deviceModel?: string
deviceOs?: string
timeout?: number
}
export function getBranchName(branchInput?: string): string {
// Priority 1: Use provided branch input if available
if (branchInput && branchInput.trim() !== '') {
return branchInput.trim()
}
// Priority 2: Try to get branch from pull_request context
const pullRequest = github.context.payload.pull_request
if (pullRequest) {
const branchName = pullRequest?.head?.ref
if (!branchName) {
throw new Error(
`Unable find pull request ref: ${JSON.stringify(
pullRequest,
undefined,
2
)}`
)
}
return branchName
}
// Priority 3: Fall back to parsing github.context.ref
const regex = /refs\/(heads|tags)\/(.*)/
const ref = github.context.ref
let result = regex.exec(ref)
if (!result || result.length < 3) {
throw new Error(`Failed to parse GitHub ref: ${ref}`)
}
return result[2]
}
function getCommitSha(): string | undefined {
return github.context.payload.pull_request?.head.sha
}
function getRepoName(): string {
return github.context.repo.repo
}
function getRepoOwner(): string {
return github.context.repo.owner
}
function getPullRequestId(): string | undefined {
const pullRequestId = github.context.payload.pull_request?.number
if (pullRequestId === undefined) return undefined
return `${pullRequestId}`
}
function getPullRequestTitle(): string | undefined {
const pullRequestTitle = github.context.payload.pull_request?.title
if (pullRequestTitle === undefined) return undefined
return `${pullRequestTitle}`
}
function getInferredName(): string {
const pullRequestTitle = getPullRequestTitle()
if (pullRequestTitle) return pullRequestTitle
if (github.context.eventName === 'push') {
const pushPayload = github.context.payload as PushEvent
const commitMessage = pushPayload.head_commit?.message
if (commitMessage) return commitMessage
}
return github.context.sha
}
function getAndroidApiLevel(apiLevel?: string): number | undefined {
return apiLevel ? +apiLevel : undefined
}
function getIOSVersion(iosVersion?: string): number | undefined {
return iosVersion ? +iosVersion : undefined
}
function getTimeout(timeout?: string): number | undefined {
return timeout ? +timeout : undefined
}
function parseTags(tags?: string): string[] {
if (tags === undefined || tags === '') return []
if (tags.includes(',')) {
const arrayTags = tags.split(',').map((it) => it.trim())
if (!Array.isArray(arrayTags)) throw new Error('tags must be an Array.')
return arrayTags
}
return [tags]
}
export async function getParameters(): Promise<Params> {
const projectId = core.getInput('project-id', { required: true })
const apiUrl =
core.getInput('api-url', { required: false }) ||
`https://api.copilot.mobile.dev/v2/project/${projectId}`
const name = core.getInput('name', { required: false }) || getInferredName()
const apiKey = core.getInput('api-key', { required: true })
const mappingFileInput = core.getInput('mapping-file', { required: false })
const workspaceFolder = core.getInput('workspace', { required: false })
const branchInput = core.getInput('branch', { required: false })
const mappingFile = mappingFileInput && validateMappingFile(mappingFileInput)
const async = core.getInput('async', { required: false }) === 'true'
const androidApiLevelString = core.getInput('android-api-level', {
required: false,
})
const iOSVersionString = core.getInput('ios-version', { required: false })
const includeTags = parseTags(
core.getInput('include-tags', { required: false })
)
const excludeTags = parseTags(
core.getInput('exclude-tags', { required: false })
)
const appFilePath = core.getInput('app-file', { required: false })
const appBinaryId = core.getInput('app-binary-id', { required: false })
const deviceOs = core.getInput('device-os', { required: false })
const hasAppFile = appFilePath !== ''
const hasAppBinaryId = appBinaryId !== ''
if (deviceOs === 'web' && (hasAppFile || hasAppBinaryId)) {
throw new Error('For web tests, neither app-file nor app-binary-id should be provided');
}
if (deviceOs !== 'web' && hasAppFile === hasAppBinaryId) {
throw new Error('Either app-file or app-binary-id must be used (but not both) for mobile tests');
}
const deviceLocale = core.getInput('device-locale', { required: false })
const deviceModel = core.getInput('device-model', { required: false })
const timeoutString = core.getInput('timeout', { required: false })
var env: { [key: string]: string } = {}
env = core
.getMultilineInput('env', { required: false })
.map((it) => {
const parts = it.split('=')
if (parts.length < 2) {
throw new Error(`Invalid env parameter: ${it}`)
}
return { key: parts[0], value: parts.slice(1).join('=') }
})
.reduce((map, entry) => {
map[entry.key] = entry.value
return map
}, env)
const branchName = getBranchName(branchInput)
const commitSha = getCommitSha()
const repoOwner = getRepoOwner()
const repoName = getRepoName()
const pullRequestId = getPullRequestId()
const androidApiLevel = getAndroidApiLevel(androidApiLevelString)
const iOSVersion = getIOSVersion(iOSVersionString)
const timeout = getTimeout(timeoutString)
return {
apiUrl,
name,
apiKey,
appFilePath,
mappingFile,
workspaceFolder,
branchName,
commitSha,
repoOwner,
repoName,
pullRequestId,
env,
async,
androidApiLevel,
iOSVersion,
includeTags,
excludeTags,
appBinaryId,
deviceLocale,
deviceModel,
deviceOs,
timeout,
projectId,
}
}