Skip to content

Commit 08cde46

Browse files
committed
fix(gitGateway): make fetchWithTimeout handle full URLs, remove wrapper functions
1 parent d679f64 commit 08cde46

File tree

1 file changed

+28
-51
lines changed

1 file changed

+28
-51
lines changed

cypress/plugins/gitGateway.js

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ async function fetchWithTimeout(netlifyApiToken, path, method = 'GET', payload =
5151
options.body = JSON.stringify(payload);
5252
}
5353

54-
const response = await fetch(`${apiRoot}${path}`, options);
54+
// Handle both full URLs and API paths
55+
const url = path.startsWith('http://') || path.startsWith('https://') ? path : `${apiRoot}${path}`;
56+
const response = await fetch(url, options);
5557
clearTimeout(timeout);
5658

5759
return parseAs === 'json' ? response.json() : response.text();
@@ -65,27 +67,6 @@ async function fetchWithTimeout(netlifyApiToken, path, method = 'GET', payload =
6567
}
6668
}
6769

68-
async function createSite(netlifyApiToken, payload) {
69-
return fetchWithTimeout(netlifyApiToken, 'sites', 'POST', payload);
70-
}
71-
72-
async function enableIdentity(netlifyApiToken, siteId) {
73-
return fetchWithTimeout(netlifyApiToken, `sites/${siteId}/identity`, 'POST', {});
74-
}
75-
76-
async function enableGitGateway(netlifyApiToken, siteId, provider, token, repo) {
77-
return fetchWithTimeout(netlifyApiToken, `sites/${siteId}/services/git/instances`, 'POST', {
78-
[provider]: {
79-
repo,
80-
access_token: token,
81-
},
82-
});
83-
}
84-
85-
async function enableLargeMedia(netlifyApiToken, siteId) {
86-
return fetchWithTimeout(netlifyApiToken, `sites/${siteId}/services/large-media/instances`, 'POST', {});
87-
}
88-
8970
async function waitForDeploys(netlifyApiToken, siteId) {
9071
const maxRetries = 5;
9172
const retryDelayMs = 15 * 1000; // 15 seconds between retries
@@ -112,23 +93,6 @@ async function waitForDeploys(netlifyApiToken, siteId) {
11293
throw new Error(`Timed out waiting for deploy of site ${siteId} after ${maxRetries * retryDelayMs / 1000}s`);
11394
}
11495

115-
async function createUser(netlifyApiToken, siteUrl, email, password) {
116-
const response = await fetch(`${siteUrl}/.netlify/functions/create-user`, {
117-
method: 'POST',
118-
headers: {
119-
'Content-Type': 'application/json',
120-
Authorization: `Bearer ${netlifyApiToken}`,
121-
},
122-
body: JSON.stringify({ email, password }),
123-
});
124-
125-
if (response.ok) {
126-
console.log('User created successfully');
127-
} else {
128-
throw new Error('Failed to create user');
129-
}
130-
}
131-
13296
const netlifySiteURL = 'https://fake-site-url.netlify.com/';
13397
const email = 'decap@p-m.si';
13498
const password = '12345678';
@@ -143,7 +107,8 @@ const methods = {
143107
transformData: transformGitHub,
144108
createSite: (netlifyApiToken, result) => {
145109
const { installationId } = getEnvs();
146-
return createSite(netlifyApiToken, {
110+
// Create a new Netlify site connected to GitHub repository
111+
return fetchWithTimeout(netlifyApiToken, 'sites', 'POST', {
147112
repo: {
148113
provider: 'github',
149114
installation_id: installationId,
@@ -160,6 +125,7 @@ const methods = {
160125
teardownTest: teardownGitLabTest,
161126
transformData: transformGitLab,
162127
createSite: async (netlifyApiToken, result) => {
128+
// Generate a deploy key for GitLab
163129
const { id, public_key } = await fetchWithTimeout(netlifyApiToken, 'deploy_keys', 'POST');
164130
const { gitlabToken } = getEnvs();
165131
const project = `${result.owner}/${result.repo}`;
@@ -172,7 +138,8 @@ const methods = {
172138
body: JSON.stringify({ title: 'Netlify Deploy Key', key: public_key, can_push: false }),
173139
}).then(res => res.json());
174140

175-
const site = await createSite(netlifyApiToken, {
141+
// Create a new Netlify site connected to GitLab repository
142+
const site = await fetchWithTimeout(netlifyApiToken, 'sites', 'POST', {
176143
account_slug: result.owner,
177144
repo: {
178145
provider: 'gitlab',
@@ -218,20 +185,22 @@ async function setupGitGateway(options) {
218185
}
219186

220187
console.log('Enabling identity for site:', site_id);
221-
await enableIdentity(netlifyApiToken, site_id);
188+
// Enable Netlify Identity
189+
await fetchWithTimeout(netlifyApiToken, `sites/${site_id}/identity`, 'POST', {});
222190

223191
console.log('Enabling git gateway for site:', site_id);
224192
const token = methods[provider].token();
225-
await enableGitGateway(
226-
netlifyApiToken,
227-
site_id,
228-
provider,
229-
token,
230-
`${result.owner}/${result.repo}`,
231-
);
193+
// Enable Git Gateway
194+
await fetchWithTimeout(netlifyApiToken, `sites/${site_id}/services/git/instances`, 'POST', {
195+
[provider]: {
196+
repo: `${result.owner}/${result.repo}`,
197+
access_token: token,
198+
},
199+
});
232200

233201
console.log('Enabling large media for site:', site_id);
234-
await enableLargeMedia(netlifyApiToken, site_id);
202+
// Enable Large Media
203+
await fetchWithTimeout(netlifyApiToken, `sites/${site_id}/services/large-media/instances`, 'POST', {});
235204

236205
const git = getGitClient(result.tempDir);
237206
await git.raw([
@@ -250,7 +219,15 @@ async function setupGitGateway(options) {
250219
console.log('Creating user for site:', site_id, 'with email:', email);
251220

252221
try {
253-
await createUser(netlifyApiToken, ssl_url, email, password);
222+
// Create a user account via Netlify Identity
223+
await fetchWithTimeout(
224+
netlifyApiToken,
225+
`${ssl_url}/.netlify/functions/create-user`,
226+
'POST',
227+
{ email, password },
228+
'text'
229+
);
230+
console.log('User created successfully');
254231
} catch (e) {
255232
console.log(e);
256233
}

0 commit comments

Comments
 (0)