-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
86 lines (69 loc) · 2.69 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
import { getInput, setFailed } from '@actions/core';
import { exec } from '@actions/exec';
import { getOctokit, context } from '@actions/github';
import {
normaliseFingerprint,
diffSizes,
diffTotals,
buildOutputText,
getPullRequest,
getAssetSizes,
sumAssetSizes,
} from './lib/helpers';
let octokit;
async function run() {
try {
const myToken = getInput('repo-token', { required: true });
octokit = getOctokit(myToken);
const pullRequest = await getPullRequest(context, octokit);
const showTotals = getInput('show-totals', { required: false }) === 'yes';
const prAssets = await getAssetSizes();
await exec(`git checkout ${pullRequest.base.sha}`);
const baseAssets = await getAssetSizes();
const fileDiffs = diffSizes(normaliseFingerprint(baseAssets), normaliseFingerprint(prAssets));
const uniqueCommentIdentifier = '_Created by [ember-asset-size-action](https://github.com/blake-education/ember-asset-size-action/)_';
let prTotals = null;
let prTotalDiffs = null;
if (showTotals) {
const baseTotals = sumAssetSizes(baseAssets);
prTotals = sumAssetSizes(prAssets);
prTotalDiffs = diffTotals(baseTotals, prTotals);
}
const body = `${buildOutputText(fileDiffs, prTotalDiffs, prTotals)}\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));
}
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;