-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.js
87 lines (67 loc) · 2.96 KB
/
main.js
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
import { getInput, setFailed } from '@actions/core';
import { exec } from '@actions/exec';
import { getOctokit, context } from '@actions/github';
import path from 'path';
import {
normaliseFingerprint,
diffSizes,
buildOutputText,
getPullRequest,
getAssetSizes,
} from './lib/helpers.mjs';
let octokit;
async function run() {
try {
const myToken = getInput('repo-token', { required: true });
octokit = getOctokit(myToken);
const pullRequest = await getPullRequest(context, octokit);
const showTotalSizeDiff = getInput('show-total-size-diff', { required: false }) === 'yes';
const workingDirectory = getInput('working-directory', { required: false });
const cwd = path.join(process.cwd(), workingDirectory);
const buildCommand = getInput('build-npm-command', { required: false });
const prAssets = await getAssetSizes({ cwd, buildCommand });
await exec(`git checkout ${pullRequest.base.sha}`);
const masterAssets = await getAssetSizes({ cwd, buildCommand });
const fileDiffs = diffSizes(normaliseFingerprint(masterAssets), normaliseFingerprint(prAssets));
const uniqueCommentIdentifier = '_Created by [ember-asset-size-action](https://github.com/mainmatter/ember-asset-size-action/)_';
const legacyUniqueCommentIdentifier = '_Created by [ember-asset-size-action](https://github.com/simplabs/ember-asset-size-action/)_';
const body = `${buildOutputText(fileDiffs, showTotalSizeDiff)}\n\n${uniqueCommentIdentifier}`;
const updateExistingComment = getInput('update-comments', { required: false });
let existingComment = false;
if (updateExistingComment === 'yes') {
const { data: comments } = await octokit.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
});
existingComment = comments.find((comment) => comment.user.login === 'github-actions[bot]' && (comment.body.endsWith(uniqueCommentIdentifier) || comment.body.endsWith(legacyUniqueCommentIdentifier)));
}
try {
if (existingComment) {
await octokit.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body,
});
} else {
await octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pullRequest.number,
body,
});
}
} catch (e) {
console.log(`Could not create a comment automatically. This could be because github does not allow writing from actions on a fork.
See https://github.community/t5/GitHub-Actions/Actions-not-working-correctly-for-forks/td-p/35545 for more information.`);
console.log(`Copy and paste the following into a comment yourself if you want to still show the diff:
${body}`);
}
} catch (error) {
setFailed(error.message);
}
}
export default run;
// auto-execute the run function
run();