This repository has been archived by the owner on Nov 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.ts
104 lines (89 loc) · 3.23 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
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
repoName: string
repoOwner: string
pullRequestId?: string,
env?: { [key: string]: string },
async?: boolean
}
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\/(.*)/
const ref = github.context.ref
let result = regex.exec(ref);
if (!result) {
throw new Error(`Failed to parse GitHub ref: ${ref}`)
}
return result[1]
}
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
}
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 appFilePath = core.getInput('app-file', { 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'
var env: { [key: string]: string } = {}
env = core.getMultilineInput('env', { required: false })
.map(it => {
const pair = it.split("=")
if (pair.length != 2) {
throw new Error(`Invalid env parameter: ${it}`)
}
return { key: pair[0], value: pair[1] }
})
.reduce((map, entry) => {
map[entry.key] = entry.value
return map
}, env)
const branchName = getBranchName()
const repoOwner = getRepoOwner();
const repoName = getRepoName();
const pullRequestId = getPullRequestId()
return { apiUrl, name, apiKey, appFilePath, mappingFile, workspaceFolder, branchName, repoOwner, repoName, pullRequestId, env, async }
}