def create_repo(
        user: AuthenticatedUser,
        name: str,
        description: str = None,
        is_private: bool = False,
        auto_init: bool = True
) -> Repository:
    """
    This function creates new repository for the user
    :param description:
    :param user: repo will be created for the user account
    :param name: repos name
    :param is_private: True if private and False if public
    :param auto_init: if True repo will be initiated and README.md file will be added to the master branch
     :return: new Repository obj
    """
    try:
        repo = user.create_repo(name=name, description=description, private=is_private, auto_init=auto_init) # create the repository
        contents = repo.get_contents("README.md", ref="master") # get automatically generated README.md file
        repo.delete_file(contents.path, "remove invalid README.md", contents.sha, branch="master") # delete the invalid README.md
    except GithubException as e:
        LOGGER.error(f"Failed to create repository due to error {e}")
        raise e
    LOGGER.info(f"Repository {name} created")
    return repo
Beispiel #2
0
def github_user(monkeypatch):
    target = AuthenticatedUser(Mock(), Mock(), dict(), True)

    def create_gist(public, files):
        _patch_object(monkeypatch, target, public=public,
                      files={name: fc._identity['content'] for name, fc in files.items()})
        return target

    monkeypatch.setattr(target, 'create_gist', create_gist)
    return target
Beispiel #3
0
def get_repo(user: AuthenticatedUser) -> RemoteRepo:
    origin: str = git("config", "--get", "remote.origin.url")
    origin = origin.strip()

    for repo in user.get_repos():
        repo: Repository
        if repo.ssh_url == origin or repo.html_url == origin:
            return RemoteRepo(repo)

    raise Exception("Unable to find repo for %s in your GitHub account" %
                    origin)
Beispiel #4
0
def get_user_public_repos(user: AuthenticatedUser):
    result = []
    for item in user.get_repos():
        if not item.private:
            r = {
                "repo_name": item.name,
                "owner": item.owner.name,
                "size": size(item.size, system=verbose),
                "date": item.pushed_at.date()
            }
            result.append(r)
    result = sorted(result, key=lambda k: k['size'], reverse=True)
    return result
Beispiel #5
0
def create_repo(user: AuthenticatedUser,
                name: str,
                is_private: bool = False,
                auto_init: bool = True) -> Repository:
    """
    This function creates new repository for the user
    :param user: repo will be created for the user account
    :param name: repos name
    :param is_private: True if private and False if public
    :param auto_init: if True repo will be initiated and README.md file will be added to the master branch
     :return: new Repository obj
    """
    try:
        repo = user.create_repo(name=name,
                                private=is_private,
                                auto_init=auto_init)
    except GithubException as e:
        LOGGER.error(f"Failed to create repository due to error {e}")
        raise e
    LOGGER.info(f"Repository {name} created")
    return repo
def fork_create(repo_name: str, from_user: GithubUser,
                to_user: AuthenticatedUser):
    print('!!!!DEPRECATED!!!!', file=sys.stderr)
    try:
        from_repo = from_user.get_repo(repo_name)
    except GithubException:
        raise Exception('Repo "{}" does not exist'.format(repo_name))
    for fork_repo in from_repo.get_forks():
        if fork_repo.owner.id == to_user.id:
            print('Repo "{}" already forked to "{}".'.format(
                from_repo.full_name, fork_repo.full_name),
                  file=sys.stderr)
            return from_repo, fork_repo
    to_repo = to_user.create_fork(from_repo)
    new_name = "{}-{}".format(FORK_PREFIX, repo_name)
    to_repo.edit(name=new_name)
    to_repo.edit(has_issues=False,
                 has_projects=False,
                 has_wiki=False,
                 private=False)
    from_topics = from_repo.get_topics()
    to_repo.replace_topics([FORK_TAG] + from_topics)
    return from_repo, to_repo
Beispiel #7
0
def get_stars(user: AuthenticatedUser) -> list[Repository]:
    """Retrieve given user's starred repositories."""
    logger.info("Fetching stars")
    stars = list(user.get_starred())
    logger.debug(f"Fetched {len(stars)} stars successfully")
    return stars
Beispiel #8
0
def _add(repo: ghr.Repository, bot: gha.AuthenticatedUser) -> None:
    repo.add_to_collaborators(bot.login)
    invitation = repo.get_pending_invitations()[0]
    bot.accept_invitation(invitation)
    print(f'GH:{repo.full_name}: "{bot.login}" added to collaborators.')