Python github.com() Examples

The following are 18 code examples of github.com(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module github , or try the search function .
Example #1
Source File: gh.py    From github-stats with MIT License 6 votes vote down vote up
def gh_admin_top():
  stars = util.param('stars', int) or 10000
  page = util.param('page', int) or 1
  per_page = util.param('per_page', int) or 100
  # TODO: fix formatting
  result = urlfetch.fetch('https://api.github.com/search/repositories?q=stars:>=%s&sort=stars&order=asc&page=%d&per_page=%d' % (stars, page, per_page))
  if result.status_code == 200:
    repos = json.loads(result.content)
  else:
    flask.abort(result.status_code)

  for repo in repos['items']:
    account = repo['owner']
    account_db = model.Account.get_or_insert(
      account['login'],
      avatar_url=account['avatar_url'].split('?')[0],
      email=account['email'] if 'email' in account else '',
      name=account['login'],
      followers=account['followers'] if 'followers' in account else 0,
      organization=account['type'] == 'Organization',
      username=account['login'],
    )

  return 'OK %d of %d' % (len(repos['items']), repos['total_count']) 
Example #2
Source File: server.py    From anonymous_github with MIT License 6 votes vote down vote up
def clean_github_repository(repo):
    """
    get the username/repository from a Github url
    :param repo:str the Github url of the repository
    :return: username/repository
    """
    if repo is None:
        return None
    repo = repo.replace("http://github.com/", "") \
        .replace("https://github.com/", "")
    if repo[-1] == '/':
        repo = repo[:-1]
    split_repo = repo.split("/")
    (username, repository) = split_repo[0:2]
    branch = "master"
    if len(split_repo) > 2:
        if split_repo[2] == "tree":
            branch = split_repo[3]
    return username, repository, branch 
Example #3
Source File: test_gpr.py    From git-pull-request with Apache License 2.0 6 votes vote down vote up
def test_git_clone_url(self):
        expected = gpr.RepositoryId("github", "example.com", "jd", "git-pull-request")

        self.assertEqual(
            expected,
            gpr.get_repository_id_from_url("https://example.com/jd/git-pull-request"),
        )

        self.assertEqual(
            expected,
            gpr.get_repository_id_from_url(
                "https://example.com/jd/git-pull-request.git"
            ),
        )

        self.assertEqual(
            expected,
            gpr.get_repository_id_from_url("https://example.com/jd/git-pull-request/"),
        ) 
Example #4
Source File: test_gpr.py    From git-pull-request with Apache License 2.0 6 votes vote down vote up
def test_no_message(self):
        e = github.GithubException(
            422,
            {
                "message": "Validation Failed",
                "documentation_url": "https://developer.github.com/v3/pulls/#create-a-pull-request",
                "errors": [
                    {"resource": "PullRequest", "field": "head", "code": "invalid"}
                ],
            },
        )
        self.assertEqual(
            "Unable to create pull request: Validation Failed (422)\n\n"
            "Check "
            "https://developer.github.com/v3/pulls/#create-a-pull-request "
            "for more information.",
            gpr._format_github_exception("create pull request", e),
        ) 
Example #5
Source File: test_gpr.py    From git-pull-request with Apache License 2.0 6 votes vote down vote up
def test_issue_12(self):
        e = github.GithubException(
            422,
            {
                u"documentation_url": u"https://developer.github.com/v3/pulls/#create-a-pull-request",
                u"message": u"Validation Failed",
                u"errors": [
                    {
                        u"message": u"No commits between issues-221 and issues-221",
                        u"code": u"custom",
                        u"resource": u"PullRequest",
                    }
                ],
            },
        )
        self.assertEqual(
            "Unable to create pull request: Validation Failed (422)\n"
            "No commits between issues-221 and issues-221\n"
            "Check "
            "https://developer.github.com/v3/pulls/#create-a-pull-request "
            "for more information.",
            gpr._format_github_exception("create pull request", e),
        ) 
Example #6
Source File: test_gpr.py    From git-pull-request with Apache License 2.0 6 votes vote down vote up
def test_git_remote_matching_url(self):
        self.assertEqual(
            "origin",
            gpr.git_remote_matching_url("https://github.com/jd/git-pull-request.git"),
        )
        self.assertEqual(
            "fork",
            gpr.git_remote_matching_url(
                "https://github.com/Flameeyes/git-pull-request.git"
            ),
        )
        self.assertEqual(
            "fork",
            gpr.git_remote_matching_url(
                "https://github.com/flameeyes/Git-Pull-Request.git"
            ),
        ) 
Example #7
Source File: tasks.py    From galaxy-integration-humblebundle with GNU General Public License v3.0 6 votes vote down vote up
def curr_ver(c, tag=None):
    """Refresh CURRENT_VERSION_FILE"""
    if tag is None:
        tag = get_repo().get_latest_release().tag_name
    content = {
        "tag_name": tag,
        "assets": []
    }
    for platform in ["Windows", "Mac"]:
        name = asset_name(tag, platform)
        content['assets'].append({
            "browser_download_url": f"https://github.com/UncleGoogle/galaxy-integration-humblebundle/releases/download/{tag}/{name}",
            "name": name
        })
    with open(CURRENT_VERSION_FILE, 'w') as f:
        json.dump(content, f, indent=4) 
Example #8
Source File: github.py    From conda-smithy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def gh_token():
    try:
        with open(
            os.path.expanduser("~/.conda-smithy/github.token"), "r"
        ) as fh:
            token = fh.read().strip()
        if not token:
            raise ValueError()
    except (IOError, ValueError):
        msg = (
            "No github token. Go to https://github.com/settings/tokens/new and generate\n"
            "a token with repo access. Put it in ~/.conda-smithy/github.token"
        )
        raise RuntimeError(msg)
    return token 
Example #9
Source File: test_gpr.py    From git-pull-request with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        self.tempdir = self.useFixture(fixtures.TempDir()).path
        os.chdir(self.tempdir)
        gpr._run_shell_command(["git", "init", "--quiet"])
        gpr._run_shell_command(
            [
                "git",
                "remote",
                "add",
                "origin",
                "https://github.com/jd/git-pull-request.git",
            ]
        )
        gpr._run_shell_command(
            [
                "git",
                "remote",
                "add",
                "fork",
                "git@github.com:Flameeyes/git-pull-request",
            ]
        )
        gpr._run_shell_command(
            ["git", "config", "branch.master.merge", "refs/heads/master"]
        )
        gpr._run_shell_command(["git", "config", "branch.master.remote", "origin"])
        gpr._run_shell_command(["git", "config", "user.name", "nobody"])
        gpr._run_shell_command(["git", "config", "user.email", "nobody@example.com"]) 
Example #10
Source File: git_downloader.py    From GithubDownloader with MIT License 5 votes vote down vote up
def download_files(args, g, repo_gen):
    file_counter = 0
    for repo in repo_gen:
        
        try:
            logging.info('Fetching repository: %s (id: %i)' % (repo.full_name, repo.id))
            tree = repo.get_git_tree('master', recursive=True)
            files_to_download = []
            for file in tree.tree:
                if fnmatch.fnmatch(file.path, args.wildcard):
                    files_to_download.append('https://github.com/%s/raw/master/%s' % (repo.full_name, file.path))
            for file in files_to_download:
                logging.info('Downloading %s' % file)
                file = quote(file)
                file_counter += 1
                filename = posixpath.basename(urlparse.urlsplit(file).path)
                output_path = os.path.join(args.output_dir, filename)
                if os.path.exists(output_path):
                    output_path += "-" + str(file_counter)
                try:
                    urlretrieve(file, output_path)
                except Exception:
                    logging.exception('Error downloading %s.' % file)
        except Exception:
            logging.exception('Error fetching repository.')

    args.yara_meta = os.path.join(args.output_dir, args.yara_meta)
    with open(args.yara_meta, 'w') as f:
        for i in os.listdir(args.output_dir):
            try:
                f.write("include \"" + i + "\"\n")
            except Exception:
                logging.exception('Couldn\'t write to %s.' % args.yara_meta) 
Example #11
Source File: git_downloader.py    From GithubDownloader with MIT License 5 votes vote down vote up
def file_repo_gen(repo_file, g):
    with open(repo_file, 'r') as f:
        for line in f:
            repo_str = line.rstrip().split('github.com/')[-1]
            yield g.get_repo(repo_str) 
Example #12
Source File: github_api.py    From plugin.git.browser with GNU General Public License v3.0 5 votes vote down vote up
def get_download_url(full_name, path):
	url = content_url % (full_name, default_branch, path)
	if github.test_url(url): return url
	# didn't work, need to get the branch name
	response = requests.get("https://api.github.com/repos/%s/branches" % full_name).json()
	for attempt in response:
		branch = attempt['name']
		url = content_url % (full_name, branch, path)
		if github.test_url(url): return url
	raise githubException('No available download link') 
Example #13
Source File: test_needs_revision.py    From ansibullbot with GNU General Public License v3.0 5 votes vote down vote up
def test_shipit_removes_needs_revision_multiple_users(self):
        """
        Ansibot should remove needs_revision if the same user that set it gave shipit afterwards.
        https://github.com/ansible/ansibullbot/issues/994
        """
        datafile = u'tests/fixtures/needs_revision/2_issue.yml'
        statusfile = u'tests/fixtures/needs_revision/0_prstatus.json'
        with mock.patch.multiple(IssueWrapper,
                               mergeable_state=mock.PropertyMock(return_value='clean'),
                               pullrequest_filepath_exists=mock.Mock(return_value=True)):
            with get_issue(datafile, statusfile) as iw:
                iw._merge_commits = []
                iw._committer_emails = [u'tsdmgz@domain.example']

                pullrequest = mock.Mock(spec_set=github.PullRequest.PullRequest)
                pullrequest.head.repo.__return_value__ = True
                iw._pr = pullrequest

                with open(u'tests/fixtures/needs_revision/1_reviews.json') as reviews:
                    iw._pr_reviews = json.load(reviews)
                    iw._history.merge_reviews(iw.reviews)

                self.meta[u'component_maintainers'] = [u'mkrizek', u'jctanner']
                facts = get_needs_revision_facts(AnsibleTriageMock(), iw, self.meta, ShippableRunsMock())

                self.assertTrue(facts[u'is_needs_revision']) 
Example #14
Source File: test_needs_revision.py    From ansibullbot with GNU General Public License v3.0 5 votes vote down vote up
def test_shipit_removes_needs_revision(self):
        """
        Ansibot should remove needs_revision if the same user that set it gave shipit afterwards.
        https://github.com/ansible/ansibullbot/issues/994
        """
        datafile = u'tests/fixtures/needs_revision/1_issue.yml'
        statusfile = u'tests/fixtures/needs_revision/0_prstatus.json'
        with mock.patch.multiple(IssueWrapper,
                               mergeable_state=mock.PropertyMock(return_value='clean'),
                               pullrequest_filepath_exists=mock.Mock(return_value=True)):
            with get_issue(datafile, statusfile) as iw:
                iw._merge_commits = []
                iw._committer_emails = [u'tsdmgz@domain.example']

                pullrequest = mock.Mock(spec_set=github.PullRequest.PullRequest)
                pullrequest.head.repo.__return_value__ = True
                iw._pr = pullrequest

                with open(u'tests/fixtures/needs_revision/1_reviews.json') as reviews:
                    iw._pr_reviews = json.load(reviews)
                    iw._history.merge_reviews(iw.reviews)

                self.meta[u'component_maintainers'] = [u'mkrizek']
                facts = get_needs_revision_facts(AnsibleTriageMock(), iw, self.meta, ShippableRunsMock())

                self.assertFalse(facts[u'is_needs_revision']) 
Example #15
Source File: fork_conda_forge.py    From build-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_shippable_project_id(token, project_full_name):
    params = {'sortBy': 'createdAt', 'sortOrder': '-1', 'projectFullNames': project_full_name}
    headers = {'Authorization': 'apiToken {}'.format(token)}
    url = 'https://api.shippable.com/projects'
    result = requests.get(url=url, params=params, headers=headers)
    n_results = len(result.json())
    if not n_results:
        raise RuntimeError('Could not find the activated repository. Make sure it exists on shippable.')
    elif n_results > 1:
        raise RuntimeError("Found multiple repositories, linking to the first one. This really shouldn't happen")
    # projectId seems like a short name
    # the real variable we need is id
    return result.json()[0]['id'] 
Example #16
Source File: fork_conda_forge.py    From build-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_shippable_token(token_dir):
    try:
        shippable_token_filename = (token_dir / 'shippable.token').expanduser()
        with open(shippable_token_filename, 'r') as fh:
            shippable_token = fh.read().strip()
        if not shippable_token:
            raise ValueError()
        return shippable_token
    except (IOError, ValueError):
        raise RuntimeError(
            'No shippable token found for archiconda on shippable. \n'
            'Go to http://docs.shippable.com/platform/api/api-tokens/\n'
            'and follow the instructions to get a token.\n'
            f'Put it in {shippable_token_filename}') 
Example #17
Source File: fork_conda_forge.py    From build-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_github_token(token_dir):
    try:
        github_token_filename = (token_dir / 'github.token').expanduser()
        with open(github_token_filename, 'r') as fh:
            github_token = fh.read().strip()
        if not github_token:
            raise ValueError()
        return github_token
    except (IOError, ValueError):
        raise RuntimeError(
            'No github token found for archiconda on Github. \n'
            'Go to https://github.com/settings/tokens/new and generate\n'
            f'a token with repo access. Put it in {github_token_filename}') 
Example #18
Source File: gh.py    From github-stats with MIT License 4 votes vote down vote up
def gh_account(username, repo=None):
  username_ = username.lower()
  account_db = model.Account.get_by_id(username_)

  if not account_db:
    g = github.Github(config.CONFIG_DB.github_username, config.CONFIG_DB.github_password)
    try:
      account = g.get_user(username_)
    except github.GithubException as error:
      return flask.abort(error.status)

    account_db = model.Account.get_or_insert(
        account.login,
        avatar_url=account.avatar_url.split('?')[0],
        email=account.email or '',
        followers=account.followers,
        joined=account.created_at,
        name=account.name or account.login,
        organization=account.type == 'Organization',
        public_repos=account.public_repos,
        username=account.login,
      )

  if account_db.username != username or repo:
    return flask.redirect(flask.url_for('gh_account', username=account_db.username))

  task.queue_account(account_db)
  repo_dbs, repo_cursor = account_db.get_repo_dbs()

  return flask.render_template(
      'account/view.html',
      html_class='gh-view',
      title='%s%s' % ('#%d - ' % account_db.rank if account_db.rank else '', account_db.name),
      description='https://github.com/' + account_db.username,
      image_url=account_db.avatar_url,
      canonical_url=flask.url_for('gh_account', username=username, _external=True),
      account_db=account_db,
      repo_dbs=repo_dbs,
      next_url=util.generate_next_url(repo_cursor),
      username=account_db.username,
    )


###############################################################################
# Cron Stuff
###############################################################################