Skip to content

Commit

Permalink
add option to group results in table by the change type (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek authored Sep 3, 2023
1 parent decfba5 commit 2352c3d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ jobs:
### 🔌 Inputs
| Input | Required | Default | Description |
| --- | :---: | :---: | --- |
| `collapsibleThreshold` | No | `25` | Number of lock changes, which will result in collapsed comment content and an addition of changes summary table. |
| `failOnDowngrade` | No | `false` | When a dependency downgrade is detected, fail the action. __Comment will still be posted.__ |
| `path` | No | `yarn.lock` | Path to the `yarn.lock` file in the repository. Default value points to the file at project root. |
| `token` | <ins>**Yes**</ins> | – | Repository `GITHUB_TOKEN` which allows action to make calls to the GitHub API (Octokit). |
| `updateComment` | No | `true` | Update the comment on each new commit. If value is set to `false`, bot will post a new comment on each change. |
| Input | Required | Default | Description |
|------------------------|:------------------:|:-----------:|-------------------------------------------------------------------------------------------------------------------|
| `collapsibleThreshold` | No | `25` | Number of lock changes, which will result in collapsed comment content, and an addition of changes summary table. |
| `failOnDowngrade` | No | `false` | WFail the action when a dependency downgrade is detected. __Comment will still be posted.__ |
| `path` | No | `yarn.lock` | Path to the `yarn.lock` file in the repository. Default value points to the file at project root. |
| `token` | <ins>**Yes**</ins> | – | Repository `GITHUB_TOKEN` which allows action to make calls to the GitHub API (Octokit). |
| `updateComment` | No | `true` | Update the comment on each new commit. If value is set to `false`, bot will post a new comment on each change. |
| `groupByType` | No | `false` | Group the dependencies in the comment table by the change type. |

## 📸 Preview

Expand Down
8 changes: 6 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ branding:
color: 'yellow'
inputs:
collapsibleThreshold:
description: 'Number of lock changes, which will result in collapsed comment content an addition of summary table.'
description: 'Number of lock changes, which will result in collapsed comment content, and an addition of changes summary table.'
required: false
default: '25'
failOnDowngrade:
description: 'When a dependency downgrade is detected, fail the action.'
description: 'Fail the action when a dependency downgrade is detected. Comment will still be posted.'
required: false
default: 'false'
path:
Expand All @@ -23,6 +23,10 @@ inputs:
description: 'Update the comment on each new commit. If value is set to "false", bot will post a new one on each change.'
required: false
default: 'true'
groupByType:
description: 'Group the dependencies in the comment table by the change type.'
required: false
default: 'false'
runs:
using: 'node16'
main: 'dist/index.js'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yarn-lock-changes",
"version": "0.11.1",
"version": "0.11.2",
"main": "dist/index.js",
"repository": "github:Simek/yarn-lock-changes",
"author": "Simek <me@simek.dev>",
Expand Down
5 changes: 3 additions & 2 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const run = async () => {
const inputPath = getInput('path');
const updateComment = getBooleanInput('updateComment');
const failOnDowngrade = getBooleanInput('failOnDowngrade');
const groupByType = getBooleanInput('groupByType');
const collapsibleThreshold = Math.max(parseInt(getInput('collapsibleThreshold'), 10), 0);

const { owner, repo, number } = context.issue;
Expand Down Expand Up @@ -98,10 +99,10 @@ const run = async () => {
debug('Bot comment ID: ' + commentId);

if (lockChangesCount) {
let diffsTable = createTable(lockChanges);
let diffsTable = createTable(lockChanges, groupByType);

if (diffsTable.length >= 64000) {
diffsTable = createTable(lockChanges, true);
diffsTable = createTable(lockChanges, groupByType, true);
}

const collapsed = lockChangesCount >= collapsibleThreshold;
Expand Down
16 changes: 9 additions & 7 deletions src/comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { markdownTable } = require('markdown-table');

const { STATUS, countStatuses } = require('./utils');
const { STATUS_ORDER, countStatuses } = require('./utils');

const ASSETS_URL = {
ADDED: 'https://git.io/J38HP',
Expand All @@ -12,18 +12,23 @@ const ASSETS_URL = {
const getStatusLabel = status =>
`[<sub><img alt="${status}" src="${ASSETS_URL[status]}" height="16" /></sub>](#)`;

export const createTable = (lockChanges, plainStatuses = false) =>
export const createTable = (lockChanges, groupByType = false, plainStatuses = false) =>
markdownTable(
[
['Name', 'Status', 'Previous', 'Current'],
...Object.entries(lockChanges)
.sort((a, b) =>
groupByType
? STATUS_ORDER.indexOf(a[1].status) - STATUS_ORDER.indexOf(b[1].status) ||
a[0].localeCompare(b[0])
: a[0].localeCompare(b[0])
)
.map(([key, { status, previous, current }]) => [
'`' + key + '`',
plainStatuses ? status : getStatusLabel(status),
previous,
current
])
.sort((a, b) => a[0].localeCompare(b[0]))
],
{ align: ['l', 'c', 'c', 'c'], alignDelimiters: false }
);
Expand All @@ -37,10 +42,7 @@ export const createSummary = lockChanges =>
markdownTable(
[
['Status', 'Count'],
createSummaryRow(lockChanges, STATUS.ADDED),
createSummaryRow(lockChanges, STATUS.UPDATED),
createSummaryRow(lockChanges, STATUS.DOWNGRADED),
createSummaryRow(lockChanges, STATUS.REMOVED)
...STATUS_ORDER.map(status => createSummaryRow(lockChanges, status))
].filter(Boolean),
{ align: ['l', 'c'], alignDelimiters: false }
);
6 changes: 4 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ const semverValid = require('semver/functions/valid');

export const STATUS = {
ADDED: 'ADDED',
UPDATED: 'UPDATED',
DOWNGRADED: 'DOWNGRADED',
REMOVED: 'REMOVED',
UPDATED: 'UPDATED'
REMOVED: 'REMOVED'
};

export const STATUS_ORDER = [STATUS.ADDED, STATUS.UPDATED, STATUS.DOWNGRADED, STATUS.REMOVED];

export const countStatuses = (lockChanges, statusToCount) =>
Object.values(lockChanges).filter(({ status }) => status === statusToCount).length;

Expand Down

0 comments on commit 2352c3d

Please sign in to comment.