vcssync: add utility function for obtaining a github3 client (
bug 1358312); r?glob
We currently don't have test coverage of GitHub API requests. I'd like
to change that.
The first step is to create a function for obtaining a github3 client.
This will give us a central point from which we can inject special
behavior when running from tests.
While there is only one place where we obtain a github3 client today,
this will change in the near future. And the code will be sufficiently
complex to warrant a standalone function.
As part of this, I also snuck in a change to how the client is
constructed. ``github3.login()`` is equivalent to the new code,
at least for the usage we currently care about.
MozReview-Commit-ID: BZb26LeQPxk
--- a/vcssync/mozvcssync/gitrewrite/__init__.py
+++ b/vcssync/mozvcssync/gitrewrite/__init__.py
@@ -8,16 +8,21 @@ import collections
import json
import os
import re
import stat
import uuid
import github3.pulls
+from ..util import (
+ get_github_client,
+)
+
+
class RewriteError(Exception):
"""Represents an error that occurred during rewriting."""
def prune_directories(object_store, tree_id, directories):
"""Remove directories from a tree, writing the new trees to the store.
An existing Git Tree object defined by ``tree_id`` will be examined for
@@ -351,20 +356,17 @@ def commit_metadata_rewriter(
on merge commits. By default, the author of the merge commit is used.
"""
if committer_action not in ('keep', 'use-author', 'use-committer'):
raise ValueError('committer_action must be one of keep, use-author, '
'or use-committer')
author_map = author_map or {}
- github_client = None
- if github_token:
- github_client = github3.login(token=github_token)
-
+ github_client = get_github_client(github_token) if github_token else None
github_org, github_repo = None, None
github_cache_dir = os.path.join(repo.path, 'github-cache')
if source_repo and source_repo.startswith(b'https://github.com/'):
orgrepo = source_repo[len(b'https://github.com/'):]
github_org, github_repo = orgrepo.split(b'/')
if github_client and github_repo and not os.path.exists(github_cache_dir):
--- a/vcssync/mozvcssync/util.py
+++ b/vcssync/mozvcssync/util.py
@@ -1,16 +1,17 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import, unicode_literals
import pipes
+import github3
import hglib
def run_hg(logger, client, args):
"""Run a Mercurial command through hgclient and log output."""
logger.warn('executing: hg %s' % ' '.join(map(pipes.quote, args)))
out = hglib.util.BytesIO()
@@ -20,8 +21,17 @@ def run_hg(logger, client, args):
out_channels = {b'o': write, b'e': write}
ret = client.runcommand(args, {}, out_channels)
if ret:
raise hglib.error.CommandError(args, ret, out.getvalue(), b'')
return out.getvalue()
+
+
+def get_github_client(token):
+ """Obtain a github3 client using an API token for authentication."""
+
+ gh = github3.GitHub()
+ gh.login(token=token)
+
+ return gh