Merge pull request #448 from titouanmathis/feat/gitlab-filter-projects-branches
feat: Add support for search for GitLab applications
This commit is contained in:
		@@ -1,6 +1,7 @@
 | 
				
			|||||||
<script lang="ts">
 | 
					<script lang="ts">
 | 
				
			||||||
	export let application;
 | 
						export let application;
 | 
				
			||||||
	export let appId;
 | 
						export let appId;
 | 
				
			||||||
 | 
						import Select from 'svelte-select';
 | 
				
			||||||
	import { page, session } from '$app/stores';
 | 
						import { page, session } from '$app/stores';
 | 
				
			||||||
	import { onMount } from 'svelte';
 | 
						import { onMount } from 'svelte';
 | 
				
			||||||
	import { errorNotification } from '$lib/form';
 | 
						import { errorNotification } from '$lib/form';
 | 
				
			||||||
@@ -33,6 +34,10 @@
 | 
				
			|||||||
	let showSave = false;
 | 
						let showSave = false;
 | 
				
			||||||
	let autodeploy = application.settings.autodeploy || true;
 | 
						let autodeploy = application.settings.autodeploy || true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let search = {
 | 
				
			||||||
 | 
							project: '',
 | 
				
			||||||
 | 
							branch: ''
 | 
				
			||||||
 | 
						};
 | 
				
			||||||
	let selected = {
 | 
						let selected = {
 | 
				
			||||||
		group: undefined,
 | 
							group: undefined,
 | 
				
			||||||
		project: undefined,
 | 
							project: undefined,
 | 
				
			||||||
@@ -84,16 +89,49 @@
 | 
				
			|||||||
		}, 100);
 | 
							}, 100);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						function selectGroup(event) {
 | 
				
			||||||
 | 
							selected.group = event.detail;
 | 
				
			||||||
 | 
							selected.project = null;
 | 
				
			||||||
 | 
							selected.branch = null;
 | 
				
			||||||
 | 
							showSave = false;
 | 
				
			||||||
 | 
							loadProjects();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						async function searchProjects(searchText) {
 | 
				
			||||||
 | 
							if (!selected.group) {
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							search.project = searchText;
 | 
				
			||||||
 | 
							await loadProjects();
 | 
				
			||||||
 | 
							return projects;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						function selectProject(event) {
 | 
				
			||||||
 | 
							selected.project = event.detail;
 | 
				
			||||||
 | 
							selected.branch = null;
 | 
				
			||||||
 | 
							showSave = false;
 | 
				
			||||||
 | 
							loadBranches();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	async function loadProjects() {
 | 
						async function loadProjects() {
 | 
				
			||||||
 | 
							const params = new URLSearchParams({
 | 
				
			||||||
 | 
								page: 1,
 | 
				
			||||||
 | 
								per_page: 25,
 | 
				
			||||||
 | 
								archived: false
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (search.project) {
 | 
				
			||||||
 | 
								params.append('search', search.project);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		loading.projects = true;
 | 
							loading.projects = true;
 | 
				
			||||||
		if (username === selected.group.name) {
 | 
							if (username === selected.group.name) {
 | 
				
			||||||
			try {
 | 
								try {
 | 
				
			||||||
				projects = await get(
 | 
									params.append('min_access_level', 40);
 | 
				
			||||||
					`${apiUrl}/v4/users/${selected.group.name}/projects?min_access_level=40&page=1&per_page=25&archived=false`,
 | 
									projects = await get(`${apiUrl}/v4/users/${selected.group.name}/projects?${params}`, {
 | 
				
			||||||
					{
 | 
					 | 
				
			||||||
					Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
										Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
				
			||||||
					}
 | 
									});
 | 
				
			||||||
				);
 | 
					 | 
				
			||||||
			} catch (error) {
 | 
								} catch (error) {
 | 
				
			||||||
				errorNotification(error);
 | 
									errorNotification(error);
 | 
				
			||||||
				throw new Error(error);
 | 
									throw new Error(error);
 | 
				
			||||||
@@ -102,12 +140,9 @@
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			try {
 | 
								try {
 | 
				
			||||||
				projects = await get(
 | 
									projects = await get(`${apiUrl}/v4/groups/${selected.group.id}/projects?${params}`, {
 | 
				
			||||||
					`${apiUrl}/v4/groups/${selected.group.id}/projects?page=1&per_page=25&archived=false`,
 | 
					 | 
				
			||||||
					{
 | 
					 | 
				
			||||||
					Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
										Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
				
			||||||
					}
 | 
									});
 | 
				
			||||||
				);
 | 
					 | 
				
			||||||
			} catch (error) {
 | 
								} catch (error) {
 | 
				
			||||||
				errorNotification(error);
 | 
									errorNotification(error);
 | 
				
			||||||
				throw new Error(error);
 | 
									throw new Error(error);
 | 
				
			||||||
@@ -117,11 +152,35 @@
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						async function searchBranches(searchText) {
 | 
				
			||||||
 | 
							if (!selected.project) {
 | 
				
			||||||
 | 
								return;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							search.branch = searchText;
 | 
				
			||||||
 | 
							await loadBranches();
 | 
				
			||||||
 | 
							return branches;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						function selectBranch(event) {
 | 
				
			||||||
 | 
							selected.branch = event.detail;
 | 
				
			||||||
 | 
							isBranchAlreadyUsed();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	async function loadBranches() {
 | 
						async function loadBranches() {
 | 
				
			||||||
 | 
							const params = new URLSearchParams({
 | 
				
			||||||
 | 
								page: 1,
 | 
				
			||||||
 | 
								per_page: 100
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (search.branch) {
 | 
				
			||||||
 | 
								params.append('search', search.branch);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		loading.branches = true;
 | 
							loading.branches = true;
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			branches = await get(
 | 
								branches = await get(
 | 
				
			||||||
				`${apiUrl}/v4/projects/${selected.project.id}/repository/branches?per_page=100&page=1`,
 | 
									`${apiUrl}/v4/projects/${selected.project.id}/repository/branches?${params}`,
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
										Authorization: `Bearer ${$gitTokens.gitlabToken}`
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
@@ -267,70 +326,79 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
<form on:submit={handleSubmit}>
 | 
					<form on:submit={handleSubmit}>
 | 
				
			||||||
	<div class="flex flex-col space-y-2 px-4 xl:flex-row xl:space-y-0 xl:space-x-2 ">
 | 
						<div class="flex flex-col space-y-2 px-4 xl:flex-row xl:space-y-0 xl:space-x-2 ">
 | 
				
			||||||
		{#if loading.base}
 | 
							<div class="custom-select-wrapper">
 | 
				
			||||||
			<select name="group" disabled class="w-96">
 | 
								<Select
 | 
				
			||||||
				<option selected value="">{$t('application.configuration.loading_groups')}</option>
 | 
									placeholder={loading.base
 | 
				
			||||||
			</select>
 | 
										? $t('application.configuration.loading_groups')
 | 
				
			||||||
		{:else}
 | 
										: $t('application.configuration.select_a_group')}
 | 
				
			||||||
			<select name="group" class="w-96" bind:value={selected.group} on:change={loadProjects}>
 | 
									id="group"
 | 
				
			||||||
				<option value="" disabled selected>{$t('application.configuration.select_a_group')}</option>
 | 
									showIndicator={!loading.base}
 | 
				
			||||||
				{#each groups as group}
 | 
									isWaiting={loading.base}
 | 
				
			||||||
					<option value={group}>{group.full_name}</option>
 | 
									on:select={selectGroup}
 | 
				
			||||||
				{/each}
 | 
									on:clear={() => {
 | 
				
			||||||
			</select>
 | 
										showSave = false;
 | 
				
			||||||
		{/if}
 | 
										projects = [];
 | 
				
			||||||
		{#if loading.projects}
 | 
										branches = [];
 | 
				
			||||||
			<select name="project" disabled class="w-96">
 | 
										selected.group = null;
 | 
				
			||||||
				<option selected value="">{$t('application.configuration.loading_projects')}</option>
 | 
										selected.project = null;
 | 
				
			||||||
			</select>
 | 
										selected.branch = null;
 | 
				
			||||||
		{:else if !loading.projects && projects.length > 0}
 | 
									}}
 | 
				
			||||||
			<select
 | 
									value={selected.group}
 | 
				
			||||||
				name="project"
 | 
									isDisabled={loading.base}
 | 
				
			||||||
				class="w-96"
 | 
									isClearable={false}
 | 
				
			||||||
				bind:value={selected.project}
 | 
									items={groups}
 | 
				
			||||||
				on:change={loadBranches}
 | 
									labelIdentifier="full_name"
 | 
				
			||||||
				disabled={!selected.group}
 | 
									optionIdentifier="id"
 | 
				
			||||||
			>
 | 
								/>
 | 
				
			||||||
				<option value="" disabled selected
 | 
							</div>
 | 
				
			||||||
					>{$t('application.configuration.select_a_project')}</option
 | 
							<div class="custom-select-wrapper">
 | 
				
			||||||
				>
 | 
								<Select
 | 
				
			||||||
				{#each projects as project}
 | 
									placeholder={loading.projects
 | 
				
			||||||
					<option value={project}>{project.name}</option>
 | 
										? $t('application.configuration.loading_projects')
 | 
				
			||||||
				{/each}
 | 
										: $t('application.configuration.select_a_project')}
 | 
				
			||||||
			</select>
 | 
									noOptionsMessage={$t('application.configuration.no_projects_found')}
 | 
				
			||||||
		{:else}
 | 
									id="project"
 | 
				
			||||||
			<select name="project" disabled class="w-96">
 | 
									showIndicator={!loading.projects}
 | 
				
			||||||
				<option disabled selected value=""
 | 
									isWaiting={loading.projects}
 | 
				
			||||||
					>{$t('application.configuration.no_projects_found')}</option
 | 
									isDisabled={loading.projects || !selected.group}
 | 
				
			||||||
				>
 | 
									on:select={selectProject}
 | 
				
			||||||
			</select>
 | 
									on:clear={() => {
 | 
				
			||||||
		{/if}
 | 
										showSave = false;
 | 
				
			||||||
 | 
										branches = [];
 | 
				
			||||||
		{#if loading.branches}
 | 
										selected.project = null;
 | 
				
			||||||
			<select name="branch" disabled class="w-96">
 | 
										selected.branch = null;
 | 
				
			||||||
				<option selected value="">{$t('application.configuration.loading_branches')}</option>
 | 
									}}
 | 
				
			||||||
			</select>
 | 
									value={selected.project}
 | 
				
			||||||
		{:else if !loading.branches && branches.length > 0}
 | 
									isClearable={false}
 | 
				
			||||||
			<select
 | 
									items={projects}
 | 
				
			||||||
				name="branch"
 | 
									loadOptions={searchProjects}
 | 
				
			||||||
				class="w-96"
 | 
									labelIdentifier="name"
 | 
				
			||||||
				bind:value={selected.branch}
 | 
									optionIdentifier="id"
 | 
				
			||||||
				on:change={isBranchAlreadyUsed}
 | 
								/>
 | 
				
			||||||
				disabled={!selected.project}
 | 
							</div>
 | 
				
			||||||
			>
 | 
							<div class="custom-select-wrapper">
 | 
				
			||||||
				<option value="" disabled selected>{$t('application.configuration.select_a_branch')}</option
 | 
								<Select
 | 
				
			||||||
				>
 | 
									placeholder={loading.branches
 | 
				
			||||||
				{#each branches as branch}
 | 
										? $t('application.configuration.loading_branches')
 | 
				
			||||||
					<option value={branch}>{branch.name}</option>
 | 
										: $t('application.configuration.select_a_branch')}
 | 
				
			||||||
				{/each}
 | 
									noOptionsMessage={$t('application.configuration.no_branches_found')}
 | 
				
			||||||
			</select>
 | 
									id="branch"
 | 
				
			||||||
		{:else}
 | 
									showIndicator={!loading.branches}
 | 
				
			||||||
			<select name="project" disabled class="w-96">
 | 
									isWaiting={loading.branches}
 | 
				
			||||||
				<option disabled selected value=""
 | 
									isDisabled={loading.branches || !selected.project}
 | 
				
			||||||
					>{$t('application.configuration.no_branches_found')}</option
 | 
									on:select={selectBranch}
 | 
				
			||||||
				>
 | 
									on:clear={() => {
 | 
				
			||||||
			</select>
 | 
										showSave = false;
 | 
				
			||||||
		{/if}
 | 
										selected.branch = null;
 | 
				
			||||||
 | 
									}}
 | 
				
			||||||
 | 
									value={selected.branch}
 | 
				
			||||||
 | 
									isClearable={false}
 | 
				
			||||||
 | 
									items={branches}
 | 
				
			||||||
 | 
									loadOptions={searchBranches}
 | 
				
			||||||
 | 
									labelIdentifier="name"
 | 
				
			||||||
 | 
									optionIdentifier="web_url"
 | 
				
			||||||
 | 
								/>
 | 
				
			||||||
 | 
							</div>
 | 
				
			||||||
	</div>
 | 
						</div>
 | 
				
			||||||
	<div class="flex flex-col items-center justify-center space-y-4 pt-5">
 | 
						<div class="flex flex-col items-center justify-center space-y-4 pt-5">
 | 
				
			||||||
		<button
 | 
							<button
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user