class User(mongoengine.Document): init_db() username = mongoengine.StringField(max_length=100) project = mongoengine.ReferenceField(Project) access_token = mongoengine.StringField(max_length=100) gitlab_user = mongoengine.StringField(max_length=100) chat_id = mongoengine.StringField(max_length=100) gitlab_user_id = mongoengine.StringField(max_length=100) api_token = mongoengine.StringField(max_length=100) domain = mongoengine.URLField() meta = {'db_alias': 'AdaBot', 'collection': 'User'} def create_user(self, username): self.username = username self.save() return self def save_gitlab_user_data(self, gitlab_user, chat_id, gitlab_user_id): self.gitlab_user = gitlab_user self.chat_id = chat_id self.gitlab_user_id = gitlab_user_id self.save() return self def save_gitlab_repo_data(self, project): self.project = project self.save() return self def get_user_project(self): project = self.project project = Project.objects(id=project.id).first() return project
class GeneralInformationPipelines(mongoengine.Document): project = mongoengine.ReferenceField(Project, required=True) number_of_pipelines = mongoengine.IntField() successful_pipelines = mongoengine.IntField() init_db() meta = { 'db_alias': 'AdaBot', 'collection': 'GeneralInformationPipeline' } def create_general_information_pipeline(self, project, number_of_pipelines, successful_pipelines): self.project = project self.number_of_pipelines = number_of_pipelines self.successful_pipelines = successful_pipelines self.save() return self @staticmethod def get_general_information_pipeline(project): general_information_pipeline = GeneralInformationPipelines.objects( project=project).first() return general_information_pipeline def add_pipeline(self, pipeline, project): all_jobs_successful = True general_information_pipeline = self.\ get_general_information_pipeline(project) general_information_pipeline.modify(inc__number_of_pipelines=1) for job in pipeline.jobs: if not job['status']: all_jobs_successful = False break else: # do nothing, if job status is true then it is successful continue if all_jobs_successful: general_information_pipeline.modify(inc__successful_pipelines=1) else: # do nothing, if pipeline fails, # only the number of pipelines increases pass
def setUp(self): self.db = init_db() self.user = User() self.user.username = '******' self.user.chat_id = '339847919' self.user.gitlab_user = '******' self.user.gitlab_user_id = '4047441' self.user.access_token = "123456" self.user.save() self.project = Project() self.project_name = 'ada-gitlab' self.project_id = '12532279' self.project.save_webhook_infos(self.user, self.project_name, self.project_id) self.user.save_gitlab_repo_data(self.project) self.GITLAB_API_TOKEN = "12345" self.mocked_404_response = Response() self.mocked_404_response.status_code = 404 self.response_unauthorized = Response() self.response_unauthorized.status_code = 401
class CurrentPipeline(mongoengine.Document): project = mongoengine.ReferenceField(Project, required=True) name = mongoengine.StringField(required=True) jobs = mongoengine.ListField(mongoengine.DictField()) init_db() meta = {'db_alias': 'AdaBot', 'collection': 'CurrentPipeline'} def create_current_pipeline(self, name, jobs, project): self.name = name self.jobs = jobs self.project = project self.save() return self @staticmethod def get_current_pipeline(project): current_pipeline = CurrentPipeline.\ objects(project=project).all() return current_pipeline
class Project(mongoengine.Document): user_id = mongoengine.ObjectIdField(required=True) description = mongoengine.StringField(max_length=100) name = mongoengine.StringField(max_length=100) web_url = mongoengine.URLField() branches = mongoengine.ListField() project_id = mongoengine.StringField(max_length=100) init_db() meta = {'db_alias': 'AdaBot', 'collection': 'Project'} def save_webhook_infos(self, user, name, project_id): self.user_id = user.id self.name = name self.project_id = project_id self.save() return self def update_webhook_infos(self, name, project_id): self.name = name self.project_id = project_id self.update(name=name, project_id=project_id) return self