-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.ts
132 lines (119 loc) · 4.83 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as path from 'path'
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as io from '@actions/io'
import { writeFolderListing, shouldWriteRootHtml } from './src/writeFolderListing.js'
import {
getLastRunId,
writeExecutorJson,
spawnAllure,
writeLastRunId,
updateDataJson,
writeAllureListing,
getTestResultIcon,
isAllureResultsOk,
} from './src/allure.js'
import { getBranchName } from './src/helpers.js'
import { isFileExist } from './src/isFileExists.js'
import { cleanupOutdatedBranches, cleanupOutdatedReports } from './src/cleanup.js'
import { writeLatestReport } from './src/writeLatest.js'
const baseDir = 'allure-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 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 githubActionRunUrl = `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
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')
// log
console.log({
report_dir: sourceReportDir,
gh_pages: ghPagesPath,
report_id: reportId,
runUniqueId,
ref: github.context.ref,
repo: github.context.repo,
branchName,
reportBaseDir,
reportDir,
listDirsBranch,
report_url: ghPagesReportUrl,
listDirs,
branchCleanupEnabled,
maxReports,
})
if (!(await isFileExist(ghPagesPath))) {
throw new Error("Folder with gh-pages branch doesn't exist: " + ghPagesPath)
}
if (!(await isAllureResultsOk(sourceReportDir))) {
throw new Error('There were issues with the allure-results, see error above.')
}
// action
await io.mkdirP(reportBaseDir)
// cleanup (should be before the folder listing)
if (branchCleanupEnabled) {
await cleanupOutdatedBranches(ghPagesBaseDir, github.context.repo)
}
if (maxReports > 0) {
await cleanupOutdatedReports(ghPagesBaseDir, maxReports)
}
// folder listing
if (listDirs) {
if (await shouldWriteRootHtml(ghPagesPath)) {
await writeFolderListing(ghPagesPath, '.')
}
await writeFolderListing(ghPagesPath, baseDir)
}
if (listDirsBranch) {
await writeFolderListing(ghPagesPath, path.join(baseDir, branchName))
}
// process allure report
const lastRunId = await getLastRunId(reportBaseDir)
if (lastRunId) {
const historyDir = path.join(reportBaseDir, lastRunId, 'history')
if (await isFileExist(historyDir)) {
await io.cp(historyDir, sourceReportDir, { recursive: true })
}
}
await writeExecutorJson(sourceReportDir, {
runUniqueId,
buildOrder: github.context.runId,
buildUrl: githubActionRunUrl,
reportUrl: ghPagesReportUrl,
})
await spawnAllure(sourceReportDir, reportDir)
const results = await updateDataJson(reportBaseDir, reportDir, github.context.runId, runUniqueId)
await writeAllureListing(reportBaseDir)
await writeLastRunId(reportBaseDir, github.context.runId, runTimestamp)
await writeLatestReport(reportBaseDir)
// outputs
core.setOutput('report_url', ghPagesReportUrl)
core.setOutput('report_history_url', ghPagesBaseUrl)
core.setOutput('test_result', results.testResult)
core.setOutput('test_result_icon', getTestResultIcon(results.testResult))
core.setOutput('test_result_passed', results.passed)
core.setOutput('test_result_failed', results.failed)
core.setOutput('test_result_total', results.total)
core.setOutput('run_unique_id', runUniqueId)
core.setOutput('report_path', reportDir)
} catch (error) {
core.setFailed(error.message)
}