Skip to content
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

Merged
merged 29 commits into from
Aug 10, 2021

Conversation

jhoffmcd
Copy link
Contributor

@jhoffmcd jhoffmcd commented Jul 5, 2021

Closes #45

  • Add support and check for the workflow_dispatch event
  • Add a new optional input for pr-number
  • In the case of a workflow_dispatch event, fetches data from GitHub for the PR that provides the relevant data as pr.

Checklist

@simoneb simoneb self-assigned this Jul 6, 2021
@simoneb simoneb changed the title feat: add support for workflow dispatch event feat: add support for workflow_dispatch event Jul 6, 2021
Copy link
Collaborator

@simoneb simoneb left a 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

package-lock.json Outdated Show resolved Hide resolved
src/getPullRequest.js Outdated Show resolved Hide resolved
src/index.js Outdated
const pullRequestNumber = pr.number
let pr = pull_request

const pullRequestNumber = PR_NUMBER || pr.number
Copy link
Collaborator

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

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'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?

https://github.com/actions/toolkit/blob/2f164000dcd42fb08287824a3bc3030dbed33687/packages/core/src/core.ts#L69-L77

Copy link
Collaborator

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

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'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'
  )
}

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 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

Copy link
Collaborator

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

Copy link
Contributor Author

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.

src/index.js Outdated Show resolved Hide resolved
@jhoffmcd
Copy link
Contributor Author

jhoffmcd commented Jul 9, 2021

I'm not quite sure what should be added as a test here since there is not much to test in getPullRequest, it's mostly just a wrapper for octokit. Definitely will try to write some tests if I can understand more of what we could effectively test here.

@simoneb
Copy link
Collaborator

simoneb commented Jul 11, 2021

including @gilach

Copy link
Collaborator

@simoneb simoneb left a 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
Comment on lines 29 to 30
const isPullRequest = Boolean(pull_request)
const isWorkflowDispatch = Boolean(workflow)
Copy link
Collaborator

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
Comment on lines 39 to 59
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
}
Copy link
Collaborator

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

@jhoffmcd
Copy link
Contributor Author

jhoffmcd commented Aug 5, 2021

@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, github object, etc as arguments to getPullRequest which would make it a little bit easier to test. What path do we prefer for our tests here?

Copy link
Collaborator

@simoneb simoneb left a 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)

@jhoffmcd
Copy link
Contributor Author

jhoffmcd commented Aug 6, 2021

@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 @actions/github for the octokit stuff. Not sure how valuable a test like that is.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
jhoffmcd and others added 2 commits August 9, 2021 10:39
Co-authored-by: Simone Busoli <[email protected]>
@jhoffmcd
Copy link
Contributor Author

jhoffmcd commented Aug 9, 2021

@simoneb Yeah good call-outs. I think we can leave it as pr-number according to the docs: https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputsinput_id.

Looks like - is accepted as part of the string identifier for an input. I've committed your suggestions.

@simoneb simoneb merged commit da66b6c into fastify:main Aug 10, 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.

Support workflow_dispatch Event Context
2 participants