Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
def load_project_to_repo(repo: Repository, branch: str = 'master') -> None:
    """
    This method will load project to the repository
    :param repo: user's repo
    :param branch: branch to commit
    """
    try:
        for file_path in project_files(os.getcwd()):
            with open(file_path) as f:
                content = f.read()
            repo_path = file_path.split('DR')[-1]
            repo.create_file(path=repo_path[1:],
                             message="initial_commit",
                             content=content,
                             branch=branch)
    except GithubException as e:
        LOGGER.error(f"Failed to load files repository due to error {e}")
    LOGGER.info(f"Files loaded to the repository {repo.full_name}")
Ejemplo n.º 3
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
Ejemplo n.º 4
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.home'))
    form = LoginForm()
    if form.validate_on_submit():
        try:
            github = Github(form.user_name.data, form.password.data)
            github_user = github.get_user().raw_data
        except BadCredentialsException as e:
            flash('Login Unsuccessful. Please check username and password',
                  'danger')
            LOGGER.error(
                f"Login to GitHub Failed, user: {form.user_name.data}, error {e}"
            )
            return render_template('login.html', title='Login', form=form)
        except GithubException as e:
            LOGGER.error(f"An error occurred, error {e}")

        user = UserLogin(github_user=github_user)
        session['github_user'] = pickle.dumps(github.get_user())
        db.session.add(user)
        try:
            db.session.commit()
        except (SQLAlchemyError, DatabaseError) as e:
            LOGGER.error(f"DB error: {e} ")
        login_user(user, remember=form.remember.data)
        next_page = request.args.get('next')
        return redirect(next_page) if next_page else redirect(
            url_for('main.home'))
    return render_template('login.html', title='Login', form=form)
Ejemplo n.º 5
0
def new_repo():
    form = NewRepoForm()
    if form.validate_on_submit():
        github_user = pickle.loads(session['github_user'])
        try:
            repo = create_repo(user=github_user,
                               name=form.repository_name.data,
                               description=form.description.data)
            load_project_to_repo(repo)
        except GithubException as e:
            if e.status == 422:
                flash(
                    'Name already exists on this account, please use different name',
                    'danger')
                LOGGER.error(
                    f"Failed to create repo, user: {current_user.username}, error {e}"
                )
                return render_template(
                    template_name_or_list='create_repo.html',
                    title='Create Repository',
                    form=form,
                    legend='Create Repository')
            else:
                raise e
        load = Load(user_login_id=current_user.id)
        db.session.add(load)
        try:
            db.session.commit()
        except (SQLAlchemyError, DatabaseError) as e:
            LOGGER.error(f"DB error: {e} ")
        flash('The source code of this app has been uploaded to your GutHub!',
              'success')
        LOGGER.info(
            f'New load by user {current_user.username}, login id {current_user.id}'
        )
        return redirect(url_for('main.home'))
    return render_template(template_name_or_list='create_repo.html',
                           title='Create Repository',
                           form=form,
                           legend='Create Repository')