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

Fix #128 : On push, use files in commit to avoid "not found" error #130

Closed
wants to merge 7 commits into from
Closed

Fix #128 : On push, use files in commit to avoid "not found" error #130

wants to merge 7 commits into from

Conversation

douglascayers
Copy link

Fixes #128

Changes

  • In lib/util.js when determining the modified files from a push event, now uses octokit's getCommit method instead of compareCommitsWithBasehead to avoid errors when the push event does not include a "before" commit and only an "after" commit.
  • The getCommit method only includes files that were part of the commit, so it naturally identifies the files that would be part of the diff when compared to the immediately prior commit.
  • In tests/util.test.js, pulls test token from environment variable TEST_GITHUB_TOKEN to mitigate accidentally committing it (like I did in my prior PR)
  • This PR includes result of running npm run all

Tests

image

Copy link
Member

@adangel adangel left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Using simply getCommit instead of compareCommitsWithBasehead likely will miss files from other commits in the same push.

Can you have a look at this?

lib/util.js Outdated
Comment on lines 143 to 145
const compareResponse = await octokit.rest.repos.compareCommitsWithBasehead({
const commitResponse = await octokit.rest.repos.getCommit({
...context.repo,
basehead: `${eventData.before}...${eventData.after}`,
ref: eventData.after,
Copy link
Member

Choose a reason for hiding this comment

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

This change will now only consider the head commit of the commits that have been pushed. If multiple commits have been pushed at once, then the file list won't be complete.

The push event is described here: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
The sample payload seems to be for a tag, that has been pushed - interestingly it also has before=00000.... That means, the same problem (#128) should occur for if a tag has been pushed.

The payload also contains an array commits[] - in the example it's empty. I guess this is because it's a tag, that has been pushed. But for usual pushes, that should contain all the commits that are contained in the push. If there is no previous commit (before=0000....) we maybe need to loop through the commits and request each commit with your suggested method (getCommit).

Also there are other interesting flags in the push event: e.g. if a branch/tag is deleted, we would also get a push event and likely fail to determine the modified files.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the clarification on the push event payload. I misunderstood and thought the before was simply the immediate prior commit to what after represented. But it seems GitHub webhooks may be trying to be efficient by collapsing multiple commits into a single payload.

I'll reconsider my approach for handling the specific instance when before=0000.... per your recommendations.

Comment on lines 16 to 22
// Personal Access Token to perform tests with.
// https://github.com/settings/tokens/new
const token = process.env['TEST_GITHUB_TOKEN'];
if (!token) {
throw new Error('Set `export TEST_GITHUB_TOKEN="<your token>"` to run tests');
}

Copy link
Member

Choose a reason for hiding this comment

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

This change will break the CI build - as there is no env var "TEST_GITHUB_TOKEN" defined.
For the unit tests, no real token is required (hence I used my_test_token), because all the requests to the github api are mocked using nock. Since you added a new API call, we likely just need to mock this as well.

It seems, you updated the mock - so I guess this change here is not needed anymore.

Copy link
Author

Choose a reason for hiding this comment

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

Ah, understood. I'll revert this change. Thanks

@@ -294,16 +301,16 @@ describe('pmd-github-action-util', function () {
process.env['GITHUB_EVENT_NAME'] = 'push';
process.env['GITHUB_EVENT_PATH'] = __dirname + '/data/push-event-data.json';
nock('https://api.github.com')
.get('/repos/pmd/pmd-github-action-tests/compare/44c1557134c7dbaf46ecdf796fb871c8df8989e4...8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=1')
.get('/repos/pmd/pmd-github-action-tests/commits/8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=1')
.replyWithFile(200, __dirname + '/data/compare-files-page1.json', {
Copy link
Member

Choose a reason for hiding this comment

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

Please also rename this file "compare-files-page1.json" and update the contents - it's now not anymore the response from compare, but from commits, which potentially looks different.

You can just look at https://api.github.com/repos/pmd/pmd-github-action-tests/commits/8a7a25638d8ca5207cc824dea9571325b243c6a1?per_page=30&page=1 to get the json.

Note: I've simplified the original response for compare back then to make it smaller and contain only the essential parts - and modified the files part to contain the data I needed for tests.

@douglascayers douglascayers marked this pull request as draft September 26, 2022 02:16
@douglascayers douglascayers closed this by deleting the head repository May 15, 2023
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.

"Error: Not Found" if push event's before commit is 0000000000000000000000000000000000000000
2 participants