diff --git a/api/object_specifications.py b/api/object_specifications.py index e47ba70ea..98c8af473 100644 --- a/api/object_specifications.py +++ b/api/object_specifications.py @@ -23,6 +23,7 @@ class Software(BaseModel): machine_id: int schedule_mode: str usage_scenario_variables: Optional[Dict[str, str]] = None + repo_to_watch_url: Optional[str] = None model_config = ConfigDict(extra='forbid') diff --git a/api/scenario_runner.py b/api/scenario_runner.py index 7bcb8b804..aed6a0370 100644 --- a/api/scenario_runner.py +++ b/api/scenario_runner.py @@ -747,6 +747,9 @@ async def software_add(software: Software, user: User = Depends(authenticate)): if software.usage_scenario_variables is None: software.usage_scenario_variables = {} + + if software.repo_to_watch_url is not None and software.repo_to_watch_url.strip() == '': + software.repo_to_watch_url = None if not DB().fetch_one('SELECT id FROM machines WHERE id=%s AND available=TRUE', params=(software.machine_id,)): raise RequestValidationError('Machine does not exist') @@ -765,6 +768,11 @@ async def software_add(software: Software, user: User = Depends(authenticate)): except ValueError as exc: # We accept the value error here if the repository is unknown, but log it for now error_helpers.log_error('Repository could not be checked in /v1/software/add.', exception=exc) + if software.repo_to_watch_url: + try: + utils.check_repo(software.repo_to_watch_url, software.branch) + except ValueError as exc: + error_helpers.log_error('Watch repository could not be checked in /v1/software/add.', exception=exc) if software.schedule_mode in ['daily', 'weekly', 'commit', 'commit-variance', 'tag', 'tag-variance']: @@ -775,7 +783,7 @@ async def software_add(software: Software, user: User = Depends(authenticate)): if 'commit' in software.schedule_mode: last_marker = utils.get_repo_last_marker(software.repo_url, 'commits') - Watchlist.insert(name=software.name, image_url=software.image_url, repo_url=software.repo_url, branch=software.branch, filename=software.filename, machine_id=software.machine_id, usage_scenario_variables=software.usage_scenario_variables, user_id=user._id, schedule_mode=software.schedule_mode, last_marker=last_marker) + Watchlist.insert(name=software.name, image_url=software.image_url, repo_url=software.repo_url, repo_to_watch_url=software.repo_to_watch_url, branch=software.branch, filename=software.filename, machine_id=software.machine_id, usage_scenario_variables=software.usage_scenario_variables, user_id=user._id, schedule_mode=software.schedule_mode, last_marker=last_marker) job_ids_inserted = [] diff --git a/cron/watchlist.py b/cron/watchlist.py index 8697df22a..75f52c881 100644 --- a/cron/watchlist.py +++ b/cron/watchlist.py @@ -24,14 +24,14 @@ def schedule_watchlist_item(): query = """ SELECT - id, name, repo_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker, + id, name, repo_url, repo_to_watch_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker, DATE(last_scheduled) >= DATE(NOW()) as "scheduled_today", DATE(last_scheduled) >= DATE(NOW() - INTERVAL '7 DAYS') as "scheduled_last_week" FROM watchlist """ data = DB().fetch_all(query) - for [item_id, name, repo_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker, scheduled_today, scheduled_last_week] in data: + for [item_id, name, repo_url, repo_to_watch_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker, scheduled_today, scheduled_last_week] in data: print(f"Watchlist item is on {schedule_mode} schedule", repo_url, branch, filename, machine_id) if schedule_mode == 'one-off': @@ -50,26 +50,36 @@ def schedule_watchlist_item(): Job.insert('run', user_id=user_id, name=name, url=repo_url, email=None, branch=branch, filename=filename, usage_scenario_variables=usage_scenario_variables, machine_id=machine_id) print('\tInserted') elif schedule_mode in ['tag', 'tag-variance']: - last_marker_new = utils.get_repo_last_marker(repo_url, 'tags') + if repo_to_watch_url: + last_marker_new = utils.get_repo_last_marker(repo_to_watch_url, 'tags') + else: + last_marker_new = utils.get_repo_last_marker(repo_url, 'tags') + print('Last marker is', last_marker, ' - Current maker is', last_marker_new) if last_marker == last_marker_new: continue amount = 3 if 'variance' in schedule_mode else 1 for _ in range(0,amount): Job.insert('run', user_id=user_id, name=name, url=repo_url, email=None, branch=branch, filename=filename, usage_scenario_variables=usage_scenario_variables, machine_id=machine_id) - print('Updating Hash', last_marker_new) - DB().query('UPDATE watchlist SET last_marker = %s WHERE id = %s', params=(last_marker_new, item_id, )) + + print('Updating Hash', last_marker_new) + DB().query('UPDATE watchlist SET last_marker = %s WHERE id = %s', params=(last_marker_new, item_id, )) elif schedule_mode in ['commit', 'commit-variance']: - last_marker_new = utils.get_repo_last_marker(repo_url, 'commits') + if repo_to_watch_url: + last_marker_new = utils.get_repo_last_marker(repo_to_watch_url, 'commits') + else: + last_marker_new = utils.get_repo_last_marker(repo_url, 'commits') + print('Last marker is', last_marker, ' - Current maker is', last_marker_new) if last_marker == last_marker_new: continue amount = 3 if 'variance' in schedule_mode else 1 for _ in range(0,amount): - print('Updating Hash', last_marker_new) Job.insert('run', user_id=user_id, name=name, url=repo_url, email=None, branch=branch, filename=filename, usage_scenario_variables=usage_scenario_variables, machine_id=machine_id) - DB().query('UPDATE watchlist SET last_marker = %s WHERE id = %s', params=(last_marker_new, item_id, )) + + print('Updating Hash', last_marker_new) + DB().query('UPDATE watchlist SET last_marker = %s WHERE id = %s', params=(last_marker_new, item_id, )) if __name__ == '__main__': try: diff --git a/docker/structure.sql b/docker/structure.sql index 8fd7a2508..cce6064e6 100644 --- a/docker/structure.sql +++ b/docker/structure.sql @@ -443,6 +443,7 @@ CREATE TABLE watchlist ( name text, image_url text, repo_url text, + repo_to_watch_url text, categories integer[], branch text NOT NULL, filename text NOT NULL, diff --git a/frontend/js/request.js b/frontend/js/request.js index bcf8aa6d3..3900f3c85 100644 --- a/frontend/js/request.js +++ b/frontend/js/request.js @@ -13,6 +13,9 @@ const populateFieldsFromURL = () => { if (urlParams.has('repo_url')) { // precedence document.querySelector('input[name="repo_url"]').value = escapeString(urlParams.get('repo_url')); } + if (urlParams.has('repo_to_watch_url')) { + document.querySelector('input[name="repo_to_watch_url"]').value = escapeString(urlParams.get('repo_to_watch_url')); + } if (urlParams.has('filename')) { document.querySelector('input[name="filename"]').value = escapeString(urlParams.get('filename')); } @@ -118,6 +121,26 @@ const updateRemoveButtonsVisibility = () => { }); + $('#variables-container').on('click', '.remove-variable', function (e) { + $(this).closest('.variable-row').remove(); + updateRemoveButtonsVisibility(); + }); + + const toggleWatchRepoVisibility = () => { + const scheduleModeSelect = document.getElementById('schedule-mode-select'); + const watchRepoDiv = document.getElementById('watch-different-repo'); + const commitModes = ['commit', 'commit-variance', 'tag', 'tag-variance']; + + if (commitModes.includes(scheduleModeSelect.value)) { + watchRepoDiv.classList.remove('hidden'); + } else { + watchRepoDiv.classList.add('hidden'); + } + }; + + toggleWatchRepoVisibility(); + $('#schedule-mode-select').on('change', toggleWatchRepoVisibility); + document.forms[0].onsubmit = async (event) => { event.preventDefault(); diff --git a/frontend/request.html b/frontend/request.html index 2f4ea4ec5..7498ad42a 100644 --- a/frontend/request.html +++ b/frontend/request.html @@ -105,7 +105,7 @@

Find the specifications of the machines in our documentation

- @@ -118,6 +118,12 @@

+

Define variables to be used. See the documentation here.

@@ -127,7 +133,10 @@

- + + + + diff --git a/lib/watchlist.py b/lib/watchlist.py index f9a1f3b0d..1e5d7455d 100644 --- a/lib/watchlist.py +++ b/lib/watchlist.py @@ -11,14 +11,14 @@ class Watchlist(): #pylint:disable=redefined-outer-name @classmethod - def insert(cls, *, name, image_url, repo_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker): + def insert(cls, *, name, image_url, repo_url, repo_to_watch_url, branch, filename, usage_scenario_variables, machine_id, user_id, schedule_mode, last_marker): # Watchlist items never insert / use emails as they are always premium and made by admin # So they need no notification on success / add insert_query = """ INSERT INTO - watchlist (name, image_url, repo_url, branch, filename, usage_scenario_variables, machine_id, last_marker, user_id, schedule_mode, created_at) + watchlist (name, image_url, repo_url, repo_to_watch_url, branch, filename, usage_scenario_variables, machine_id, last_marker, user_id, schedule_mode, created_at) VALUES - (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW()) RETURNING id; + (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW()) RETURNING id; """ - params = (name, image_url, repo_url, branch, filename, json.dumps(usage_scenario_variables), machine_id, last_marker, user_id, schedule_mode,) + params = (name, image_url, repo_url, repo_to_watch_url, branch, filename, json.dumps(usage_scenario_variables), machine_id, last_marker, user_id, schedule_mode,) return DB().fetch_one(insert_query, params=params)[0]