def get_projects(gitlab_user): gitlab_projects_including_non_visualisation = gitlab_api_v4( 'GET', f'projects', params=( ('archived', 'false'), ('min_access_level', DEVELOPER_ACCESS_LEVEL), ('sudo', gitlab_user['id']), ('per_page', '100'), ), ) gitlab_projects = [ gitlab_project for gitlab_project in gitlab_projects_including_non_visualisation if 'visualisation' in [tag.lower() for tag in gitlab_project['tag_list']] ] return sorted( [ { 'gitlab_project': gitlab_project, 'manage_link': reverse( 'visualisations:branch', kwargs={ 'gitlab_project_id': gitlab_project['id'], 'branch_name': gitlab_project['default_branch'], }, ), } for gitlab_project in gitlab_projects ], key=lambda d: d['gitlab_project']['name'].lower(), )
def _visualisation_branches(gitlab_project): # Sort default branch first, the remaining in last commit order def branch_sort_key(branch): return ( branch['name'] == gitlab_project['default_branch'], branch['commit']['committed_date'], branch['name'], ) return sorted( gitlab_api_v4('GET', f'/projects/{gitlab_project["id"]}/repository/branches'), key=branch_sort_key, reverse=True, )
def _gitlab_ecr_pipeline_get(pipeline_id): return gitlab_api_v4( 'GET', f'/projects/{ECR_PROJECT_ID}/pipelines/{pipeline_id}')
def _gitlab_ecr_pipeline_cancel(pipeline_id): return gitlab_api_v4( 'POST', f'/projects/{ECR_PROJECT_ID}/pipelines/{pipeline_id}/cancel')
def _gitlab_ecr_pipeline_get(pipeline_id): return gitlab_api_v4( "GET", f"/projects/{ECR_PROJECT_ID}/pipelines/{pipeline_id}")
def _gitlab_ecr_pipeline_cancel(pipeline_id): return gitlab_api_v4( "POST", f"/projects/{ECR_PROJECT_ID}/pipelines/{pipeline_id}/cancel")
def visualisations_html_GET(request): gitlab_users = gitlab_api_v4( 'GET', f'/users', params=( ('extern_uid', request.user.profile.sso_id), ('provider', 'oauth2_generic'), ), ) has_gitlab_user = bool(gitlab_users) # Something has really gone wrong if GitLab has multiple users with the # same SSO ID if len(gitlab_users) > 1: return HttpResponse(status=500) gitlab_url = ( urlsplit( gitlab_api_v4('GET', f'groups/{settings.GITLAB_VISUALISATIONS_GROUP}')[ 'web_url' ] ) ._replace(path='/', query='', fragment='') .geturl() ) def get_projects(gitlab_user): gitlab_projects_including_non_visualisation = gitlab_api_v4( 'GET', f'projects', params=( ('archived', 'false'), ('min_access_level', DEVELOPER_ACCESS_LEVEL), ('sudo', gitlab_user['id']), ('per_page', '100'), ), ) gitlab_projects = [ gitlab_project for gitlab_project in gitlab_projects_including_non_visualisation if 'visualisation' in [tag.lower() for tag in gitlab_project['tag_list']] ] return sorted( [ { 'gitlab_project': gitlab_project, 'manage_link': reverse( 'visualisations:branch', kwargs={ 'gitlab_project_id': gitlab_project['id'], 'branch_name': gitlab_project['default_branch'], }, ), } for gitlab_project in gitlab_projects ], key=lambda d: d['gitlab_project']['name'].lower(), ) return render( request, 'visualisations.html', { 'has_gitlab_user': has_gitlab_user, 'gitlab_url': gitlab_url, 'projects': get_projects(gitlab_users[0]) if has_gitlab_user else [], }, status=200, )