Skip to content

Commit

Permalink
do not post empty comment when there are no changes (#5)
Browse files Browse the repository at this point in the history
* simplify utils code, update ESLint config
  • Loading branch information
Simek authored Apr 26, 2021
1 parent 535c741 commit dce9064
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 41 deletions.
19 changes: 7 additions & 12 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
"ecmaVersion": 12
},
"rules": {
"indent": [
"error",
2
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
"indent": ["error", 2],
"no-extra-parens": ["error", "all"],
"no-var": "error",
"prefer-arrow-callback": "error",
"prefer-const": "error",
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
56 changes: 28 additions & 28 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const path = require('path');
const { markdownTable } = require('markdown-table');
const fetch = require('node-fetch');

const diff = (previous, current) => {
const diffLocks = (previous, current) => {
const changes = {};
const previousPackages = formatNameAndVersion(previous);
const currentPackages = formatNameAndVersion(current);
Expand Down Expand Up @@ -39,17 +39,22 @@ const diff = (previous, current) => {
return changes;
};

const formatNameAndVersion = (obj) => {
const packages = {};

Object.keys(obj.object).forEach((key) => {
const names = key.split('@');
const name = names[0] === '' ? '@' + names[1] : names[0];
packages[name] = { name, version: obj.object[key].version };
});

return packages;
};
const formatNameAndVersion = (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 }];
}))
;

const createTable = (lockChanges) =>
markdownTable([
['Name', 'Status', 'Previous', 'Current'],
...Object.entries(lockChanges).map(([key, { status, previous, current }]) =>
['`' + key + '`', status, previous, current]
).sort((a, b) => a[0].localeCompare(b[0]))
])
;

const run = async () => {
try {
Expand Down Expand Up @@ -77,22 +82,17 @@ const run = async () => {
}

const masterLock = lockfile.parse(await response.text());
const lockChanges = diff(masterLock, updatedLock);

const diffsTable = markdownTable([
['Name', 'Status', 'Previous', 'Current'],
...Object.entries(lockChanges).map(([key, value]) => (
['`' + key + '`', value.status, value.previous, value.current]
)).sort((a, b) => a[0].localeCompare(b[0]))
]);

await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: '## `yarn.lock` changes' + '\n' + diffsTable
});

const lockChanges = diffLocks(masterLock, updatedLock);

if (Object.keys(lockChanges).length) {
const diffsTable = createTable(lockChanges);
await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: '## `yarn.lock` changes' + '\n' + diffsTable
});
}
} catch (error) {
core.setFailed(error.message);
}
Expand Down
Loading

0 comments on commit dce9064

Please sign in to comment.