forked from mobile-dev-inc/action-maestro-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.ts
182 lines (155 loc) · 5.25 KB
/
params.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
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
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 = {
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,
timeout?: number,
}
function getBranchName(): string {
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
}
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 apiUrl = core.getInput('api-url', { required: false }) || 'https://api.mobile.dev'
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 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 })
if (!(appFilePath !== "") !== (appBinaryId !== "")) {
throw new Error("Either app-file or app-binary-id must be used")
}
const deviceLocale = core.getInput('device-locale', { 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()
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,
timeout,
}
}