Skip to content

Commit

Permalink
add Jest, switch to semver for compare, fix downgrade issue (#39)
Browse files Browse the repository at this point in the history
* add Jest, fix downgrade issue

* fix main workflow, add test workflow

* switch to `semver`, improve sorting, add more complex test
  • Loading branch information
Simek committed Aug 22, 2021
1 parent 1d53f4d commit 3655ac7
Show file tree
Hide file tree
Showing 18 changed files with 24,417 additions and 186 deletions.
6 changes: 4 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"es2021": true,
"node": true
},
"plugins": ["prettier"],
"plugins": ["jest", "prettier"],
"extends": [
"eslint:recommended",
"plugin:jest/recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
Expand All @@ -20,5 +21,6 @@
"prefer-const": "error",
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
},
"ignorePatterns": ["dist", "node_modules"]
}
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
uses: ./
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Yarn Lock Changes Detached Package
- name: '[Test] Yarn Lock Changes Detached Package'
uses: ./
with:
path: tests/detached/yarn.lock
path: tests/ci/yarn.lock
token: ${{ secrets.GITHUB_TOKEN }}
13 changes: 13 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Run Tests
on: [pull_request]

jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
- run: yarn
- run: yarn test
3 changes: 3 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }]]
};
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
transformIgnorePatterns: ['<rootDir>/node_modules/*']
};
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@
"license": "MIT",
"scripts": {
"compile": "ncc build ./src/action.js -m",
"lint": "eslint ./src",
"optimize": "svgo -f ./assets"
"lint": "eslint .",
"optimize": "svgo -f ./assets",
"test": "jest"
},
"dependencies": {
"@actions/core": "^1.4.0",
"@actions/core": "^1.5.0",
"@actions/github": "^5.0.0",
"@yarnpkg/lockfile": "^1.1.0",
"compare-versions": "^3.6.0",
"js-base64": "^3.6.1",
"markdown-table": "^3.0.1"
"markdown-table": "^3.0.1",
"semver": "^7.3.5"
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"babel-jest": "^27.0.6",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"svgo": "^2.3.1"
"svgo": "^2.4.0"
}
}
3 changes: 2 additions & 1 deletion src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const fs = require('fs');
const { Base64 } = require('js-base64');
const path = require('path');

const { STATUS, countStatuses, createTable, createSummary, diffLocks } = require('./utils');
const { STATUS, countStatuses, diffLocks } = require('./utils');
const { createTable, createSummary } = require('./comment');

const getCommentId = async (octokit, oktokitParams, issueNumber, commentHeader) => {
const currentComments = await octokit.rest.issues.listComments({
Expand Down
46 changes: 46 additions & 0 deletions src/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { markdownTable } = require('markdown-table');

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

const ASSETS_URL = {
ADDED: 'https://git.io/J38HP',
DOWNGRADED: 'https://git.io/J38ds',
REMOVED: 'https://git.io/J38dt',
UPDATED: 'https://git.io/J38dY'
};

const getStatusLabel = status =>
`[<sub><img alt="${status}" src="${ASSETS_URL[status]}" height="16" /></sub>](#)`;

export const createTable = (lockChanges, plainStatuses = false) =>
markdownTable(
[
['Name', 'Status', 'Previous', 'Current'],
...Object.entries(lockChanges)
.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 }
);

const createSummaryRow = (lockChanges, status) => {
const statusCount = countStatuses(lockChanges, status);
return statusCount ? [getStatusLabel(status), statusCount] : undefined;
};

export const createSummary = lockChanges =>
markdownTable(
[
['Status', 'Count'],
createSummaryRow(lockChanges, STATUS.ADDED),
createSummaryRow(lockChanges, STATUS.UPDATED),
createSummaryRow(lockChanges, STATUS.DOWNGRADED),
createSummaryRow(lockChanges, STATUS.REMOVED)
].filter(Boolean),
{ align: ['l', 'c'], alignDelimiters: false }
);
71 changes: 22 additions & 49 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
const compareVersions = require('compare-versions');
const { markdownTable } = require('markdown-table');

const ASSETS_URL = {
ADDED: 'https://git.io/J38HP',
DOWNGRADED: 'https://git.io/J38ds',
REMOVED: 'https://git.io/J38dt',
UPDATED: 'https://git.io/J38dY'
};
const semverCompare = require('semver/functions/compare');
const semverCoerce = require('semver/functions/coerce');
const semverValid = require('semver/functions/valid');

export const STATUS = {
ADDED: 'ADDED',
Expand All @@ -15,52 +9,31 @@ export const STATUS = {
UPDATED: 'UPDATED'
};

const getStatusLabel = status =>
`[<sub><img alt="${status}" src="${ASSETS_URL[status]}" height="16" /></sub>](#)`;

export const createTable = (lockChanges, plainStatuses = false) =>
markdownTable(
[
['Name', 'Status', 'Previous', 'Current'],
...Object.entries(lockChanges)
.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 }
);

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

const createSummaryRow = (lockChanges, status) => {
const statusCount = countStatuses(lockChanges, status);
return statusCount ? [getStatusLabel(status), statusCount] : undefined;
};
const formatForNameCompare = key => key.substr(0, key.lastIndexOf('@'));

export const createSummary = lockChanges =>
markdownTable(
[
['Status', 'Count'],
createSummaryRow(lockChanges, STATUS.ADDED),
createSummaryRow(lockChanges, STATUS.UPDATED),
createSummaryRow(lockChanges, STATUS.DOWNGRADED),
createSummaryRow(lockChanges, STATUS.REMOVED)
].filter(Boolean),
{ align: ['l', 'c'], alignDelimiters: false }
);
const formatForVersionCompare = key => {
const version = key.substr(key.lastIndexOf('@') + 1);
return semverValid(semverCoerce(version)) || '0.0.0';
};

const formatLockEntry = obj =>
Object.fromEntries(
Object.keys(obj.object).map(key => {
const nameParts = key.split('@');
const name = nameParts[0] === '' ? '@' + nameParts[1] : nameParts[0];
return [name, { name, version: obj.object[key].version }];
})
Object.keys(obj.object)
.sort((a, b) => {
const nameCompare = formatForNameCompare(a).localeCompare(formatForNameCompare(b));
if (nameCompare === 0) {
return semverCompare(formatForVersionCompare(a), formatForVersionCompare(b));
}
return nameCompare;
})
.map(key => {
const nameParts = key.split('@');
const name = nameParts[0] === '' ? '@' + nameParts[1] : nameParts[0];
return [name, { name, version: obj.object[key].version }];
})
);

export const diffLocks = (previous, current) => {
Expand Down Expand Up @@ -88,7 +61,7 @@ export const diffLocks = (previous, current) => {
delete changes[key];
} else {
changes[key].current = currentPackages[key].version;
if (compareVersions(changes[key].previous, changes[key].current) === 1) {
if (semverCompare(changes[key].previous, changes[key].current) === 1) {
changes[key].status = STATUS.DOWNGRADED;
} else {
changes[key].status = STATUS.UPDATED;
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3655ac7

Please sign in to comment.