Exemplo n.º 1
0
def create_git_repo_from_template(user, new_owner, new_repo, template_owner, template_repo, private=True):
    """Creates a new github repository from a template
    
    Args:
        user (AuthenticatedUser): user object
        new_owner (string): owner of the new repository
        new_repo (string): name of the new repository
        template_owner (string): owner of the template repository
        template_repo (string): name of the template repository
        private (boolean): if the repo is private (default True)

    Returns:
        The new repository object
    """
    post_parameters = {
        "owner": new_owner,
        "name": new_repo,
        "private": private
    }
    headers, data = user._requester.requestJsonAndCheck(
        "POST",
         "/repos/" + template_owner + "/" + template_repo + "/generate",
         headers = {"Accept":"application/vnd.github.baptiste-preview+json"},   #required because templating api in preview
        input=post_parameters
    )
    return Repository.Repository(
        user._requester, headers, data, completed=True
    )
Exemplo n.º 2
0
def open_pr_in_parent():
    """
    Opens a PR in the parent repository, including changes in the submodules.

    Needs to:
    1. Create a new branch from default branch in llp-catalog
    2. Update the git tag reference for this submodule
    3. Commit the change to the branch
    4. Put up a PR in llp-catalog
    """
    changes = {}

    if GH_CHANGES_PATTERN and GH_CURRENT_RELEASE_TAG:
        child_repo = Repository(
            GH_OWNER,
            GH_CURRENT_REPO_NAME,
            GH_CURRENT_REPO_BASE_BRANCH,
            github_api_token=GH_TOKEN,
        )
        changes = _changes_since_last_release(child_repo)
        if not changes:
            # break out early. No changes detected between releases.
            print(
                f"No changes detected. No PR will be opened in {GH_PARENT_REPO}."
            )
            return

    repo = Repository(
        GH_OWNER,
        GH_PARENT_REPO,
        GH_PARENT_REPO_DEFAULT_BRANCH,
        github_api_token=GH_TOKEN,
    )
    repo.set_author("{given_name} {family_name}".format(**GH_USER),
                    GH_USER["email"])

    default_commit = repo.last_commit()

    # create the new branch
    target_branch_name = f"automated/update-submodule/{GH_CURRENT_REPO_NAME}"

    # open a new branch, if one does not already exist for the current repository.
    try:
        repo.get_branch(target_branch_name)
    except:
        repo.create_branch(target_branch_name, default_commit)
        print(f"Branch {target_branch_name} created.")
    else:
        print(f"Branch {target_branch_name} already exists, updating...")

    # update the submodule reference hash
    tree_sha = repo.update_submodule(default_commit, GH_CURRENT_REPO_COMMIT,
                                     f"submodules/{GH_CURRENT_REPO_NAME}")

    # add commit
    commit_sha = repo.create_commit(
        f"Updating submodule {GH_CURRENT_REPO_NAME} ref to {_get_readable_reference()}",
        tree_sha,
        default_commit,
    )

    # update the branch HEAD
    repo.update_branch(commit_sha, target_branch_name, force=True)

    # check if PR already exists
    branch_pr = repo.get_pull_request(target_branch_name)

    if branch_pr:
        # update existing PR
        pr_number = branch_pr["number"]
        repo.update_pull_request(pr_number, _get_title(), _get_body(changes))
        print(f"Pull request updated in {GH_PARENT_REPO}")

    else:
        # open a PR
        repo.create_pull_request(target_branch_name, _get_title(),
                                 _get_body(changes))
        print(f"Pull request opened in {GH_PARENT_REPO}")
Exemplo n.º 3
0
from github import Organization, Repository

if __name__ == "__main__":
    # let's take an example of Google
    org = Organization('google')
    top_repos = org.top_repos_by_fork(5)

    if not top_repos:
        print "Sorry! This organization has no public repos."
        exit()

    print "REPOSITORY | NO_FORKS \n"
    for repo in top_repos:
        current_repo = Repository(repo)
        print repo, top_repos[repo]

        print "contributor_name | no_commits"
        contributor_commits = current_repo.top_contributors_by_commits(3)
        for contributor in contributor_commits:
            print contributor, contributor_commits[contributor]

        print "\n"
Exemplo n.º 4
0
 def test_results_for_repo(self, mock_github):
     client = RepoHistory()
     mock_github.get_pulls = MagicMock(return_value=Repository.Repository(
         Requester, [], pr, completed=True))
     results = client.handle("alnutile/blog")
     self.assertIsNotNone(results)