Skip to content

Commit

Permalink
update comment by default, add ability to switch to old behaviour (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simek authored Apr 27, 2021
1 parent 787d945 commit 2862ab6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ Creates a comment inside Pull Request with the human-readable summary of the cha
## Inputs
| Input | Required | Description |
| --- | --- | --- |
| `path` | No | Path to the `yarn.lock` file in the repository. Default `yarn.lock` (project root). |
| `token` | **Yes** | GitHub token for the bot, so it can publish a comment in the pull request. |
| Input | Required | Default | Description |
| --- | :---: | :---: | --- |
| `path` | No | `'yarn.lock'` | Path to the `yarn.lock` file in the repository. Default value points to the file at project root. |
| `token` | **Yes** | - | GitHub token for the bot, so it can publish a comment in the pull request. |
| `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. |
61 changes: 53 additions & 8 deletions action.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const path = require('path');

const GH_RAW_URL = 'https://raw.githubusercontent.com';
const ASSETS_URL = `${GH_RAW_URL}/Simek/yarn-lock-changes/main/assets`;
const COMMENT_HEADER = '## `yarn.lock` changes';

const getStatusLabel = (status) =>
`[<sub><img alt="${status.toUpperCase()}" src="${ASSETS_URL}/${status}.svg" height="16" /></sub>](#)`;
Expand Down Expand Up @@ -74,17 +75,18 @@ const run = async () => {
try {
const octokit = github.getOctokit(core.getInput('token'));
const inputPath = core.getInput('path');
const updateComment = core.getInput('updateComment');

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

if (!number) {
throw new Error('Cannot find the PR!');
throw new Error('💥 Cannot find the PR, aborting!');
}

const lockPath = path.resolve(process.cwd(), inputPath);

if (!fs.existsSync(lockPath)) {
throw new Error(`${lockPath} does not exist!`);
throw new Error('💥 It looks like lock does not exist in this PR, aborting!');
}

const content = await fs.readFileSync(lockPath, { encoding: 'utf8' });
Expand All @@ -96,17 +98,60 @@ const run = async () => {
path: inputPath
});

if (!masterLockResponse || !masterLockResponse.data || !masterLockResponse.data.content) {
throw new Error('💥 Cannot fetch base lock, aborting!');
}

const masterLock = lockfile.parse(Base64.decode(masterLockResponse.data.content));
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
});
const commentBody = COMMENT_HEADER + '\n' + diffsTable;

if (updateComment === 'true') {
const currentComments = await octokit.issues.listComments({
owner,
repo,
issue_number: number,
per_page: 100
});

if (!currentComments || !currentComments.data) {
throw new Error('💥 Cannot fetch PR comments, aborting!');
}

const commentId = currentComments.data
.filter(
(comment) =>
comment.user.login === 'github-actions[bot]' &&
comment.body.startsWith(COMMENT_HEADER)
)
.map((comment) => comment.id)[0];

if (commentId) {
await octokit.issues.updateComment({
owner,
repo,
comment_id: commentId,
body: commentBody
});
} else {
await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: commentBody
});
}
} else {
await octokit.issues.createComment({
owner,
repo,
issue_number: number,
body: commentBody
});
}
}
} catch (error) {
core.setFailed(error.message);
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ branding:
color: 'purple'
inputs:
path:
description: 'Path to the `yarn.lock` file in the repository.'
description: 'Path to the `yarn.lock` file in the repository. Default value points to the file at project root.'
required: false
default: 'yarn.lock'
token:
description: 'GitHub token for the bot, so it can publish a comment in the pull request.'
required: true
updateComment:
description: 'Should the bot update the summary comment. If value is other than default, bot will post new comment on each new commit.'
required: false
default: 'true'
runs:
using: 'node12'
main: 'dist/index.js'
Loading

0 comments on commit 2862ab6

Please sign in to comment.