Use configured github server in app mode

Currently when running as github app the urls for the api access are
hard coded to github.com. We need to use the configured github server
for this if we want to use apps for github enterprise.

Change-Id: I9b98d45f37d7cb6842c95cdce6435b86d33e924d
This commit is contained in:
Tobias Henkel 2017-12-15 16:22:12 +01:00
parent 8904f17c32
commit 467bf8be7d
1 changed files with 15 additions and 6 deletions

View File

@ -38,10 +38,8 @@ from zuul.model import Ref, Branch, Tag, Project
from zuul.exceptions import MergeFailure
from zuul.driver.github.githubmodel import PullRequest, GithubTriggerEvent
ACCESS_TOKEN_URL = 'https://api.github.com/installations/%s/access_tokens'
GITHUB_BASE_URL = 'https://api.github.com'
PREVIEW_JSON_ACCEPT = 'application/vnd.github.machine-man-preview+json'
INSTALLATIONS_URL = 'https://api.github.com/app/installations'
REPOS_URL = 'https://api.github.com/installation/repositories'
def _sign_request(body, secret):
@ -415,6 +413,11 @@ class GithubConnection(BaseConnection):
self.source = driver.getSource(self)
self.event_queue = queue.Queue()
if self.server == 'github.com':
self.base_url = GITHUB_BASE_URL
else:
self.base_url = 'https://%s/api/v3' % self.server
# ssl verification must default to true
verify_ssl = self.connection_config.get('verify_ssl', 'true')
self.verify_ssl = True
@ -546,7 +549,10 @@ class GithubConnection(BaseConnection):
if ((not expiry) or (not token) or (now >= expiry)):
headers = self._get_app_auth_headers()
url = ACCESS_TOKEN_URL % installation_id
url = "%s/installations/%s/access_tokens" % (self.base_url,
installation_id)
json_data = {'user_id': user_id} if user_id else None
response = requests.post(url, headers=headers, json=json_data)
@ -568,7 +574,8 @@ class GithubConnection(BaseConnection):
if not self.app_id:
return
url = INSTALLATIONS_URL
url = '%s/app/installations' % self.base_url
headers = self._get_app_auth_headers()
self.log.debug("Fetching installations for GitHub app")
response = requests.get(url, headers=headers)
@ -581,7 +588,9 @@ class GithubConnection(BaseConnection):
token = self._get_installation_key(project=None, inst_id=inst_id)
headers = {'Accept': PREVIEW_JSON_ACCEPT,
'Authorization': 'token %s' % token}
url = REPOS_URL
url = '%s/installation/repositories' % self.base_url
self.log.debug("Fetching repos for install %s" % inst_id)
response = requests.get(url, headers=headers)
response.raise_for_status()