-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (63 loc) · 3.5 KB
/
index.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
import * as Commander from 'commander'
import type * as Types from './sources/types.js'
import {ExportArgs, IsDebug} from './sources/debug.js'
import {ReplaceStringWithBooleanInObject} from './sources/utility.js'
import {GetLatestWorkflowTime} from './sources/actions.js'
import {ListBranches} from './sources/branches.js'
import {CommitManager} from './sources/commits.js'
import {PurgeRequestManager} from './sources/requests.js'
import {GetIPAddress} from './sources/ipcheck.js'
import * as Actions from '@actions/core'
import * as Os from 'node:os'
Actions.info(`Running on ${Os.cpus()[0].model} with ${Os.cpus().length} threads/vCPUs.`)
const Program = new Commander.Command()
// Set options.
Program.option('--debug', 'output extra debugging', false)
.option('--gh-token <TOKEN>', 'GitHub token', '')
.option('--repo <REPO>', 'A GitHub repository. eg: owner/repo', '')
.option('--workflow-ref <WORKFLOW_REF>', 'A GitHub workflow ref. eg: refs/heads/master', '')
.option('--branch <BRANCH>', 'A GitHub branch. eg: master', '')
.option('--ci-workspace-path <PATH>', 'A path to the CI workspace.', '')
.option('--ci-action-path <PATH>', 'A path to the CI action.', '')
// Initialize Input of the options and export them.
Program.parse()
// Declare the options and print them if the debugging mode is enabled.
const ProgramRawOptions: Types.ProgramOptionsRawType = Program.opts()
if (IsDebug(ProgramRawOptions)) {
ExportArgs(ProgramRawOptions)
}
// Redefine with boolean.
const ProgramOptions = ReplaceStringWithBooleanInObject(ProgramRawOptions) as Types.ProgramOptionsType
// Print the runner's IP address.
Actions.info(`The runner's IP address: ${await GetIPAddress().then(IPAddress => IPAddress)}`)
// Get the latest workflow run time.
performance.mark('latestworkflowtime')
const LatestWorkflowRunTime = await GetLatestWorkflowTime(ProgramOptions).then(LatestWorkflowRunTime => LatestWorkflowRunTime)
Actions.info(`Getting the latest workflow run took ${Math.floor(performance.measure('latestworkflowtime-duration', 'latestworkflowtime').duration)} ms.`)
// List branches.
const Branches = await ListBranches(ProgramOptions).then(Branches => Branches)
// Get changed files.
var ChangedFiles: Array<{Branch: string; Filename: string}> = []
for (const Branch of Branches.Branches) {
const CommitManagerInstance = new CommitManager(ProgramOptions)
// eslint-disable-next-line no-await-in-loop
const CommitSHA = await CommitManagerInstance.GetCommitSHAFromLatestWorkflowTime(LatestWorkflowRunTime).then(CommitSHA => CommitSHA)
if (CommitSHA.length === 0) {
continue
}
if (CommitSHA.length === 1) {
// eslint-disable-next-line no-await-in-loop
ChangedFiles.push(...(await CommitManagerInstance.GetChangedFilesFromACommit(CommitSHA.sha).then(ChangedFiles => ChangedFiles)).map(ChangedFile => ({Branch, Filename: ChangedFile})))
} else {
// eslint-disable-next-line no-await-in-loop
ChangedFiles.push(...(await CommitManagerInstance.GetChangedFilesFromSHAToHead(CommitSHA.sha, Branch).then(ChangedFiles => ChangedFiles)).map(ChangedFile => ({Branch, Filename: ChangedFile})))
}
}
performance.mark('purge')
const PurgeRequest = new PurgeRequestManager(ProgramOptions)
PurgeRequest.AddURLs(ChangedFiles.filter(ChangedFile => ChangedFile.Branch === Branches.Default).map(ChangedFile => ChangedFile.Filename), 'latest')
for (const Branch of Branches.Branches) {
PurgeRequest.AddURLs(ChangedFiles.filter(ChangedFile => ChangedFile.Branch === Branch).map(ChangedFile => ChangedFile.Filename), Branch)
}
PurgeRequest.Start()
PurgeRequest.OnEnded()