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 installation payload handling #1371

Merged
merged 4 commits into from
Feb 14, 2022
Merged

Conversation

gsmet
Copy link
Contributor

@gsmet gsmet commented Feb 2, 2022

Hey there,

Long time no see :).

The problem was initially reported in my Quarkus GitHub App project: quarkiverse/quarkus-github-app#253.

Parsing Installation event payloads with a connected GitHub instance (i.e. not an offline one) leads to:

Exception: java.lang.NullPointerException: Missing instance URL!
	at java.base/java.util.Objects.requireNonNull(Objects.java:247)
	at org.kohsuke.github.GHRepository.populate(GHRepository.java:3260)
	at org.kohsuke.github.GHEventPayload$Installation.lateBind(GHEventPayload.java:276)
	at org.kohsuke.github.GitHub.parseEventPayload(GitHub.java:944)

Installation payloads do not contain the URL of the repository so we cannot populate the data from there. You can see an example in the tests here:
https://github.com/hub4j/github-api/blob/main/src/test/resources/org/kohsuke/github/GHEventPayloadTest/installation.json#L45-L53

I think what I did is safe but better check the URL thing as I decided to always use a forged URL rather than the one provided in the JSON. I can do it in another way if you prefer and keep the JSON URL when it's provided but, given we already had a try/catch for a corner case, I thought we might as well always forge the URL.

The tests are not failing because we are testing all the payloads with an offline GitHub instance thus not populating the repositories. Maybe we should change that? But I'm not totally sure of the consequences of this change. So I thought I might as well ask.

Very motivated to get this change in as one of my users is blocked by this so eager to hear your feedback :).

In the installation payload, the repository doesn't contain a lot of
information and it especially does not contain the URL.

Tests are passing because we use an offline GitHub instance for testing
the payloads.
@@ -2978,7 +2977,7 @@ String getApiTailUrl(String tail) {
if (tail.length() > 0 && !tail.startsWith("/")) {
tail = '/' + tail;
}
return "/repos/" + getOwnerName() + "/" + name + tail;
return "/repos/" + full_name + tail;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FWIW, the owner is not provided either in the installation token, the only info we have is the full name.

throw e;
}
}
root().createRequest().withPreview(BAPTISTE).withPreview(NEBULA).withUrlPath(getApiTailUrl("")).fetchInto(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to simplify that but I can add a bunch of ifs if you prefer using the URL coming from the JSON when it is around.

Copy link
Member

Choose a reason for hiding this comment

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

No, this is functional and clean. 💯

@codecov
Copy link

codecov bot commented Feb 2, 2022

Codecov Report

Merging #1371 (c87903e) into main (0e41ccb) will decrease coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1371      +/-   ##
============================================
- Coverage     78.18%   78.10%   -0.09%     
  Complexity     2045     2045              
============================================
  Files           200      200              
  Lines          6284     6270      -14     
  Branches        356      355       -1     
============================================
- Hits           4913     4897      -16     
- Misses         1162     1165       +3     
+ Partials        209      208       -1     
Impacted Files Coverage Δ
...c/main/java/org/kohsuke/github/GHEventPayload.java 80.41% <100.00%> (ø)
src/main/java/org/kohsuke/github/GHRepository.java 68.04% <100.00%> (-0.39%) ⬇️
...c/main/java/org/kohsuke/github/GitHubResponse.java 81.39% <0.00%> (-9.31%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0e41ccb...c87903e. Read the comment docs.

@gsmachado
Copy link

I can confirm that this PR fixes the issue on my GitHub app. 👍 💪 💯

@gsmet
Copy link
Contributor Author

gsmet commented Feb 3, 2022

@bitwiseman I updated the test to make it a connected one so it's now properly tested.

@@ -630,7 +630,7 @@ public void checkRunEvent() throws Exception {
GHEventPayload.CheckRun.class);
final GHCheckRun checkRun2 = verifyBasicCheckRunEvent(event2);

int expectedRequestCount = mockGitHub.isUseProxy() ? 3 : 2;
int expectedRequestCount = 2;
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 refreshed the snapshots and this is no longer needed.

@bitwiseman
Copy link
Member

Sorry for the slow response. Looking now.

@bitwiseman bitwiseman closed this Feb 14, 2022
@bitwiseman bitwiseman reopened this Feb 14, 2022
Copy link
Member

@bitwiseman bitwiseman left a comment

Choose a reason for hiding this comment

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

Looks good.

@bitwiseman bitwiseman merged commit 9b164ca into hub4j:main Feb 14, 2022
@gsmet
Copy link
Contributor Author

gsmet commented Feb 25, 2022

Hey @bitwiseman , any chance we could have a release with this fix? Thanks!

@bitwiseman
Copy link
Member

@gsmet Released v1.302

@gsmet
Copy link
Contributor Author

gsmet commented Mar 2, 2022

@bitwiseman thanks!

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.

3 participants