-
Notifications
You must be signed in to change notification settings - Fork 736
Expand file tree
/
Copy pathgithubRef.ts
More file actions
33 lines (30 loc) · 1.44 KB
/
githubRef.ts
File metadata and controls
33 lines (30 loc) · 1.44 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Protocol } from './protocol';
import { parseRemote } from './remote';
import { Remote, Repository } from '../api/api';
export class GitHubRef {
public repositoryCloneUrl: Protocol;
constructor(public ref: string, public label: string, public sha: string, repositoryCloneUrl: string,
public readonly owner: string, public readonly name: string, public readonly isInOrganization: boolean) {
this.repositoryCloneUrl = new Protocol(repositoryCloneUrl);
}
}
export function findLocalRepoRemoteFromGitHubRef(repository: Repository, gitHubRef: GitHubRef): Remote | undefined {
const targetRepo = gitHubRef.repositoryCloneUrl.repositoryName.toLowerCase();
const targetOwner = gitHubRef.owner.toLowerCase();
for (const remote of repository.state.remotes) {
const url = remote.fetchUrl ?? remote.pushUrl;
if (!url) {
continue;
}
const parsedRemote = parseRemote(remote.name, url);
const parsedOwner = parsedRemote?.owner.toLowerCase();
const parsedRepo = parsedRemote?.repositoryName.toLowerCase();
if (parsedOwner === targetOwner && parsedRepo === targetRepo) {
return remote;
}
}
}