Skip to content

Commit

Permalink
feat: add an endpoint to retrieve lock status
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Jan 19, 2022
1 parent cbff0ed commit cacad21
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
13 changes: 11 additions & 2 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,18 @@
# )
# print(request.json())

"""Retrieve all pipelines"""
# """Retrieve all pipelines"""
# request = requests.get(
# 'http://127.0.0.1:5000/pipelines',
# headers={
# 'Content-Type': 'application/json',
# },
# )
# print(json.dumps(request.json(), indent=4))

"""Retrieve the lock status of a project"""
request = requests.get(
'http://127.0.0.1:5000/pipelines',
'http://127.0.0.1:5000/locks/justintime50-justinpaulhammond',
headers={
'Content-Type': 'application/json',
},
Expand Down
15 changes: 13 additions & 2 deletions harvey/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlitedict import SqliteDict # type: ignore

from harvey.globals import Global
from harvey.utils import LOG_LEVEL, setup_logger
from harvey.utils import LOG_LEVEL, Utils, setup_logger
from harvey.webhooks import Webhook

load_dotenv() # Must remain at the top of this file
Expand Down Expand Up @@ -94,7 +94,7 @@ def retrieve_pipelines():

@APP.route('/projects', methods=['GET'])
def retrieve_projects():
"""Retrieves projects from the Sqlite store."""
"""Retrieves a list of project names from the git repos stored in Harvey."""
projects = {'projects': []}

page_size = int(request.args.get('page_size', Global.PAGINATION_LIMIT))
Expand All @@ -116,6 +116,17 @@ def retrieve_projects():
return projects


@APP.route('/locks/<project_name>', methods=['GET'])
def retrieve_lock(project_name: str):
"""Retrieves the lock status of a project by its name."""
try:
lock_status = Utils.lookup_project_lock(project_name)

return {'locked': lock_status}
except Exception:
return abort(404)


# TODO: Add a `lock` and `unlock` endpoint for deployments


Expand Down
2 changes: 1 addition & 1 deletion harvey/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initialize_pipeline(webhook: Dict[str, Any]) -> Tuple[Dict[str, Any], str, d
logger = woodchips.get(LOGGER_NAME)

# Kill the pipeline if the project is locked
if Utils.lookup_project_lock(webhook) is True:
if Utils.lookup_project_lock(Global.repo_full_name(webhook)) is True:
Utils.kill(
f'{Global.repo_full_name(webhook)} deployments are locked. Please try again later or unlock'
' deployments.',
Expand Down
9 changes: 5 additions & 4 deletions harvey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,21 @@ def update_project_lock(webhook: Dict[str, Any], locked: bool = False):
logger.info(f'{locked_string} deployments for {Global.repo_full_name(webhook)}...')

with SqliteDict(Global.LOCKS_STORE_PATH) as mydict:
mydict[Global.repo_full_name(webhook)] = {
mydict[Global.repo_full_name(webhook).replace("/", "-")] = {
'locked': locked,
}

mydict.commit()

@staticmethod
def lookup_project_lock(webhook: Dict[str, Any]) -> bool:
"""Checks if a project is locked or not."""
def lookup_project_lock(project_name: str) -> bool:
"""Checks if a project is locked or not by its full name."""
locked_value = False
corrected_project_name = project_name.replace("/", "-")

with SqliteDict(Global.LOCKS_STORE_PATH) as mydict:
for key, value in mydict.iteritems():
if key == Global.repo_full_name(webhook):
if key == corrected_project_name:
locked_value = value['locked']
break

Expand Down

0 comments on commit cacad21

Please sign in to comment.