[cli] Support multiple remote URLs in Git config (#8145)

Two features that handle a user's local Git config have been shipped:

- #8100 
- #7910 

Both of these features currently pull only from the user's remote origin URL. This covers 90% of cases, but there are cases in which the user has more than one remote URL, and may want to use one other than the origin URL, especially in `vc git connect`. This PR:

- Adds support for multiple remote URLs in a user's Git config
- Updates `vc git connect` to prompt the user to select a URL if they have multiple remote URLs
- Updates `createGitMeta` to send the connected Git repository url by default, origin url otherwise

### 📋 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
This commit is contained in:
Matthew Stanciu
2022-07-20 00:17:53 -07:00
committed by GitHub
parent e5421c27e8
commit 0140db38fa
25 changed files with 362 additions and 89 deletions

View File

@@ -25,7 +25,7 @@ describe('git', () => {
id: 'unlinked',
name: 'unlinked',
});
client.setArgv('projects', 'connect');
client.setArgv('git', 'connect');
const gitPromise = git(client);
await expect(client.stderr).toOutput('Set up');
@@ -40,7 +40,7 @@ describe('git', () => {
client.stdin.write('y\n');
await expect(client.stderr).toOutput(
`Identified Git remote "origin": https://github.com/user/repo.git`
`Connecting Git remote: https://github.com/user/repo.git`
);
const exitCode = await gitPromise;
@@ -76,7 +76,7 @@ describe('git', () => {
id: 'no-git-config',
name: 'no-git-config',
});
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const exitCode = await git(client);
expect(exitCode).toEqual(1);
await expect(client.stderr).toOutput(
@@ -98,11 +98,11 @@ describe('git', () => {
id: 'no-remote-url',
name: 'no-remote-url',
});
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const exitCode = await git(client);
expect(exitCode).toEqual(1);
await expect(client.stderr).toOutput(
`Error! No remote origin URL found in your Git config. Make sure you've configured a remote repo in your local Git config. Run \`git remote --help\` for more details.`
`Error! No remote URLs found in your Git config. Make sure you've configured a remote repo in your local Git config. Run \`git remote --help\` for more details.`
);
} finally {
await fs.rename(join(cwd, '.git'), join(cwd, 'git'));
@@ -121,15 +121,15 @@ describe('git', () => {
id: 'bad-remote-url',
name: 'bad-remote-url',
});
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const exitCode = await git(client);
expect(exitCode).toEqual(1);
await expect(client.stderr).toOutput(
`Identified Git remote "origin": bababooey`
`Connecting Git remote: bababooey`
);
await expect(client.stderr).toOutput(
`Error! Failed to parse Git repo data from the following remote URL in your Git config: bababooey\n`
`Error! Failed to parse Git repo data from the following remote URL: bababooey\n`
);
} finally {
await fs.rename(join(cwd, '.git'), join(cwd, 'git'));
@@ -148,11 +148,11 @@ describe('git', () => {
id: 'new-connection',
name: 'new-connection',
});
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const gitPromise = git(client);
await expect(client.stderr).toOutput(
`Identified Git remote "origin": https://github.com/user/repo`
`Connecting Git remote: https://github.com/user/repo`
);
await expect(client.stderr).toOutput(
`> Connected GitHub repository user/repo!\n`
@@ -201,11 +201,11 @@ describe('git', () => {
updatedAt: 1656109539791,
};
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const gitPromise = git(client);
await expect(client.stderr).toOutput(
`Identified Git remote "origin": https://github.com/user2/repo2`
`Connecting Git remote: https://github.com/user2/repo2`
);
await expect(client.stderr).toOutput(
`> Connected GitHub repository user2/repo2!\n`
@@ -253,11 +253,11 @@ describe('git', () => {
createdAt: 1656109539791,
updatedAt: 1656109539791,
};
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const gitPromise = git(client);
await expect(client.stderr).toOutput(
`Identified Git remote "origin": https://github.com/user/repo`
`Connecting Git remote: https://github.com/user/repo`
);
await expect(client.stderr).toOutput(
`> user/repo is already connected to your project.\n`
@@ -283,11 +283,11 @@ describe('git', () => {
name: 'invalid-repo',
});
client.setArgv('projects', 'connect', '--confirm');
client.setArgv('git', 'connect', '--confirm');
const gitPromise = git(client);
await expect(client.stderr).toOutput(
`Identified Git remote "origin": https://github.com/laksfj/asdgklsadkl`
`Connecting Git remote: https://github.com/laksfj/asdgklsadkl`
);
await expect(client.stderr).toOutput(
`Failed to link laksfj/asdgklsadkl. Make sure there aren't any typos and that you have access to the repository if it's private.`
@@ -300,6 +300,56 @@ describe('git', () => {
process.chdir(originalCwd);
}
});
it('should connect the default option of multiple remotes', async () => {
const cwd = fixture('multiple-remotes');
try {
process.chdir(cwd);
await fs.rename(join(cwd, 'git'), join(cwd, '.git'));
useUser();
useTeams('team_dummy');
useProject({
...defaultProject,
id: 'multiple-remotes',
name: 'multiple-remotes',
});
client.setArgv('git', 'connect');
const gitPromise = git(client);
await expect(client.stderr).toOutput('Found multiple remote URLs.');
await expect(client.stderr).toOutput(
'Which remote do you want to connect?'
);
client.stdin.write('\r');
await expect(client.stderr).toOutput(
'Connecting Git remote: https://github.com/user/repo.git'
);
await expect(client.stderr).toOutput(
'Connected GitHub repository user/repo!'
);
const exitCode = await gitPromise;
expect(exitCode).toEqual(0);
const project: Project = await client.fetch(
`/v8/projects/multiple-remotes`
);
expect(project.link).toMatchObject({
type: 'github',
repo: 'user/repo',
repoId: 1010,
gitCredentialId: '',
sourceless: true,
createdAt: 1656109539791,
updatedAt: 1656109539791,
});
} finally {
await fs.rename(join(cwd, '.git'), join(cwd, 'git'));
process.chdir(originalCwd);
}
});
});
describe('disconnect', () => {
const originalCwd = process.cwd();