Skip to content

Commit

Permalink
Automatically add upstream (#2560)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane authored Mar 10, 2021
1 parent e48713c commit ae60656
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/github/folderRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ export class FolderRepositoryManager implements vscode.Disposable {
this._githubRepositories.some(newRepo => newRepo.remote.equals(oldRepo.remote)),
);

if (this._githubRepositories.length && repositoriesChanged) {
this.checkIfMissingUpstream();
}

this.getMentionableUsers(repositoriesChanged);
this.getAssignableUsers(repositoriesChanged);
this._onDidLoadRepositories.fire(
Expand All @@ -500,6 +504,30 @@ export class FolderRepositoryManager implements vscode.Disposable {
});
}

private async checkIfMissingUpstream(): Promise<void> {
try {
const origin = await this.getOrigin();
const metadata = await origin.getMetadata();
if (metadata.fork) {
const parentUrl = new Protocol(metadata.parent.git_url);
const missingParentRemote = !this._githubRepositories.some(
repo =>
repo.remote.owner === parentUrl.owner &&
repo.remote.repositoryName === parentUrl.repositoryName,
);

if (missingParentRemote) {
const upstreamAvailable = !this.repository.state.remotes.some(remote => remote.name === 'upstream');
const remoteName = upstreamAvailable ? 'upstream' : metadata.parent.owner.login;
await this.repository.addRemote(remoteName, metadata.parent.git_url);
}
}
} catch (e) {
Logger.appendLine(`Missing upstream check failed: ${e}`);
// ignore
}
}

getAllAssignableUsers(): IAccount[] | undefined {
if (this._assignableUsers) {
const allAssignableUsers: IAccount[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/github/githubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class GitHubRepository implements vscode.Disposable {
return defaultSchema as any;
}

async getMetadata(): Promise<any> {
async getMetadata(): Promise<IMetadata> {
Logger.debug(`Fetch metadata - enter`, GitHubRepository.ID);
if (this._metadata) {
Logger.debug(
Expand Down

0 comments on commit ae60656

Please sign in to comment.