Skip to content

Commit ec6fe97

Browse files
authored
add ability to collapse comment content, collapsibleThreshold input and summary table (#14)
1 parent 2862ab6 commit ec6fe97

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Creates a comment inside Pull Request with the human-readable summary of the cha
1919
2020
| Input | Required | Default | Description |
2121
| --- | :---: | :---: | --- |
22+
| `collapsibleThreshold` | No | `'25'` | Number of lock changes, which will result in collapsed comment content an addition of summary table. |
2223
| `path` | No | `'yarn.lock'` | Path to the `yarn.lock` file in the repository. Default value points to the file at project root. |
2324
| `token` | **Yes** | - | GitHub token for the bot, so it can publish a comment in the pull request. |
2425
| `updateComment` | No | `'true'` | Should the bot update the summary comment. If value is other than default, bot will post new comment on each new commit. |

action.js

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const COMMENT_HEADER = '## `yarn.lock` changes';
1414
const getStatusLabel = (status) =>
1515
`[<sub><img alt="${status.toUpperCase()}" src="${ASSETS_URL}/${status}.svg" height="16" /></sub>](#)`;
1616

17-
const formatNameAndVersion = (obj) =>
17+
const formatEntry = (obj) =>
1818
Object.fromEntries(
1919
Object.keys(obj.object).map((key) => {
2020
const nameParts = key.split('@');
@@ -25,14 +25,14 @@ const formatNameAndVersion = (obj) =>
2525

2626
const diffLocks = (previous, current) => {
2727
const changes = {};
28-
const previousPackages = formatNameAndVersion(previous);
29-
const currentPackages = formatNameAndVersion(current);
28+
const previousPackages = formatEntry(previous);
29+
const currentPackages = formatEntry(current);
3030

3131
Object.keys(previousPackages).forEach((key) => {
3232
changes[key] = {
3333
previous: previousPackages[key].version,
3434
current: '-',
35-
status: getStatusLabel('removed')
35+
status: 'removed'
3636
};
3737
});
3838

@@ -41,17 +41,17 @@ const diffLocks = (previous, current) => {
4141
changes[key] = {
4242
previous: '-',
4343
current: currentPackages[key].version,
44-
status: getStatusLabel('added')
44+
status: 'added'
4545
};
4646
} else {
4747
if (changes[key].previous === currentPackages[key].version) {
4848
delete changes[key];
4949
} else {
5050
changes[key].current = currentPackages[key].version;
5151
if (compareVersions(changes[key].previous, changes[key].current) === 1) {
52-
changes[key].status = getStatusLabel('downgraded');
52+
changes[key].status = 'downgraded';
5353
} else {
54-
changes[key].status = getStatusLabel('updated');
54+
changes[key].status = 'updated';
5555
}
5656
}
5757
}
@@ -65,17 +65,43 @@ const createTable = (lockChanges) =>
6565
[
6666
['Name', 'Status', 'Previous', 'Current'],
6767
...Object.entries(lockChanges)
68-
.map(([key, { status, previous, current }]) => ['`' + key + '`', status, previous, current])
68+
.map(([key, { status, previous, current }]) => [
69+
'`' + key + '`',
70+
getStatusLabel(status),
71+
previous,
72+
current
73+
])
6974
.sort((a, b) => a[0].localeCompare(b[0]))
7075
],
7176
{ align: ['l', 'c', 'c', 'c'], alignDelimiters: false }
7277
);
7378

79+
const countStatuses = (lockChanges, statusToCount) =>
80+
Object.values(lockChanges).filter(({ status }) => status === statusToCount).length;
81+
82+
const createSummaryRow = (lockChanges, status) => {
83+
const statusCount = countStatuses(lockChanges, status);
84+
return statusCount ? [getStatusLabel(status), statusCount] : undefined;
85+
};
86+
87+
const createSummary = (lockChanges) =>
88+
markdownTable(
89+
[
90+
['Status', 'Count'],
91+
createSummaryRow(lockChanges, 'added'),
92+
createSummaryRow(lockChanges, 'updated'),
93+
createSummaryRow(lockChanges, 'downgraded'),
94+
createSummaryRow(lockChanges, 'removed')
95+
].filter(Boolean),
96+
{ align: ['l', 'c'], alignDelimiters: false }
97+
);
98+
7499
const run = async () => {
75100
try {
76101
const octokit = github.getOctokit(core.getInput('token'));
77102
const inputPath = core.getInput('path');
78103
const updateComment = core.getInput('updateComment');
104+
const collapsibleThreshold = parseInt(core.getInput('collapsibleThreshold'));
79105

80106
const { owner, repo, number } = github.context.issue;
81107

@@ -104,10 +130,26 @@ const run = async () => {
104130

105131
const masterLock = lockfile.parse(Base64.decode(masterLockResponse.data.content));
106132
const lockChanges = diffLocks(masterLock, updatedLock);
133+
const lockChangesCount = Object.keys(lockChanges).length;
107134

108-
if (Object.keys(lockChanges).length) {
135+
if (lockChangesCount) {
109136
const diffsTable = createTable(lockChanges);
110-
const commentBody = COMMENT_HEADER + '\n' + diffsTable;
137+
const collapsed = lockChangesCount >= collapsibleThreshold;
138+
139+
const changesSummary = collapsed ? '### Summary\n' + createSummary(lockChanges) : '';
140+
141+
const commentBody =
142+
COMMENT_HEADER +
143+
'\n' +
144+
changesSummary +
145+
'\n' +
146+
'<details' +
147+
(collapsed ? '' : ' open') +
148+
'>\n' +
149+
'<summary>Click to toggle table visibility</summary>\n<br/>\n\n' +
150+
diffsTable +
151+
'\n\n' +
152+
'</details>';
111153

112154
if (updateComment === 'true') {
113155
const currentComments = await octokit.issues.listComments({

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ branding:
44
icon: 'copy'
55
color: 'purple'
66
inputs:
7+
collapsibleThreshold:
8+
description: 'Number of lock changes, which will result in collapsed comment content an addition of summary table.'
9+
required: false
10+
default: '25'
711
path:
812
description: 'Path to the `yarn.lock` file in the repository. Default value points to the file at project root.'
913
required: false

0 commit comments

Comments
 (0)