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

Support full GitHub app authentication #1986

Merged

Conversation

dblanchette
Copy link
Contributor

This adds support for GitHub App authentication using the main class which is simpler for users (see #1855)

import Github

github_client = Github(app_id=MY_APP_ID, app_private_key=MY_PRIVATE_KEY)

Also, the credentials are refreshed when needed (tokens lasts for one hour). This should happen transparently to the users, as the token expiration is checked at every call.

Caveats and other notes:

  • Uses the first found installation for an app. This means that it only works in the context of private apps
  • I'm not sure how to add tests correctly and there does not seem to be any for GithubIntegration, so I skipped that part. Manual testing works and existing tests are not broken
  • I could not resist fixing some type stubs along the way

@codecov-commenter
Copy link

codecov-commenter commented Jun 30, 2021

Codecov Report

Base: 98.75% // Head: 98.77% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (38fb3cd) compared to base (7cf3dfc).
Patch coverage: 95.86% of modified lines in pull request are covered.

❗ Current head 38fb3cd differs from pull request most recent head a7ca0cb. Consider uploading reports for the commit a7ca0cb to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1986      +/-   ##
==========================================
+ Coverage   98.75%   98.77%   +0.01%     
==========================================
  Files         115      116       +1     
  Lines       11595    11669      +74     
==========================================
+ Hits        11451    11526      +75     
+ Misses        144      143       -1     
Impacted Files Coverage Δ
github/Requester.py 97.46% <90.62%> (-0.69%) ⬇️
github/GithubIntegration.py 96.87% <96.87%> (ø)
github/Consts.py 100.00% <100.00%> (ø)
github/InstallationAuthorization.py 92.10% <100.00%> (-0.21%) ⬇️
github/MainClass.py 99.23% <100.00%> (+2.29%) ⬆️
github/__init__.py 100.00% <100.00%> (ø)
github/GitRelease.py 99.35% <0.00%> (-0.03%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor Author

@dblanchette dblanchette left a comment

Choose a reason for hiding this comment

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

Added some comments for easier reviewing.

@@ -125,3 +125,12 @@

# https://developer.github.com/changes/2018-10-16-deployments-environments-states-and-auto-inactive-updates/
deploymentStatusEnhancementsPreview = "application/vnd.github.flash-preview+json"


DEFAULT_BASE_URL = "https://api.github.com"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved those here since they are used in 2 files now (MainClass and GithubIntegration)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Comparing this is really difficult. Would have been better to do the refactoring (without changes) in a separate PR and base this PR on that. Then we would see only changes, not moving unchanged code around.

@@ -38,7 +38,7 @@ class GithubException(Exception):
Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.
"""

def __init__(self, status, data, headers):
def __init__(self, status, data, headers=None):
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 had a 404 while testing and had an exception because the headers parameters was not passed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There seems to be a fix there for the same issue: #2079

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think fixing the calling code as in #2079 is better than changing the signature.

@@ -0,0 +1,140 @@
import time
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved from MainClass, it's already big as it is and that does not really belong there.

status=response.status_code, data=response.text
)

@deprecated.deprecated("Use get_repo_installation")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know if that does not suit you. I feel that this call is badly named

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 the existing call? It should probably also state it's deprecated in the docstring. Also, it should mention the class, not just the bare function.

def get_repo_installation(self, owner, repo):
return self.get_installation(owner, repo)

def get_installations(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the part I added in that file. Users having a private app with more than one installations can use this call as the main class always uses the first found installation.

@@ -304,6 +310,9 @@ def __init__(
):
self._initializeDebugFeature()

self.__installation_authorization = None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The heart of the changes I made is here!

@maxdanilov
Copy link

also very interested in seeing this implemented, are there any updates on this feature?

@simkimsia
Copy link
Contributor

Will this be integrated?

@jonapich
Copy link

@s-t-e-v-e-n-k sorry to ping you directly; there hasn't been any feedback from the maintainers on this PR for close to a month now and I'm not sure who's in charge. What can we do to speed up things and help you out?

@tscully49
Copy link

Would also really like to see this feature! How can we help?

@stale
Copy link

stale bot commented Jan 8, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 8, 2022
@vitalii94lukianenko
Copy link

Hi, Could you merge this PR?

@stale stale bot removed the stale label Jan 10, 2022
@ThomasJRyan
Copy link

Would definitely appreciate this feature being merged in

github/MainClass.pyi Outdated Show resolved Hide resolved
@@ -38,7 +38,7 @@ class GithubException(Exception):
Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.
"""

def __init__(self, status, data, headers):
def __init__(self, status, data, headers=None):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think fixing the calling code as in #2079 is better than changing the signature.

@dblanchette
Copy link
Contributor Author

Thanks for the review! I made the requested changes.

@dblanchette
Copy link
Contributor Author

@EnricoMi @ThomasJRyan Anything else I should do?

github/ApplicationOAuth.pyi Outdated Show resolved Hide resolved
github/Requester.py Show resolved Hide resolved
github/CheckSuite.pyi Outdated Show resolved Hide resolved
github/Commit.pyi Outdated Show resolved Hide resolved
@dblanchette
Copy link
Contributor Author

I went and look at all the files I changed to make sure there were no default values left in pyi. Crossing my fingers that everything is ok now 🤞

@dblanchette
Copy link
Contributor Author

I hate to ask again, but is it all good?

I'm looking forward to go back to using the official library instead of our fork.

Thanks for your work maintaining this library!

@dblanchette
Copy link
Contributor Author

Now fails tests with E AttributeError: module 'github' has no attribute 'AppAuthentication'

That is fixed. I must say I find the imports in this project to be done in a peculiar fashion.

@dblanchette
Copy link
Contributor Author

Comparing this is really difficult. Would have been better to do the refactoring (without changes) in a separate PR and base this PR on that. Then we would see only changes, not moving unchanged code around.

Agreed. I tend to go all out in refactoring as you've seen with the unrelated changes. I'll know to do that in the future.

Thank you both for the reviews!

@dblanchette dblanchette requested review from s-t-e-v-e-n-k and EnricoMi and removed request for s-t-e-v-e-n-k and EnricoMi February 1, 2023 16:23
github/AppAuthentication.py Outdated Show resolved Hide resolved
github/MainClass.py Outdated Show resolved Hide resolved
@dblanchette
Copy link
Contributor Author

All good now? I'm excited to see this release :)

@s-t-e-v-e-n-k s-t-e-v-e-n-k merged commit 5e27c10 into PyGithub:master Feb 6, 2023
@EnricoMi
Copy link
Collaborator

@dblanchette can you please take a look at #2420 / #2425?

@EnricoMi
Copy link
Collaborator

@dblanchette another issue likely being introduced by this PR: #2431

@EnricoMi
Copy link
Collaborator

Another issue: #2432

@EnricoMi
Copy link
Collaborator

EnricoMi commented Mar 1, 2023

This is quite some fallout, another issue reported: #2436

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.

None yet