|
1 | | -import axios from 'axios' |
| 1 | +const axios = require('axios') |
| 2 | +const { getInput, getMultilineInput, setFailed, setOutput } = require('@actions/core') |
| 3 | +const { context } = require('@actions/github') |
2 | 4 |
|
3 | | -import { |
4 | | - getInput, |
5 | | - getMultilineInput, |
6 | | - setFailed, |
7 | | - setOutput, |
8 | | -} from '@actions/core' |
9 | | -import { context } from '@actions/github' |
| 5 | +async function run() { |
| 6 | + const appId = getInput('app-id') |
| 7 | + const workflowId = getInput('workflow-id') |
| 8 | + const token = getInput('token') |
10 | 9 |
|
11 | | -const appId = getInput('app-id') |
12 | | -const workflowId = getInput('workflow-id') |
13 | | -const token = getInput('token') |
| 10 | + const labels = getMultilineInput('labels') |
14 | 11 |
|
15 | | -const labels = getMultilineInput('labels') |
| 12 | + let branch = getInput('branch') |
| 13 | + let tag = getInput('tag') |
16 | 14 |
|
17 | | -let branch = getInput('branch') |
18 | | -let tag = getInput('tag') |
19 | | - |
20 | | -if (!branch && !tag) { |
21 | | - const { ref } = context |
22 | | - branch = ref.split('refs/heads/')[1] || process.env.GITHUB_HEAD_REF |
23 | | - tag = ref.split('refs/tags/')[1] |
24 | | -} |
25 | | - |
26 | | -const variables = Object.keys(process.env).reduce((variables, variable) => { |
27 | | - if (variable.startsWith('CM_')) { |
28 | | - variables[variable.substring(3)] = process.env[variable] |
| 15 | + if (!branch && !tag) { |
| 16 | + const { ref } = context |
| 17 | + branch = ref.split('refs/heads/')[1] || process.env.GITHUB_HEAD_REF |
| 18 | + tag = ref.split('refs/tags/')[1] |
29 | 19 | } |
30 | | - return variables |
31 | | -}, {}) |
32 | 20 |
|
33 | | -const softwareVersions = [ |
34 | | - 'xcode', |
35 | | - 'flutter', |
36 | | - 'cocoapods', |
37 | | - 'node', |
38 | | - 'npm', |
39 | | - 'ndk', |
40 | | - 'java', |
41 | | - 'ruby', |
42 | | -].reduce((softwareVersions, software) => { |
43 | | - const version = getInput(software) |
44 | | - if (version) { |
45 | | - softwareVersions[software] = version |
46 | | - } |
47 | | - return softwareVersions |
48 | | -}, {}) |
| 21 | + const variables = Object.keys(process.env).reduce((variables, variable) => { |
| 22 | + if (variable.startsWith('CM_')) { |
| 23 | + variables[variable.substring(3)] = process.env[variable] |
| 24 | + } |
| 25 | + return variables |
| 26 | + }, {}) |
49 | 27 |
|
50 | | -const url = 'https://api.codemagic.io/builds' |
| 28 | + const softwareVersions = [ |
| 29 | + 'xcode', |
| 30 | + 'flutter', |
| 31 | + 'cocoapods', |
| 32 | + 'node', |
| 33 | + 'npm', |
| 34 | + 'ndk', |
| 35 | + 'java', |
| 36 | + 'ruby', |
| 37 | + ].reduce((softwareVersions, software) => { |
| 38 | + const version = getInput(software) |
| 39 | + if (version) { |
| 40 | + softwareVersions[software] = version |
| 41 | + } |
| 42 | + return softwareVersions |
| 43 | + }, {}) |
51 | 44 |
|
52 | | -const payload = { |
53 | | - appId, |
54 | | - workflowId, |
55 | | - labels, |
56 | | - branch, |
57 | | - tag, |
58 | | - environment: { variables, softwareVersions }, |
59 | | -} |
| 45 | + const url = 'https://api.codemagic.io/builds' |
60 | 46 |
|
61 | | -const headers = { |
62 | | - 'x-auth-token': token, |
63 | | -} |
| 47 | + const payload = { |
| 48 | + appId, |
| 49 | + workflowId, |
| 50 | + labels, |
| 51 | + branch, |
| 52 | + tag, |
| 53 | + environment: { variables, softwareVersions }, |
| 54 | + } |
| 55 | + |
| 56 | + const headers = { |
| 57 | + 'x-auth-token': token, |
| 58 | + } |
64 | 59 |
|
65 | | -try { |
66 | | - const response = await axios.post(url, payload, { headers }) |
| 60 | + try { |
| 61 | + const response = await axios.post(url, payload, { headers }) |
67 | 62 |
|
68 | | - const { buildId } = await response.data |
69 | | - setOutput('build-id', buildId) |
70 | | - setOutput('build-api-url', `https://api.codemagic.io/builds/${buildId}`) |
71 | | - setOutput('build-url', `https://codemagic.io/app/${appId}/build/${buildId}`) |
72 | | -} catch (error) { |
73 | | - let details = null |
74 | | - if (error.response.status === 403) { |
75 | | - details = 'Either app ID or token is incorrect' |
76 | | - } else { |
77 | | - details = error.response.data.error |
| 63 | + const { buildId } = response.data |
| 64 | + setOutput('build-id', buildId) |
| 65 | + setOutput('build-api-url', `https://api.codemagic.io/builds/${buildId}`) |
| 66 | + setOutput('build-url', `https://codemagic.io/app/${appId}/build/${buildId}`) |
| 67 | + } catch (error) { |
| 68 | + let details = null |
| 69 | + if (error.response.status === 401 || error.response.status === 403) { |
| 70 | + details = 'Either app ID or token is incorrect' |
| 71 | + } else { |
| 72 | + details = error.response.data.error |
| 73 | + } |
| 74 | + setFailed(`${error.message}: ${details}`) |
78 | 75 | } |
79 | | - setFailed(`${error.message}: ${details}`) |
80 | 76 | } |
81 | 77 |
|
| 78 | +run() |
0 commit comments