-
Notifications
You must be signed in to change notification settings - Fork 15
Improve error handling and further improve the logic #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Improving error handling and further logic
|
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is safe, because:
- GitHub masks all secrets in the logs
- In addition,
paramsdoesn't contain any sensitive information anyway
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>'}`); |
There was a problem hiding this comment.
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>'}`); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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]
|
@kt3k please take a look when you can, and if it looks fine, please merge and release to the marketplace 🙂 |
(cherry picked from commit 081bc30)
| @@ -0,0 +1 @@ | |||
| console.info(require('child_process').execSync('npm ci --ignore-scripts', { cwd: __dirname }).toString()); | |||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>'}"`); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
kt3k
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Further improve error handling and logic