-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathupload-preview.mjs
More file actions
98 lines (88 loc) · 2.47 KB
/
Copy pathupload-preview.mjs
File metadata and controls
98 lines (88 loc) · 2.47 KB
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
/* eslint-disable no-console, camelcase */
import { Octokit } from '@octokit/rest';
import path from 'node:path';
import surge from 'surge';
const octokit = new Octokit({ auth: process.env.GH_PR_TOKEN });
const publishFn = surge().publish();
// From github actions
const ghrepo = process.env.GITHUB_REPOSITORY || '';
const [owner, repo] = ghrepo.split('/');
const prnum = process.env.GH_PR_NUM;
const prbranch = process.env.GITHUB_REF.split('/').pop();
const uploadFolder = process.argv[2];
if (!uploadFolder) {
process.exit(1);
}
const uploadFolderName = path.basename(uploadFolder);
let uploadURL = `pf-react-${prnum ? `pr-${prnum}` : prbranch}`.replace(/[/|.]/g, '-');
switch (uploadFolderName) {
case 'coverage':
uploadURL += '-a11y.surge.sh';
break;
case 'public':
if (!prnum && prbranch === 'main') {
uploadURL = 'pf-react-staging.patternfly.org';
} else {
uploadURL += '.surge.sh';
}
break;
default:
uploadURL += `-${uploadFolderName}`;
uploadURL += '.surge.sh';
break;
}
publishFn({
project: uploadFolder,
p: uploadFolder,
domain: uploadURL,
d: uploadURL,
e: 'https://surge.surge.sh',
endpoint: 'https://surge.surge.sh'
});
function tryAddComment(comment, commentBody) {
if (!commentBody.includes(comment)) {
return comment;
}
return '';
}
if (prnum) {
octokit.issues
.listComments({
owner,
repo,
issue_number: prnum
})
.then((res) => res.data)
.then((comments) => {
let commentBody = '';
const existingComment = comments.find((comment) => comment.user.login === 'patternfly-build');
if (existingComment) {
commentBody += existingComment.body.trim();
commentBody += '\n\n';
}
if (uploadFolderName === 'public') {
commentBody += tryAddComment(`Preview: https://${uploadURL}`, commentBody);
} else if (uploadFolderName === 'coverage') {
commentBody += tryAddComment(`A11y report: https://${uploadURL}`, commentBody);
}
if (existingComment) {
octokit.issues
.updateComment({
owner,
repo,
comment_id: existingComment.id,
body: commentBody
})
.then(() => console.log('Updated comment!'));
} else {
octokit.issues
.createComment({
owner,
repo,
issue_number: prnum,
body: commentBody
})
.then(() => console.log('Created comment!'));
}
});
}