Skip to content

Commit

Permalink
add docstring for the most of services in the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
khansadaoudi committed Dec 17, 2024
1 parent 74bf28f commit 63fee2a
Show file tree
Hide file tree
Showing 27 changed files with 993 additions and 105 deletions.
1 change: 1 addition & 0 deletions app/constructicon/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from mypy_extensions import TypedDict

class ConstructiconInterfce(TypedDict, total=False):
"""Typed constructicon interface"""
id: str
title: str
description: str
Expand Down
1 change: 1 addition & 0 deletions app/constructicon/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from app.shared.model import BaseM

class Constructicon(db.Model, BaseM):
"""Constructicon representation in the db"""
__tablename__ = 'constructicon'

id = Column(String(256), primary_key=True)
Expand Down
1 change: 1 addition & 0 deletions app/constructicon/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class ConstructiconSchema(Schema):
"""Constructicon schema shared with the db"""
id = fields.String(attribute="id")
title = fields.String(attribute="title")
description = fields.String(attribute="description")
Expand Down
34 changes: 34 additions & 0 deletions app/constructicon/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,60 @@
class ConstructiconService:
@staticmethod
def get_by_id(entry_id: str) -> Constructicon:
"""Get constructicon entity by id
Args:
entry_id (str)
Returns:
Constructicon
"""
return Constructicon.query.get(entry_id)

@staticmethod
def delete_by_id(entry_id: str):
"""Delete constructicon by id
Args:
entry_id (str)
"""
Constructicon.query.filter_by(id=entry_id).delete()
db.session.commit()

@staticmethod
def get_all_by_project_id(project_id):
"""Get all constructicon entities related to an object
Args:
project_id (int)
Returns:
constructicon_list(List[Constructicon])
"""
return Constructicon.query.filter_by(project_id=project_id).all()

@staticmethod
def create(new_attrs) -> Constructicon:
"""Create new constructicon entity
Args:
new_attrs (dict)
Returns:
Constructicon
"""
new_constructicon_entry = Constructicon(**new_attrs)
db.session.add(new_constructicon_entry)
db.session.commit()
return new_constructicon_entry

@staticmethod
def create_or_update(new_attrs):
"""Create or update constructicon entity
Args:
new_attrs (dict)
"""
entry_if_exists = ConstructiconService.get_by_id(new_attrs["id"])
if entry_if_exists:
print("KK updating values in db")
Expand Down
34 changes: 28 additions & 6 deletions app/github/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,23 @@

@api.route("/<string:project_name>/synchronize")
class GithubSynchronizationResource(Resource):

"""Class contains endpoints that deals with the synchronization"""
@responds(schema=GithubRepositorySchema, api=api)
def get(self, project_name):
"""Get the synchronized repository"""
project = ProjectService.get_by_name(project_name)
ProjectService.check_if_project_exist(project)
return GithubRepositoryService.get_by_project_id(project.id)

def post(self, project_name):
"""Create synchronization
Args:
project_name (str)
full_name(str): the name of the repository to be synchronized
branch_import(str): branch used for the import
branch_sync(str): branch to be used for the synchronization
"""
data = request.get_json()
full_name = data.get("fullName")
branch_import = data.get("branchImport")
Expand All @@ -35,20 +44,22 @@ def post(self, project_name):
GithubRepositoryService.create(data)

def delete(self, project_name):
"""Delete synchronization"""
project = ProjectService.get_by_name(project_name)
GithubRepositoryService.delete_by_project_id(project.id)
return { "status": "ok" }

@api.route("/github")
class UserGithubRepositories(Resource):

"""Class contains the endpoint to get user repositories"""
def get(self):
"""List user github repos"""
github_access_token = UserService.get_by_id(current_user.id).github_access_token
return GithubService.get_repositories(github_access_token)

@api.route("/github/branch")
class GithubRepositoryBranch(Resource):

"""class contains the endpoint to get the branch of specific repo"""
def get(self):
data = request.args
full_name = data.get("full_name")
Expand All @@ -57,12 +68,14 @@ def get(self):

@api.route("/<string:project_name>/synchronize/commit")
class GithubCommitResource(Resource):

"""Class contains endpoints related to commit"""
def get(self, project_name):
"""Get the number of changes to be committed"""
project = ProjectService.get_by_name(project_name)
return GithubCommitStatusService.get_changes_number(project.id)

def post(self, project_name):
"""Create and push a commit"""
data = request.get_json()
commit_message = data.get("commitMessage")
project = ProjectService.get_by_name(project_name)
Expand All @@ -75,20 +88,29 @@ def post(self, project_name):

@api.route("/<string:project_name>/synchronize/pull")
class GithubPullResource(Resource):

"""Class contains methods deals with the pulls"""
def get(self, project_name):
"""Check if there is changes to pull"""
github_access_token = UserService.get_by_id(current_user.id).github_access_token
return GithubWorkflowService.check_pull(github_access_token, project_name)

def post(self, project_name):
"""Pull changes"""
GithubWorkflowService.pull_changes(project_name)
LastAccessService.update_last_access_per_user_and_project(current_user.id, project_name, "write")
return { "status": "ok" }

@api.route("/<string:project_name>/synchronize/pull-request")
class GithubPullRequestResource(Resource):

"""Class deals with pull requests"""
def post(self,project_name):
"""_summary_
Args:
project_name (str)
branch (str)
title (str)
"""
data = request.get_json()
branch = data.get("branch")
title = data.get("title")
Expand Down
8 changes: 5 additions & 3 deletions app/github/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


class GithubRepository(db.Model):
"""Object represents the synchronized github repository"""
__tablename__ = "github_repositories"
id = Column(Integer, primary_key=True)
project_id = Column(Integer, db.ForeignKey("projects.id"))
user_id = Column(String(256), db.ForeignKey("users.id"))
repository_name = Column(String(256))
branch = Column(String(256))
user_id = Column(String(256), db.ForeignKey("users.id"))
repository_name = Column(String(256))
branch = Column(String(256)) # github branch synchronized with AG project
base_sha = Column(String(256)) # hash of the last commit of the synchronized branch

def update(self, changes):
Expand All @@ -18,6 +19,7 @@ def update(self, changes):


class GithubCommitStatus(db.Model):
"""entity represents the number of changes that needs to be committed by sample"""
__tablename__ = "commit_status"
id = Column(Integer, primary_key=True)
sample_name = Column(String(256), nullable=False)
Expand Down
1 change: 1 addition & 0 deletions app/github/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class GithubRepositorySchema(Schema):
"""Schema of the synchronized github that will be shared with the frontend"""
id = fields.Integer(attribute="id")
projectId = fields.Integer(attribute="project_id")
userId = fields.String(attribute="user_id")
Expand Down
Loading

0 comments on commit 63fee2a

Please sign in to comment.