Skip to content

Conversation

@devlato
Copy link
Contributor

@devlato devlato commented Nov 18, 2021

Further improve error handling and logic

devlato and others added 2 commits November 18, 2021 15:53
Improving error handling and further logic
@devlato
Copy link
Contributor Author

devlato commented Nov 18, 2021

Hey @kt3k – sorry for annoyance, here's another follow-up one 🙂 Probably the last one

const url = `/repos/${credentials.owner}/${credentials.repo}/pulls/${pullNumber}`;

core.info(`Making PATCH request to "${url}" with params ${JSON.stringify(params)}`);
await octokit.request(`PATCH ${url}`, params);
Copy link
Contributor Author

@devlato devlato Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making the request manually, because of a strange error 'Body must be a JSON' that the octokit.pulls.update returns

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested it on my other repo and it worked like a charm (and both PR title and PR body were updated as expected), while the pulls.update threw the above error

})
.catch((e) => {
core.setFailed(e.message);
core.setFailed(e.stack || e.message);
Copy link
Contributor Author

@devlato devlato Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stack trace's better than only a message, right?

index.js Outdated

const url = `/repos/${credentials.owner}/${credentials.repo}/pulls/${pullNumber}`;

core.info(`Making PATCH request to "${url}" with params ${JSON.stringify(params)}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is safe, because:

  1. GitHub masks all secrets in the logs
  2. In addition, params doesn't contain any sensitive information anyway

@devlato devlato changed the title Improve error handling and further logic Improve error handling and further improve the logic Nov 18, 2021
index.js Outdated
core.info(`No such pull request: source=${source} base=${base}`);
return;
if (pulls.length === 0) {
throw new Error(`No pull request found for source="${source || '<not found>'}" base=${baseBranch || '<not specified>'}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw would stop the script execution here and would be reported to the core

index.js Outdated
core.info(`Found pull request #${number}`);
const pullRequest = pulls.find((p) => p.state === 'open');
if (pullRequest == null) {
throw new Error(`No open pull request found for source="${source || '<not found>'}" base=${baseBranch || '<not specified>'}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto


const { number } = pulls[0];
core.info(`Found pull request #${number}`);
const pullRequest = pulls.find((p) => p.state === 'open');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is (technically) safer than pulls[0]

@devlato
Copy link
Contributor Author

devlato commented Nov 18, 2021

@kt3k please take a look when you can, and if it looks fine, please merge and release to the marketplace 🙂

@@ -0,0 +1 @@
console.info(require('child_process').execSync('npm ci --ignore-scripts', { cwd: __dirname }).toString());
Copy link
Contributor Author

@devlato devlato Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handles npm packages pre-installation (in a fast CI-compliant way), instead of storing node_modules/ in it. When node_modules/ contain binaries, they won't run anyway

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this recommended way to remove node_modules from repository?

The doc seems recommending the use of ncc

When node_modules/ contain binaries, they won't run anyway

I don't think our dependency includes any binary

Copy link
Contributor Author

@devlato devlato Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think our dependency includes any binary

Yeah no binaries in our deps atm

The doc seems recommending the use of ncc

I'd say safer to use a docker images for those scenarios, as ncc still builds a platform and architecture-specific binary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this recommended way to remove node_modules from repository?

Nope, technically, there's no recommended way – this one's straightforward though, and still guarantees the result.
The docs kinda suggest just ignoring it and keeping node_modules/ in the repo but I can name, like, a hundred reasons why it's totally wrong – I'm 100% sure this is just a bloody hack GitHub devs had to suggest to get away with

core.info(`No such pull request: source=${source} base=${base}`);
return;
if (pulls.length === 0) {
throw new Error(`No pull request found for a source branch "${sourceBranch || '<not found>'}" and a base branch "${baseBranch || '<not specified>'}"`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the action fails in this case? In our use case, this needs to be success (When no need of update, we want this action to be just silent.

Copy link
Contributor Author

@devlato devlato Nov 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kt3k right, but what's the scenario where the workflow gets triggered but no pull request exists? I assume this would be a developer mistake (e.g. they've subscribed to a wrong event) – wouldn't it be better to fail and let them know that something's wrong and let them fix it, rather than just keep running and silently consuming their GitHub actions monthly minutes allowance?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Thought twice, and I think your suggestion is correct. Let's try it

Copy link
Owner

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kt3k kt3k merged commit c23dd69 into kt3k:master Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants