def _is_branch_exists(self, repo: Repository,
                       branch: str) -> Optional[str]:
     try:
         b = repo.get_branch(branch)
         return b.commit.sha
     except GithubException:
         return None
Exemplo n.º 2
0
 def _scan_repository(self, results: ScanResults, user: NamedUser, org: Organization, repo: Repository) -> None:
     try:
         branch = repo.get_branch(branch="master")
         tree = repo.get_git_tree(branch.commit.sha, recursive=True).tree
     except GithubException as e:
         print("[W] {}/{} - {}".format(org.login, repo.name, str(e)))
         # Skip if no master branch
         return
     filelist = [x.path for x in tree]
     for element in tree:
         self._scan_file(results, user, org, repo, branch.commit.sha, element.path, filelist)
def protected_default_branch(repository: Repository) -> bool:
    """ Check if the default_branch has branch protection enabled """
    default_branch = repository.default_branch
    b = repository.get_branch(default_branch)
    return b.protected
Exemplo n.º 4
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}")