From a9c532f8736d56a9f7edbdabb5f4feaa659f812f Mon Sep 17 00:00:00 2001 From: andrew Date: Sun, 11 Jun 2023 06:49:38 -0400 Subject: [PATCH] Allow using git tags for branch names Now supports branch names, commit shas, and git tags. Shas must still be the full length 40-character value. Tags are a fall back on branch names. If a tag has the same name as a branch, the branch will be retrieved and not the tag --- OpenBench/urls.py | 2 +- OpenBench/utils.py | 24 ++++++++++++------- OpenBench/views.py | 9 +++---- .../{newTest.html => create_test.html} | 0 4 files changed, 19 insertions(+), 16 deletions(-) rename Templates/OpenBench/{newTest.html => create_test.html} (100%) diff --git a/OpenBench/urls.py b/OpenBench/urls.py index d6b42e7..324fb13 100644 --- a/OpenBench/urls.py +++ b/OpenBench/urls.py @@ -52,7 +52,7 @@ urlpatterns = [ django.urls.path(r'test//', OpenBench.views.test), django.urls.path(r'viewTest//', OpenBench.views.test), django.urls.path(r'test//', OpenBench.views.test), - django.urls.path(r'newTest/', OpenBench.views.newTest), + django.urls.path(r'newTest/', OpenBench.views.create_test), # Links for viewing and managing Networks django.urls.path(r'networks/', OpenBench.views.networks), diff --git a/OpenBench/utils.py b/OpenBench/utils.py index 7a5b986..e1f4528 100644 --- a/OpenBench/utils.py +++ b/OpenBench/utils.py @@ -216,10 +216,9 @@ def determine_bench(request, field, message): def collect_github_info(request, errors, field): - # Get branch name, whether it is a commit SHA, and the API path for it + # Get branch name / commit sha / tag, and the API path for it branch = request.POST['{0}_branch'.format(field)] bysha = bool(re.search('[0-9a-fA-F]{40}', branch)) - url = 'commits' if bysha else 'branches' # All API requests will share this common path. Some engines are private. base = request.POST['%s_repo' % (field)].replace('github.com', 'api.github.com/repos') @@ -257,14 +256,21 @@ def collect_github_info(request, errors, field): ## [D] If the engine is private, we will carry onto Step 3. try: # Fetch data from the Github API - path = 'commits' if bysha else 'branches' - url = path_join(base, path, branch) - data = requests.get(url, headers=headers).json() - data = data if bysha else data['commit'] - except: # Unable to connect for whatever reason - path = 'Commit Sha' if bysha else 'Branch' - errors.append('{0} {1} could not be found'.format(path, branch)) + # Lookup branch or commit sha, but will fail for tags + url = path_join(base, 'commits' if bysha else 'branches', branch) + data = requests.get(url, headers=headers).json() + + # Check to see if the branch name was actually a tag name + if not bysha and 'commit' not in data: + url = path_join(base, 'commits', branch) + data = requests.get(url, headers=headers).json() + + # Actual branches have to go one layer deeper + elif not bysha: data = data['commit'] + + except: # Unable to find for whatever reason + errors.append('%s could not be found' % (branch)) return (None, None) # Extract the bench from the web form, or from the commit message diff --git a/OpenBench/views.py b/OpenBench/views.py index 06c5c1c..b10e62f 100644 --- a/OpenBench/views.py +++ b/OpenBench/views.py @@ -394,7 +394,7 @@ def test(request, id, action=None): LogEvent.objects.create(author=request.user.username, summary=action, log_file='', test_id=test.id) return django.http.HttpResponseRedirect('/index/') -def newTest(request): +def create_test(request): if not request.user.is_authenticated: return redirect(request, '/login/', error='Only enabled users can create tests') @@ -404,14 +404,11 @@ def newTest(request): if request.method == 'GET': data = { 'networks' : list(Network.objects.all().values()) } - return render(request, 'newTest.html', data) + return render(request, 'create_test.html', data) test, errors = OpenBench.utils.create_new_test(request) if errors != [] and errors != None: - errors = ["[{0}]: {1}".format(i, e) for i, e in enumerate(errors)] - longest = max([len(e) for e in errors]) - errors = ["{0}{1}".format(e, ' ' * (longest-len(e))) for e in errors] - return render(request, 'newTest.html', {'error_message': '\n'.join(errors)}) + return redirect(request, '/newTest/', error='\n'.join(errors)) username = request.user.username profile = Profile.objects.get(user=request.user) diff --git a/Templates/OpenBench/newTest.html b/Templates/OpenBench/create_test.html similarity index 100% rename from Templates/OpenBench/newTest.html rename to Templates/OpenBench/create_test.html