-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
117 lines (103 loc) · 4.1 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
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
import * as path from 'path'
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as io from '@actions/io'
import { csvReport } from './src/csvReport.js'
import { isFileExist } from './src/isFileExists.js'
import { shouldWriteRootHtml, writeFolderListing } from './src/writeFolderListing.js'
import { getBranchName } from './src/helpers.js'
import { cleanupOutdatedBranches, cleanupOutdatedReports } from './src/cleanup.js'
import { writeLatestReport } from './src/writeLatest.js'
const baseDir = 'report-action'
try {
const runTimestamp = Date.now()
// vars
const sourceReportDir = core.getInput('report_dir')
const ghPagesPath = core.getInput('gh_pages')
const reportId = core.getInput('report_id')
const reportType = core.getInput('report_type')
const listDirs = core.getInput('list_dirs') == 'true'
const listDirsBranch = core.getInput('list_dirs_branch') == 'true'
const branchCleanupEnabled = core.getInput('branch_cleanup_enabled') == 'true'
const maxReports = parseInt(core.getInput('max_reports'), 10)
const branchName = getBranchName(github.context.ref, github.context.payload.pull_request)
const ghPagesBaseDir = path.join(ghPagesPath, baseDir)
const reportBaseDir = path.join(ghPagesBaseDir, branchName, reportId)
/**
* `runId` is unique but won't change on job re-run
* `runNumber` is not unique and resets from time to time
* that's why the `runTimestamp` is used to guarantee uniqueness
*/
const runUniqueId = `${github.context.runId}_${runTimestamp}`
const reportDir = path.join(reportBaseDir, runUniqueId)
// urls
const ghPagesUrl = `https://${github.context.repo.owner}.github.io/${github.context.repo.repo}`
const ghPagesBaseUrl = `${ghPagesUrl}/${baseDir}/${branchName}/${reportId}`.replaceAll(' ', '%20')
const ghPagesReportUrl = `${ghPagesBaseUrl}/${runUniqueId}`.replaceAll(' ', '%20')
const reportUrl = reportType === 'csv' ? ghPagesBaseUrl : ghPagesReportUrl
// log
console.log({
report_dir: sourceReportDir,
gh_pages: ghPagesPath,
report_id: reportId,
runUniqueId,
ref: github.context.ref,
repo: github.context.repo,
branchName,
reportBaseDir,
reportDir,
report_url: reportUrl,
listDirs,
listDirsBranch,
branchCleanupEnabled,
maxReports,
})
if (!(await isFileExist(ghPagesPath))) {
throw new Error("Folder with gh-pages branch doesn't exist: " + ghPagesPath)
}
if (!['html', 'csv'].includes(reportType)) {
throw new Error('Unsupported report type: ' + reportType)
}
const isHtmlReport = reportType === 'html'
// action
await io.mkdirP(reportBaseDir)
// cleanup (should be before the folder listing)
if (branchCleanupEnabled) {
await cleanupOutdatedBranches(ghPagesBaseDir)
}
if (maxReports > 0) {
await cleanupOutdatedReports(ghPagesBaseDir, maxReports)
}
// process report
if (isHtmlReport) {
await io.cp(sourceReportDir, reportDir, { recursive: true })
} else if (reportType === 'csv') {
await csvReport(sourceReportDir, reportBaseDir, reportId, {
sha: github.context.sha,
})
}
// folder listing
if (listDirs) {
if (await shouldWriteRootHtml(ghPagesPath)) {
await writeFolderListing(ghPagesPath, '.')
}
await writeFolderListing(ghPagesPath, baseDir)
}
if (listDirsBranch) {
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName))
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName))
if (isHtmlReport) {
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName, reportId))
}
}
if (isHtmlReport) {
await writeLatestReport(reportBaseDir)
}
// outputs
core.setOutput('report_url', reportUrl)
core.setOutput('report_history_url', ghPagesBaseUrl)
core.setOutput('run_unique_id', runUniqueId)
core.setOutput('report_path', reportDir)
} catch (error) {
core.setFailed(error.message)
}