-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathcreateRepository.svelte
More file actions
174 lines (161 loc) 路 6.91 KB
/
Copy pathcreateRepository.svelte
File metadata and controls
174 lines (161 loc) 路 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<script lang="ts">
import { page } from '$app/stores';
import Button from '$lib/elements/forms/button.svelte';
import FormList from '$lib/elements/forms/formList.svelte';
import InputChoice from '$lib/elements/forms/inputChoice.svelte';
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
import InputText from '$lib/elements/forms/inputText.svelte';
import { WizardStep } from '$lib/layout';
import { addNotification } from '$lib/stores/notifications';
import { sdk } from '$lib/stores/sdk';
import { onMount } from 'svelte';
import Repositories from '../components/repositories.svelte';
import { installation, repository, template, templateConfig } from '../store';
import { Box } from '$lib/components';
let selectedInstallationId: string;
let hasInstallations: boolean;
let selectedRepository: string;
onMount(() => {
$templateConfig.repositoryPrivate = true;
});
async function beforeSubmit() {
if (!hasInstallations || !$installation) {
throw new Error('Please connect a Git provider');
}
if ($templateConfig.repositoryBehaviour === 'new') {
const repo = await sdk.forProject.vcs.createRepository(
$installation.$id,
$templateConfig.repositoryName,
$templateConfig.repositoryPrivate
);
$repository = repo;
addNotification({
type: 'success',
message: 'Repository successfully created.'
});
}
if (!$repository) {
throw new Error('Please select a repository');
}
}
function connectGitHub() {
const redirect = new URL($page.url);
const callbackState = {
from: 'github',
to: 'template',
step: '4',
template: $template.id,
templateConfig: JSON.stringify($templateConfig)
};
if (callbackState) {
Object.keys(callbackState).forEach((key) => {
redirect.searchParams.append(key, callbackState[key]);
});
}
const target = new URL(`${sdk.forProject.client.config.endpoint}/vcs/github/authorize`);
target.searchParams.set('project', $page.params.project);
target.searchParams.set('success', redirect.toString());
target.searchParams.set('failure', redirect.toString());
target.searchParams.set('mode', 'admin');
return target;
}
async function loadInstallations() {
const { installations } = await sdk.forProject.vcs.listInstallations();
if (installations.length) {
$installation = installations[0];
hasInstallations = true;
}
return installations;
}
function getProviderIcon(provider: string) {
switch (provider) {
case 'github':
return 'icon-github';
default:
return '';
}
}
</script>
<WizardStep {beforeSubmit}>
<svelte:fragment slot="title">Repository</svelte:fragment>
<svelte:fragment slot="subtitle">
Select a Git repository that will trigger your function deployments when updated.
</svelte:fragment>
{#if $templateConfig.repositoryBehaviour === 'existing'}
<Repositories bind:hasInstallations bind:selectedRepository />
{:else}
{#await loadInstallations()}
<div class="u-flex u-gap-8 u-cross-center u-main-center">
<div class="loader u-margin-32" />
</div>
{:then installations}
{#if hasInstallations}
<div class="u-flex u-gap-16">
<div class="u-width-full-line">
<InputSelect
id="installation"
label="Git organization"
options={installations.map((entry) => {
return {
label: entry.organization,
value: entry.$id
};
})}
on:change={() => {
$installation = installations.find(
(entry) => entry.$id === selectedInstallationId
);
}}
bind:value={selectedInstallationId} />
</div>
</div>
{:else}
<div class="u-flex u-cross-center u-flex-vertical u-gap-16">
<Button href={connectGitHub().toString()} fullWidth secondary>
<span class="icon-github" aria-hidden="true" />
<span class="text">GitHub</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-gitlab" aria-hidden="true" />
<span class="text">GitLab (coming soon)</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-bitBucket" aria-hidden="true" />
<span class="text">BitBucket (coming soon)</span>
</Button>
<Button disabled fullWidth secondary>
<span class="icon-azure" aria-hidden="true" />
<span class="text">Azure (coming soon)</span>
</Button>
</div>
{/if}
{#if $installation}
<Box radius="small" class="u-margin-block-start-20">
<div class="u-flex u-gap-16">
<div class="avatar is-size-x-small">
<span class={getProviderIcon($installation.provider)} />
</div>
<div class="u-cross-child-center u-line-height-1-5">
<h6 class="u-bold u-trim-1">
{$installation.organization}/{$templateConfig.repositoryName}
</h6>
</div>
</div>
<div class="u-margin-block-start-24">
<FormList>
<InputText
id="repositoryName"
label="Repository name"
placeholder="my-repository"
bind:value={$templateConfig.repositoryName} />
<InputChoice
id="repositoryPrivate"
label="Keep repository private"
bind:value={$templateConfig.repositoryPrivate} />
</FormList>
</div>
</Box>
{/if}
{/await}
{/if}
</WizardStep>