예제 #1
0
    def register_repo(self, repo_json):
        project_name = repo_json["repository_name"]
        owner = repo_json["owner"]
        chat_id = repo_json["chat_id"]

        user = User.objects(chat_id=chat_id).first()
        try:
            project = Project()
            if user.project:
                webhook = Webhook(chat_id)
                webhook.delete_hook(owner, project_name)
                webhook.delete_hook(user.github_user, user.project.name)
                user.github_user = owner
                user.save()
                project = user.project
                project.update_repository_infos(str(project_name))
            else:
                webhook = Webhook(chat_id)
                webhook.delete_hook(owner, project_name)
                user.github_user = owner
                user.save()
                project.save_repository_infos(user, str(project_name))
            user.save_github_repo_data(project)
        except AttributeError:
            dict_error = {
                "message":
                "Tive um erro tentando cadastrar seu repositório. "
                "Mais tarde você tenta. Ok?"
            }
            raise AttributeError(json.dumps(dict_error))
예제 #2
0
def get_user_infos(chat_id):
    dict_user = {"username": 0,
                 "repository": 0}
    user = User.objects(chat_id=chat_id).first()
    if user:
        dict_user["username"] = user.github_user
        dict_user["repository"] = user.project.name
    return jsonify(dict_user), 200
예제 #3
0
    def test_save_github_user_data(self):
        User.drop_collection()
        github_user = '******'
        chat_id = 'id'
        github_user_id = 'git_id'
        user = User()
        user.github_user = github_user
        user.save()
        user.save_github_user_data(github_user, chat_id, github_user_id)

        user_db = User.objects(github_user=github_user).first()
        self.assertEqual(user, user_db)
예제 #4
0
def get_releases(chat_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project_release = user.project
        project_release = project_release.name.split("/")
        release = Release(chat_id, user.github_user, project_release[-1])
        release_data = release.get_last_release()
    except HTTPError as http_error:
        return release.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(release_data), 200
예제 #5
0
    def test_save_github_repo_data(self):
        user = User()
        username = "******"
        user.create_user(username)

        project = Project()
        project.user_id = user.id
        project.save()

        user.save_github_repo_data(project)

        project_user = User.objects(project=project).first()
        self.assertEqual(user, project_user)
예제 #6
0
def get_pull_request(chat_id):

    try:
        user = User.objects(chat_id=chat_id).first()
        project_pr = user.project
        project_pr = project_pr.name.split("/")
        pull_request = PullRequest(chat_id)
        pull_request_data = pull_request.get_pull_requests(
            user.github_user, project_pr[-1])
    except HTTPError as http_error:
        return pull_request.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(pull_request_data), 200
예제 #7
0
def get_contributor_issues(chat_id, contributor_username):
    try:
        user = User.objects(chat_id=chat_id).first()
        contributor_issues = ContributorIssues(chat_id)
        project = user.project
        project_collaborator = FindProjectCollaborators(chat_id)
        full_name = project_collaborator.get_project(project.name)
        issues = contributor_issues.\
            get_contributor_issues(full_name, contributor_username)
    except HTTPError as http_error:
        return contributor_issues.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(issues), 200
예제 #8
0
def find_collaborators(chat_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project = user.project
        project_name = project.name
        collab = FindProjectCollaborators(chat_id)
        owner_and_repo = collab.get_project(project_name)
        contributors_names = collab.get_collaborators(str(owner_and_repo))

    except HTTPError as http_error:
        return collab.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404

    else:
        return jsonify({"collaborators": contributors_names}), 200
예제 #9
0
def get_access_token(chat_id):
    code = request.args.get('code')
    existing_user = User.objects(chat_id=chat_id).first()
    send_message(ACCESS_TOKEN, chat_id)
    if not existing_user:
        GITHUB_TOKEN = authenticate_access_token(code)
        db_user = User()
        db_user.access_token = GITHUB_TOKEN
        db_user.chat_id = str(chat_id)
        db_user.save()
        user = UserInfo(chat_id)
        user_infos = user.get_own_user_data()
        db_user.github_user = user_infos["github_username"]
        db_user.github_user_id = str(user_infos["github_user_id"])
        db_user.save()
        user.send_button_message(user_infos, chat_id)
    redirect_uri = "https://t.me/{bot_name}".format(bot_name=BOT_NAME)
    return redirect(redirect_uri, code=302)
예제 #10
0
def comment_issue(chat_id):
    try:
        response = request.get_json()
        body = response['body']
        issue_number = response['issue_number']

        user = User.objects(chat_id=chat_id).first()
        project = Project()
        project = user.project
        project = project.name.split("/")
        issue = Issue(chat_id)
        comment_issue = issue.comment_issue(project[-1], user.github_user,
                                            issue_number, body)
    except HTTPError as http_error:
        return issue.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify({"body": comment_issue["body"]}), 200
예제 #11
0
def create_issue(chat_id):
    try:
        response = request.get_json()
        title = response['title']
        body = response['body']

        user = User.objects(chat_id=chat_id).first()
        project = Project()
        project = user.project
        project = project.name.split("/")
        issue = Issue(chat_id)
        create_issue = issue.create_issue(project[-1], user.github_user, title,
                                          body)
    except HTTPError as http_error:
        return issue.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify({
            "title": create_issue["title"],
            "body": create_issue["body"],
            "html_url": create_issue["html_url"]
        }), 200
예제 #12
0
 def get_access_token(self, chat_id):
     user = User.objects(chat_id=chat_id).first()
     return user.access_token
예제 #13
0
 def test_create_user(self):
     user = User()
     username = "******"
     user.create_user(username)
     user2 = User.objects(github_user=username).first()
     self.assertEqual(user, user2)