Exemplo n.º 1
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.gitlab_user
        dict_user["repository"] = user.project.name
    return jsonify(dict_user), 200
Exemplo n.º 2
0
def webhook_repository(chat_id, project_id):
    if request.is_json:
        content = request.get_json()
        if content["object_kind"] == "pipeline" and "finished_at" in \
           content["object_attributes"]:
            webhook = Webhook(chat_id)
            pipeline_id = content["object_attributes"]["id"]
            jobs = webhook.get_pipeline_infos(project_id, pipeline_id)
            messages = webhook.build_message(jobs)
            status_message = webhook.build_status_message(content, jobs)
            project = Project.objects(project_id=project_id).first()
            user = User.objects(project=project.id).first()
            bot = Bot(token=ACCESS_TOKEN)
            if status_message:
                bot.send_message(chat_id=user.chat_id,
                                 text=status_message,
                                 parse_mode='Markdown',
                                 disable_web_page_preview=True)
            if content["object_attributes"]["status"] == "failed":
                rerunpipeline = RerunPipeline(user.chat_id)
                buttons = rerunpipeline.build_buttons(pipeline_id)
                reply_markup = telegram.InlineKeyboardMarkup(buttons)
                bot.send_message(chat_id=user.chat_id,
                                 text=messages["jobs_message"])
                bot.send_message(chat_id=user.chat_id,
                                 text="Se você quiser reiniciar essa pipeline,"
                                 " é só clicar nesse botão",
                                 reply_markup=reply_markup)
            return "OK"
        else:
            return "OK"
    else:
        return "OK"
Exemplo n.º 3
0
    def register_repo(self, repo_data):
        project_fullname = repo_data["project_name"]
        project_fullname_splited = project_fullname.split('/')
        project_name = project_fullname_splited[-1]
        project_id = repo_data["project_id"]

        user = User.objects(chat_id=self.chat_id).first()
        try:
            project = Project()
            if user.project:
                to_delete_project = user.project
                self.delete_webhook(project_id)
                self.delete_webhook(to_delete_project.project_id)
                project = user.project
                project.update_webhook_infos(project_name, project_id)
            else:
                project.save_webhook_infos(user, project_name, project_id)
            user.save_gitlab_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))
Exemplo n.º 4
0
    def test_create_user(self):
        User.drop_collection()
        user = User()
        username = "******"
        user.create_user(username)

        user2 = User.objects(username=username).first()
        self.assertEqual(user, user2)
Exemplo n.º 5
0
    def test_register_repo(self):
        user = self.user
        user.project = None
        user.save()
        user = User.objects(chat_id=self.user.chat_id).first()
        self.assertEqual(self.user.project, None)
        project_name = "ada-gitlab"
        project_id = "12532279"
        repo_data = {
            "project_name": project_name,
            "chat_id": "339847919",
            "project_id": project_id
        }
        self.webhook.register_repo(repo_data)

        user = User.objects(chat_id=self.user.chat_id).first()
        self.assertEqual(user.project.name, project_name)
        self.assertEqual(user.project.project_id, project_id)
Exemplo n.º 6
0
def save_user_domain(chat_id):
    try:
        post_json = request.json
        domain = post_json["domain"]
        user = User.objects(chat_id=chat_id).first()
        user.update(domain=domain)
    except HTTPError as http_error:
        return user.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify({"status": "success"}), 200
Exemplo n.º 7
0
    def test_save_gitlab_user_data(self):
        gitlab_user = '******'
        chat_id = 'id'
        gitlab_user_id = 'git_id'
        username = '******'
        user = User()
        user.username = username
        user.save()
        user.save_gitlab_user_data(gitlab_user, chat_id, gitlab_user_id)

        user_db = User.objects(username=username).first()
        self.assertEqual(user, user_db)
Exemplo n.º 8
0
def rerun_pipeline(chat_id, pipeline_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project = user.get_user_project()
        rerunpipeline = RerunPipeline(chat_id)
        restarted_pipeline = rerunpipeline.rerun_pipeline(
            project.project_id, pipeline_id)
    except HTTPError as http_error:
        return rerunpipeline.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(restarted_pipeline), 200
Exemplo n.º 9
0
 def return_project(self, chat_id, check_project_exists, object_type):
     user = User.objects(chat_id=chat_id).first()
     project = user.get_user_project()
     try:
         if self.get_class_type(object_type) == "Report":
             util = self.check_project_exists(project, object_type, user)
         else:
             util = self.check_project_exists(project, object_type, None)
     except HTTPError as http_error:
         raise HTTPError(
             self.exception_json(http_error.response.status_code))
     else:
         return util
Exemplo n.º 10
0
def stable_deploy(chat_id):
    try:
        user = User.objects(chat_id=chat_id).first()
        project = user.project
        stable_deploy = StableDeploy(chat_id)
        pipeline_id = stable_deploy.find_latest_stable_version(
            project.project_id)
        deploy_status = stable_deploy.run_stable_deploy(
            project.project_id, pipeline_id)
    except HTTPError as http_error:
        return stable_deploy.error_message(http_error)
    except AttributeError:
        return jsonify(NOT_FOUND), 404
    else:
        return jsonify(deploy_status), 200
Exemplo n.º 11
0
 def test_register_repo_validation(self, mocked_get, mocked_delete,
                                   mocked_os):
     user = self.user
     user.save()
     user = User.objects(chat_id=self.user.chat_id).first()
     project_name = "ada-gitlab"
     project_id = "12532279"
     repo_data = {
         "project_name": project_name,
         "chat_id": "339847919",
         "project_id": project_id
     }
     self.webhook.register_repo(repo_data)
     acess_token = "32y324798798732"
     webhook_url = "https://gitlab.adachatops.com/"
     mocked_os.getenv.return_value = acess_token
     mocked_os.getenv.return_value = webhook_url
     delete_hook = Response()
     delete_hook.status_code = 200
     mocked_get.return_value = self.mocked_get_hooks_response
     mocked_delete.return_value = delete_hook
     user = User.objects(chat_id=self.user.chat_id).first()
     self.assertEqual(user.project.name, project_name)
     self.assertEqual(user.project.project_id, project_id)
Exemplo n.º 12
0
 def register_user(self, user_data):
     user = User()
     gitlab_user = user_data["gitlab_user"]
     chat_id = user_data["chat_id"]
     gitlab_user_id = user_data["gitlab_user_id"]
     existing_user = User.objects(chat_id=chat_id).first()
     if existing_user:
         dict_error = {
             "message":
             "Eu vi aqui que você já cadastrou o usuário "
             "do GitLab. Sinto muitos, mas no momento não "
             "é possível cadastrar um novo usuário do GitLab "
             "ou alterá-lo."
         }
         raise HTTPError(json.dumps(dict_error))
     user.save_gitlab_user_data(gitlab_user, chat_id, gitlab_user_id)
Exemplo n.º 13
0
def get_access_token():
    code = request.args.get('code')
    chat_id = request.args.get('state')
    send_message(ACCESS_TOKEN, chat_id)
    existing_user = User.objects(chat_id=chat_id).first()
    if not existing_user:
        GITLAB_TOKEN = authenticate_access_token(code)
        db_user = User()
        db_user.access_token = GITLAB_TOKEN
        db_user.chat_id = str(chat_id)
        db_user.save()
        user = UserUtils(chat_id)
        user_infos = user.get_own_user_data()
        db_user.gitlab_user = user_infos["gitlab_username"]
        db_user.gitlab_user_id = str(user_infos["gitlab_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)
Exemplo n.º 14
0
    def test_save_gitlab_repo_data(self):
        User.drop_collection()
        user = User()
        username = "******"
        user.create_user(username)
        user.save()

        Project.drop_collection()
        project = Project()
        project.user_id = user.id
        project.description = "Test user add project"
        project.name = "Test user add project"
        project.web_url = "https://useraddProject.com"
        project.branches = ["branch1", "branch2"]
        project.save()

        user.save_gitlab_repo_data(project)

        project_user = User.objects(project=project).first()
        self.assertEqual(user, project_user)
Exemplo n.º 15
0
 def get_user_domain(self):
     user = User.objects(chat_id=self.chat_id).first()
     return user.domain
Exemplo n.º 16
0
 def test_register_user(self):
     self.webhook.register_user(self.user_data)
     user = User.objects(gitlab_user=self.gitlab_user).first()
     self.assertEqual(user.gitlab_user_id, self.gitlab_user_id)
Exemplo n.º 17
0
 def get_access_token(self, chat_id):
     user = User.objects(chat_id=chat_id).first()
     return user.access_token