-
Notifications
You must be signed in to change notification settings - Fork 35
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
feat: add support for workflow_dispatch event #51
Conversation
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.
See inline comments. Add tests
src/index.js
Outdated
const pullRequestNumber = pr.number | ||
let pr = pull_request | ||
|
||
const pullRequestNumber = PR_NUMBER || pr.number |
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 may throw an exception if the pr-number input is not provided and this is not run in the context of a pull request. make it more robust
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've added a check to log an error if a PR number isn't present.
Looks like core.getInput
will return as an empty string for an optional input, so this should be okay?
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 not the problem. if PR_NUMER is falsy and github.context.payload.pull_request is falsy, it will throw a JavaScript error
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've included a check after this statement since a pull request is required to do any of the later work. Is that not enough? I can add a check before I do the variable assignment to pullRequestNumber
if that is better?
const pullRequestNumber = PR_NUMBER || pr.number
if (!pullRequestNumber) {
return logError(
'No pull request number has been found. Please make sure a pull request number has been provided'
)
}
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 suppose I can introduce a variable hasPullRequest
? Then I can check the value of that, and error out if nothing is present or defined.
const hasPullRequest = pull_request || PR_NUMBER
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.
The action needs to be robust enough that wrong user input doesn't lead to unexpected errors
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.
@simoneb okay check it out now, I think there are enough checks and conditionals to prevent an issue based on user input or context.
I'm not quite sure what should be added as a test here since there is not much to test in |
including @gilach |
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.
The logic looks good but the structure of the code needs improvement. See inline comments.
src/index.js
Outdated
const isPullRequest = Boolean(pull_request) | ||
const isWorkflowDispatch = Boolean(workflow) |
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.
these boolean variables are unnecessary. since you're checking for their truthyness you don't need temp variables
src/index.js
Outdated
let pr | ||
let pullRequestNumber | ||
|
||
// Conditionally sets pr and pullRequestNumber based on context. The default context case is "pull request" | ||
if (isWorkflowDispatch) { | ||
if (!PR_NUMBER || (PR_NUMBER && isNaN(PR_NUMBER))) { | ||
return logError( | ||
'Missing or invalid pull request number. Please make sure you are using a valid pull request number' | ||
) | ||
} | ||
|
||
pullRequestNumber = PR_NUMBER | ||
const repo = github.context.payload.repository | ||
const owner = repo.owner.login | ||
const repoName = repo.name | ||
|
||
pr = await getPullRequest(owner, repoName, pullRequestNumber) | ||
} else { | ||
pr = pull_request | ||
pullRequestNumber = pr.number | ||
} |
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.
extract this logic in a function, it will avoid cluttering the body of the main function and it will spare the need for mutable state. just return a "pull request" from that function and you can then extract the number from it
@simoneb I've made some changes that I think encapsulate the functionality pretty nicely. I looked into writing some tests and found that the current way I wrote this would require a lot of mocking to get the test to work. I can do that, or I can pass the inputs, |
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'm still hoping that we can achieve a better implementation. The code works, but it can be written in a better way:
- the logic to test if we are in a supported context is too specific. We don't really care if we are in a workflow dispatch event, as long as we have a PR_NUMER in case we are not in a pull_request context
- let's get the inputs in a single place, meaning that you can move loading the PR_NUMER in the index.js file
- let's keep the logic to use payload.pull_request outside of the getPullRequest function. That function should not load any globals but it should get all the inputs it needs as arguments, so it's more easily testable. This includes the PR_NUMBER argument
- in index.js the logic to obtain a pull request object can be simplified to:
const pr = pull_request || await getPullRequest(PR_NUMER)
@simoneb I've made the requested changes. I did try and write a test for this new function, but it ends up just testing whether or not we return the mocked value from mocking |
Co-authored-by: Simone Busoli <[email protected]>
Co-authored-by: Simone Busoli <[email protected]>
@simoneb Yeah good call-outs. I think we can leave it as Looks like |
Closes #45
workflow_dispatch
eventpr-number
workflow_dispatch
event, fetches data from GitHub for the PR that provides the relevant data aspr
.Checklist
npm run test
andnpm run benchmark
and the Code of conduct