|
1 | 1 | import * as core from '@actions/core' |
2 | 2 | import * as github from '@actions/github' |
3 | | -import { populateDepotJsonFromGithub } from './populate' |
| 3 | +import { populateDepotJsonsFromGithub } from './populate' |
4 | 4 | import { Octokit } from '@octokit/rest' |
5 | 5 | import { pushDepotJsonToGithub } from './pushDepot' |
6 | 6 | import { createCommitMessage } from './message' |
| 7 | +import { DepotLocation, DepotType, RepositoryIdentifier } from './types' |
7 | 8 |
|
8 | 9 | const repoInputRegex = /[^\/\n\s\t]+\/[^\/\n\s\t]+/ |
9 | 10 |
|
| 11 | +function getRepositoryIdentifier(): RepositoryIdentifier { |
| 12 | + const repo: { owner: string; repo: string } = { owner: '', repo: '' } |
| 13 | + const repoInput = core.getInput('repo') |
| 14 | + |
| 15 | + if (repoInput.match(repoInputRegex)) { |
| 16 | + const parsedRepoInput = repoInput.split('/') |
| 17 | + repo.owner = parsedRepoInput[0] |
| 18 | + repo.repo = parsedRepoInput[1] |
| 19 | + } else throw new Error('Invalid repository input: ' + repoInput) |
| 20 | + return repo |
| 21 | +} |
| 22 | + |
| 23 | +function getDepotLocations( |
| 24 | + repo: RepositoryIdentifier |
| 25 | +): Record<DepotType, RepositoryIdentifier & Partial<DepotLocation>> { |
| 26 | + const stableBranch = core.getInput('branch') |
| 27 | + const stablePath = core.getInput('path') |
| 28 | + const betaBranch = core.getInput('pre-release-branch') |
| 29 | + const betaPath = core.getInput('pre-release-path') |
| 30 | + |
| 31 | + return { |
| 32 | + stable: { |
| 33 | + ...repo, |
| 34 | + branch: stableBranch, |
| 35 | + path: stablePath |
| 36 | + }, |
| 37 | + beta: { |
| 38 | + ...repo, |
| 39 | + branch: betaBranch, |
| 40 | + path: betaPath |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function filterDepots( |
| 46 | + locations: ReturnType<typeof getDepotLocations>, |
| 47 | + jsons: Awaited<ReturnType<typeof populateDepotJsonsFromGithub>> |
| 48 | +): Array<DepotLocation & { json: string }> { |
| 49 | + const depots: Array<DepotLocation & { json: string }> = [] |
| 50 | + for (const rawType in locations) { |
| 51 | + const type = rawType as DepotType |
| 52 | + const location = locations[type] |
| 53 | + const json = jsons[type] |
| 54 | + |
| 55 | + if (location?.path != null && location?.branch != null && json != null) { |
| 56 | + depots.push({ |
| 57 | + ...location, |
| 58 | + path: location.path, |
| 59 | + branch: location.branch, |
| 60 | + json |
| 61 | + }) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return depots |
| 66 | +} |
| 67 | + |
| 68 | +async function updateDepotJsons( |
| 69 | + locations: ReturnType<typeof getDepotLocations>, |
| 70 | + jsons: Awaited<ReturnType<typeof populateDepotJsonsFromGithub>>, |
| 71 | + client: Octokit |
| 72 | +) { |
| 73 | + const depots = filterDepots(locations, jsons) |
| 74 | + const messageInput = core.getInput('message') |
| 75 | + for (const depot of depots) { |
| 76 | + const message = createCommitMessage(depot.json, depot, client) |
| 77 | + if (message === undefined) continue |
| 78 | + await pushDepotJsonToGithub( |
| 79 | + depot.json, |
| 80 | + depot, |
| 81 | + messageInput ?? message, |
| 82 | + client |
| 83 | + ) |
| 84 | + } |
| 85 | + return depots |
| 86 | +} |
| 87 | + |
10 | 88 | /** |
11 | 89 | * The main function for the action. |
12 | 90 | * @returns {Promise<void>} Resolves when the action is complete. |
13 | 91 | */ |
14 | 92 | export async function run(): Promise<void> { |
15 | 93 | try { |
16 | | - const repo: { owner: string; repo: string } = { owner: '', repo: '' } |
17 | | - const repoInput = core.getInput('repo') |
18 | | - |
19 | | - if (repoInput.match(repoInputRegex)) { |
20 | | - const parsedRepoInput = repoInput.split('/') |
21 | | - repo.owner = parsedRepoInput[0] |
22 | | - repo.repo = parsedRepoInput[1] |
23 | | - } else throw new Error('Invalid repository input: ' + repoInput) |
| 94 | + const repo = getRepositoryIdentifier() |
| 95 | + const locations = getDepotLocations(repo) |
24 | 96 |
|
25 | | - const ghToken = core.getInput('token') |
26 | 97 | const readableFlag = core.getInput('readable') === 'true' |
| 98 | + const ghToken = core.getInput('token') |
27 | 99 |
|
28 | 100 | const client = new Octokit({ auth: ghToken }) |
| 101 | + |
| 102 | + const unified = |
| 103 | + locations.beta.branch === locations.stable.branch && |
| 104 | + locations.beta.path === locations.stable.path |
29 | 105 |
|
30 | | - const json = await populateDepotJsonFromGithub(repo, client, readableFlag) |
31 | | - |
32 | | - const dest = { |
33 | | - ...repo, |
34 | | - branch: core.getInput('branch'), |
35 | | - path: core.getInput('path') |
36 | | - } |
37 | | - |
38 | | - const message = |
39 | | - core.getInput('message') ?? createCommitMessage(json, dest, client) |
40 | | - |
41 | | - await pushDepotJsonToGithub( |
42 | | - json, |
43 | | - { |
44 | | - owner: dest.owner, |
45 | | - repo: dest.repo, |
46 | | - path: dest.path, |
47 | | - branch: dest.branch |
48 | | - }, |
49 | | - message, |
50 | | - client |
| 106 | + const jsons = await populateDepotJsonsFromGithub( |
| 107 | + repo, |
| 108 | + client, |
| 109 | + readableFlag, |
| 110 | + unified |
51 | 111 | ) |
| 112 | + |
| 113 | + updateDepotJsons(locations, jsons, client) |
52 | 114 | } catch (error) { |
53 | 115 | // Fail the workflow run if an error occurs |
54 | 116 | if (error instanceof Error) core.setFailed(error.message) |
|
0 commit comments