def get_access_token(): if GITHUB_ACCESS_TOKEN: return GITHUB_ACCESS_TOKEN else: priv_key = open(GITHUB_INTEGRATION_PRIVATE_KEY, "r").read() integration = GithubIntegration(GITHUB_APP_ID, priv_key) return integration.get_access_token(GITHUB_INTEGRATION_ID).token
def get_installation_token(owner, repo, integration_id, private_key): integration = GithubIntegration(integration_id=int(integration_id), private_key=private_key) installation = integration.get_installation(owner, repo) return integration.get_access_token(installation.id.value).token
def get_app_token(): GITHUB_PRIVATE_KEY = open(config.GITHUB_PRIVATE_KEY).read() integration = GithubIntegration(config.APP_ID, GITHUB_PRIVATE_KEY, config.GITHUB_BASE_URL) token = integration.get_access_token(config.INSTALLATION_ID) print(token) return (token.token)
def test_get_installation(self): from github import GithubIntegration integr = GithubIntegration("11111", private_key) inst = integr.get_installation("foo", "bar") self.assertEqual( self.get_mock.calls[0][0], ('https://api.github.com/repos/foo/bar/installation', )) self.assertEqual(inst.id.value, 111111)
def test_get_installation_custom_base_url(self): from github import GithubIntegration integr = GithubIntegration("11111", private_key, base_url='https://corp.com/v3') inst = integr.get_installation("foo", "bar") self.assertEqual(self.get_mock.calls[0][0], ('https://corp.com/v3/repos/foo/bar/installation', )) self.assertEqual(inst.id.value, 111111)
def testGetAccessToken(self): from github import GithubIntegration integration = GithubIntegration(25216, private_key) auth_obj = integration.get_access_token(664281) self.assertEqual( self.mock.args[0], "https://api.github.com/app/installations/664281/access_tokens") self.assertEqual(auth_obj.token, "v1.ce63424bc55028318325caac4f4c3a5378ca0038") self.assertEqual(auth_obj.expires_at, datetime.datetime(2019, 2, 13, 11, 10, 38))
def get_github(): with open(GH_PRIVATE_KEY_PATH) as in_file: gh_integration = GithubIntegration( integration_id=47055, private_key=in_file.read() ) installation = gh_integration.get_installation("ooni", "probe") access_token = gh_integration.get_access_token( installation_id=installation.id.value ) return Github(login_or_token=access_token.token)
def testCreateJWT(self): from github import GithubIntegration integration = GithubIntegration(25216, private_key) token = integration.create_jwt() payload = jwt.decode( token, key=public_key, algorithms=["RS256"], options={"verify_exp": False}, ) self.assertDictEqual( payload, {"iat": 1550055331, "exp": 1550055391, "iss": 25216} )
def testGetAccessToken(self): from github import GithubIntegration integration = GithubIntegration(25216, private_key) auth_obj = integration.get_access_token(664281) self.assertEqual( self.mock.args[0], "https://api.github.com/app/installations/664281/access_tokens" ) self.assertEqual( auth_obj.token, "v1.ce63424bc55028318325caac4f4c3a5378ca0038" ) self.assertEqual( auth_obj.expires_at, datetime.datetime(2019, 2, 13, 11, 10, 38) )
def github_instance(self): if not self._github_instance: if self.service.github_app_id and self.service.github_app_private_key: integration = GithubIntegration( self.service.github_app_id, self.service.github_app_private_key) inst_id = integration.get_installation(self.namespace, self.repo).id.value inst_auth = integration.get_access_token(inst_id) self._github_instance = github.Github( login_or_token=inst_auth.token) else: self._github_instance = self.service.github return self._github_instance
def token(user, repo): with open(os.path.join(dir_path, 'app_id.txt'), 'r') as fh: app_id = int(fh.read()) with open(os.path.join(dir_path, 'dev-desktop.private-key.pem'), 'rb') as fh: private_key = fh.read() integration = GithubIntegration(app_id, private_key) installation = integration.get_installation(user, repo) auth = integration.get_access_token(installation.id) return auth.token
def testCreateJWT(self): from github import GithubIntegration integration = GithubIntegration(25216, private_key) token = integration.create_jwt() payload = jwt.decode( token, key=public_key, algorithm="RS256", options={'verify_exp': False}, ) self.assertDictEqual(payload, { 'iat': 1550055331, 'exp': 1550055391, 'iss': 25216 })
def __init__(self, token, repo=None): super().__init__() self.git = GithubIntegration(token) self.repo = None if (repo): self.setRepo(repo) self.user = self.git.get_user()
def testCreateJWT(self): from github import GithubIntegration integration = GithubIntegration(25216, private_key) token = integration.create_jwt() payload = jwt.decode( token, key=public_key, algorithm="RS256", options={'verify_exp': False}, ) self.assertDictEqual( payload, { 'iat': 1550055331, 'exp': 1550055391, 'iss': 25216 } )
def github_instance(self): if not self._github_instance: if self.service.github_app_id and self.service.github_app_private_key: integration = GithubIntegration( self.service.github_app_id, self.service.github_app_private_key) inst_id = integration.get_installation(self.namespace, self.repo).id.value if not inst_id: raise OgrException( f"No installation ID provided for {self.namespace}/{self.repo}: " "please make sure that you provided correct credentials of your GitHub app." ) inst_auth = integration.get_access_token(inst_id) self._github_instance = github.Github( login_or_token=inst_auth.token) else: self._github_instance = self.service.github return self._github_instance
def test_get_app_installation_id(settings, mocker, mock_rsa_key, app_id, install_id): """Should return the installation id based on matching app id returned in a response from Github""" settings.GITHUB_APP_ID = app_id settings.GITHUB_APP_PRIVATE_KEY = mock_rsa_key mock_get = mocker.patch("content_sync.apis.github.requests.get") mock_get.return_value.json.return_value = json.loads( GITHUB_APP_INSTALLATIONS) installation_id = get_app_installation_id( GithubIntegration(settings.GITHUB_APP_ID, mock_rsa_key)) assert installation_id == install_id
def get_token(): """ Get a github token for requests """ if settings.GITHUB_APP_ID and settings.GITHUB_APP_PRIVATE_KEY: app = GithubIntegration( settings.GITHUB_APP_ID, default_backend().load_pem_private_key( settings.GITHUB_APP_PRIVATE_KEY, None ), **( {"base_url": settings.GIT_API_URL} if settings.GIT_API_URL is not None else {} ), ) return app.get_access_token(get_app_installation_id(app)).token elif settings.GIT_TOKEN: return settings.GIT_TOKEN else: raise ImproperlyConfigured( "Missing Github settings, a token or app id and private key are required" )
def create_app(): configure_sentry() app = Flask(__name__) app.config.from_envvar("TOKMAN_CONFIG") log_level = os.getenv("LOG_LEVEL", "info") log_level = getattr(logging, log_level.upper()) logging.basicConfig(level=log_level) private_key = Path(app.config["GITHUB_APP_PRIVATE_KEY"]).read_text() app_id = int(app.config["GITHUB_APP_ID"]) app.github_integration = GithubIntegration(app_id, private_key) api.init_app(app) db.init_app(app) return app
def test_get_installation(self): from github import GithubIntegration integr = GithubIntegration("11111", private_key) inst = integr.get_installation("foo", "bar") self.assertEqual(inst.id.value, 111111)
"r") as myfile: APPID = myfile.read().strip() with open( os.environ['HOME'] + "/.ssh/github.app.sphenix-jenkins-ci.installationid", "r") as myfile: INSTALLATIONID = myfile.read().strip() with open( os.environ['HOME'] + "/.ssh/github.app.sphenix-jenkins-ci.private-key.pem", 'rb') as fh: signing_key = fh.read() print( f"Authentication with private key for app {APPID} installation {INSTALLATIONID} ..." ) integration = GithubIntegration(APPID, signing_key) jwt_token = integration.create_jwt() access_obj = integration.get_access_token(INSTALLATIONID) # pprint.pprint(access_obj.__dict__); ######################### # Talk to GitHub ######################### gh = Github(login_or_token=access_obj.token) # pprint.pprint(gh.__dict__); org = gh.get_organization(checkrun_organziation) repo = org.get_repo(checkrun_repo)
def get_github_integration(app_id, key_path): private_key = get_app_key(key_path) return GithubIntegration(app_id, private_key)
import discord from os import urandom from github import Github, GithubIntegration from src.defines import TOKEN, GUILD, TARGET_CHANNEL, GITHUB_APP_ID, GITHUB_CLIENT_ID from src.input_proccesors.vote import Vote from src.input_proccesors.suggestion import Suggestion github_integration = GithubIntegration(GITHUB_APP_ID, open('angry-villager.2020-10-03.private-key.pem', 'r').read()) installation = github_integration.get_installation('OutlawByteStudios', 'KingdomsDiscordBot') token = github_integration.create_jwt() github_client = Github('v1.e9827ef3291f575ac58d4def4ab428afc868806e') client = discord.Client() # Suggestions project = 5594461 # Suggestions column = 11079851 # Project Plan project id = 5594457 project_kingdoms_suggestions_repo = github_client.get_repo( 'OutlawByteStudios/KingdomsDiscordBot') suggestion_column = github_client.get_project_column(11079851) suggestion_label = project_kingdoms_suggestions_repo.get_label('suggestion') vote = Vote(project_kingdoms_suggestions_repo) suggestion = Suggestion(project_kingdoms_suggestions_repo, suggestion_column, suggestion_label) print(f'Repo has {project_kingdoms_suggestions_repo.open_issues} open issues')
import os import base64 from github import GithubIntegration # Get the App id = int(os.getenv("GITHUB_APP_ID")) private_key = base64.b64decode(os.getenv("GITHUB_APP_PRIVATE_KEY")) app = GithubIntegration(id, private_key) # Get the installation owner = os.getenv("GITHUB_OWNER") repo = os.getenv("GITHUB_REPO") installation = app.get_installation(owner, repo) # Get an installation access token tok = app.get_access_token(installation.id) # Print the token to STDOUT print(tok.token)
def token(self): return GithubIntegration( self.meta.get("app_id"), self.private_key ).get_access_token(self.meta.get("installation_id"))