Skip to content

Commit 9963965

Browse files
[cli] Fix vc link not respecting --yes (vercel#8412)
As of 28.0.0, `vc link` stopped respecting `--yes` and would wait for prompt if it found a Git repository to connect. This PR passes `autoConfirm` through to the Git prompt, so that `vc link` continues to respect `--yes`. Here's what `vc link` looks like after this PR if a user has a local Git repository: <img width="513" alt="Screen Shot 2022-08-16 at 10 05 56 AM" src="https://user-images.githubusercontent.com/14811170/184899917-c3dc0603-370d-4c86-afb8-19758fbc051a.png"> Not sure if we want to remove the log messages to be consistent with the rest of `vc link`? ### 📋 Checklist <!-- Please keep your PR as a Draft until the checklist is complete --> #### Tests - [x] The code changed/added as part of this PR has been covered with tests - [x] All tests pass locally with `yarn test-unit` #### Code Review - [ ] This PR has a concise title and thorough description useful to a reviewer - [ ] Issue from task tracker has a link to this PR
1 parent f3ed279 commit 9963965

File tree

5 files changed

+103
-16
lines changed

5 files changed

+103
-16
lines changed

packages/cli/src/util/link/add-git-connection.ts

+35-15
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function addGitConnection(
2828
org: Org,
2929
project: Project,
3030
remoteUrls: Dictionary<string>,
31+
autoConfirm: Boolean,
3132
settings?: ProjectSettings
3233
): Promise<number | void> {
3334
if (!settings) {
@@ -39,15 +40,17 @@ export async function addGitConnection(
3940
org,
4041
project,
4142
remoteUrls,
42-
settings || project
43+
settings || project,
44+
autoConfirm
4345
);
4446
} else if (Object.keys(remoteUrls).length > 1 && !project.link) {
4547
return addMultipleGitRemotes(
4648
client,
4749
org,
4850
project,
4951
remoteUrls,
50-
settings || project
52+
settings || project,
53+
autoConfirm
5154
);
5255
}
5356
}
@@ -57,7 +60,8 @@ async function addSingleGitRemote(
5760
org: Org,
5861
project: Project,
5962
remoteUrls: Dictionary<string>,
60-
settings: ProjectSettings
63+
settings: ProjectSettings,
64+
autoConfirm: Boolean
6165
) {
6266
const [remoteName, remoteUrl] = Object.entries(remoteUrls)[0];
6367
const repoInfo = parseRepoUrl(remoteUrl);
@@ -81,27 +85,43 @@ async function addSingleGitRemote(
8185
(project.link.org !== parsedOrg ||
8286
project.link.repo !== repo ||
8387
project.link.type !== provider);
84-
const shouldConnect = await promptGitConnectSingleUrl(
88+
89+
let shouldConnectOption: string | undefined;
90+
if (autoConfirm) {
91+
shouldConnectOption = 'yes';
92+
} else {
93+
shouldConnectOption = await promptGitConnectSingleUrl(
94+
client,
95+
project,
96+
remoteName,
97+
remoteUrl,
98+
replace
99+
);
100+
}
101+
return handleOptions(
102+
shouldConnectOption,
85103
client,
104+
org,
86105
project,
87-
remoteName,
88-
remoteUrl,
89-
replace
106+
settings,
107+
repoInfo
90108
);
91-
return handleOptions(shouldConnect, client, org, project, settings, repoInfo);
92109
}
93110

94111
async function addMultipleGitRemotes(
95112
client: Client,
96113
org: Org,
97114
project: Project,
98115
remoteUrls: Dictionary<string>,
99-
settings: ProjectSettings
116+
settings: ProjectSettings,
117+
autoConfirm: Boolean
100118
) {
101-
client.output.log('Found multiple Git remote URLs in Git config.');
102-
const remoteUrlOrOptions = await promptGitConnectMultipleUrls(
103-
client,
104-
remoteUrls
105-
);
106-
return handleOptions(remoteUrlOrOptions, client, org, project, settings);
119+
let remoteUrl: string | undefined;
120+
if (autoConfirm) {
121+
remoteUrl = remoteUrls['origin'] || Object.values(remoteUrls)[0];
122+
} else {
123+
client.output.log('Found multiple Git remote URLs in Git config.');
124+
remoteUrl = await promptGitConnectMultipleUrls(client, remoteUrls);
125+
}
126+
return handleOptions(remoteUrl, client, org, project, settings);
107127
}

packages/cli/src/util/link/setup-and-link.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ export default async function setupAndLink(
136136
client,
137137
org,
138138
project,
139-
remoteUrls
139+
remoteUrls,
140+
autoConfirm
140141
);
141142
if (typeof connectGit === 'number') {
142143
return { status: 'error', exitCode: connectGit };
@@ -264,6 +265,7 @@ export default async function setupAndLink(
264265
org,
265266
project,
266267
remoteUrls,
268+
autoConfirm,
267269
settings
268270
);
269271
if (typeof connectGit === 'number') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!.vercel
2+
.vercel

packages/cli/test/fixtures/unit/link-connect-git/multiple-remotes-prefer-origin/git/config

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/test/unit/commands/link.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -306,5 +306,52 @@ describe('link', () => {
306306
process.chdir(originalCwd);
307307
}
308308
});
309+
it('should respect --yes', async () => {
310+
const cwd = fixture('single-remote');
311+
try {
312+
process.chdir(cwd);
313+
await fs.rename(join(cwd, 'git'), join(cwd, '.git'));
314+
315+
useUser();
316+
useProject({
317+
...defaultProject,
318+
name: 'single-remote',
319+
id: 'single-remote',
320+
});
321+
useTeams('team_dummy');
322+
client.setArgv('--yes');
323+
const linkPromise = link(client);
324+
expect(client.stderr).not.toOutput('Do you want to connect "origin"');
325+
await expect(client.stderr).toOutput('Linked to');
326+
await expect(linkPromise).resolves.toEqual(0);
327+
} finally {
328+
await fs.rename(join(cwd, '.git'), join(cwd, 'git'));
329+
process.chdir(originalCwd);
330+
}
331+
});
332+
it('should respect --yes for multiple remotes when origin is not the first', async () => {
333+
const cwd = fixture('multiple-remotes-prefer-origin');
334+
try {
335+
process.chdir(cwd);
336+
await fs.rename(join(cwd, 'git'), join(cwd, '.git'));
337+
useUser();
338+
useProject({
339+
...defaultProject,
340+
name: 'multiple-remotes-prefer-origin',
341+
id: 'multiple-remotes-prefer-origin',
342+
});
343+
useTeams('team_dummy');
344+
client.setArgv('--yes');
345+
const linkPromise = link(client);
346+
expect(client.stderr).not.toOutput('Found multiple Git remote URLs');
347+
await expect(client.stderr).toOutput(
348+
'Connected GitHub repository user/repo'
349+
);
350+
await expect(linkPromise).resolves.toEqual(0);
351+
} finally {
352+
await fs.rename(join(cwd, '.git'), join(cwd, 'git'));
353+
process.chdir(originalCwd);
354+
}
355+
});
309356
});
310357
});

0 commit comments

Comments
 (0)