Skip to content

Commit 5ba2a5a

Browse files
authored
Merge pull request #1865 from github/shati-patel/gh-nwo
Extract github nwo helper functions
2 parents e18a330 + 674a126 commit 5ba2a5a

3 files changed

Lines changed: 57 additions & 51 deletions

File tree

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import { showAndLogInformationMessage, tmpDir } from "./helpers";
2121
import { reportStreamProgress, ProgressCallback } from "./commandRunner";
2222
import { extLogger } from "./common";
2323
import { Credentials } from "./authentication";
24-
import { REPO_REGEX, getErrorMessage } from "./pure/helpers-pure";
24+
import { getErrorMessage } from "./pure/helpers-pure";
25+
import {
26+
convertGitHubUrlToNwo,
27+
looksLikeGithubRepo,
28+
} from "./databases/github-nwo";
2529

2630
/**
2731
* Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file.
@@ -509,55 +513,6 @@ export async function findDirWithFile(
509513
return;
510514
}
511515

512-
/**
513-
* The URL pattern is https://github.com/{owner}/{name}/{subpages}.
514-
*
515-
* This function accepts any URL that matches the pattern above. It also accepts just the
516-
* name with owner (NWO): `<owner>/<repo>`.
517-
*
518-
* @param githubRepo The GitHub repository URL or NWO
519-
*
520-
* @return true if this looks like a valid GitHub repository URL or NWO
521-
*/
522-
export function looksLikeGithubRepo(
523-
githubRepo: string | undefined,
524-
): githubRepo is string {
525-
if (!githubRepo) {
526-
return false;
527-
}
528-
if (REPO_REGEX.test(githubRepo) || convertGitHubUrlToNwo(githubRepo)) {
529-
return true;
530-
}
531-
return false;
532-
}
533-
534-
/**
535-
* Converts a GitHub repository URL to the corresponding NWO.
536-
* @param githubUrl The GitHub repository URL
537-
* @return The corresponding NWO, or undefined if the URL is not valid
538-
*/
539-
function convertGitHubUrlToNwo(githubUrl: string): string | undefined {
540-
try {
541-
const uri = Uri.parse(githubUrl, true);
542-
if (uri.scheme !== "https") {
543-
return;
544-
}
545-
if (uri.authority !== "github.com" && uri.authority !== "www.github.com") {
546-
return;
547-
}
548-
const paths = uri.path.split("/").filter((segment: string) => segment);
549-
const nwo = `${paths[0]}/${paths[1]}`;
550-
if (REPO_REGEX.test(nwo)) {
551-
return nwo;
552-
}
553-
return;
554-
} catch (e) {
555-
// Ignore the error here, since we catch failures at a higher level.
556-
// In particular: returning undefined leads to an error in 'promptImportGithubDatabase'.
557-
return;
558-
}
559-
}
560-
561516
export async function convertGithubNwoToDatabaseUrl(
562517
githubRepo: string,
563518
octokit: Octokit.Octokit,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Uri } from "vscode";
2+
import { REPO_REGEX } from "../pure/helpers-pure";
3+
4+
/**
5+
* The URL pattern is https://github.com/{owner}/{name}/{subpages}.
6+
*
7+
* This function accepts any URL that matches the pattern above. It also accepts just the
8+
* name with owner (NWO): `<owner>/<repo>`.
9+
*
10+
* @param githubRepo The GitHub repository URL or NWO
11+
*
12+
* @return true if this looks like a valid GitHub repository URL or NWO
13+
*/
14+
export function looksLikeGithubRepo(
15+
githubRepo: string | undefined,
16+
): githubRepo is string {
17+
if (!githubRepo) {
18+
return false;
19+
}
20+
if (REPO_REGEX.test(githubRepo) || convertGitHubUrlToNwo(githubRepo)) {
21+
return true;
22+
}
23+
return false;
24+
}
25+
26+
/**
27+
* Converts a GitHub repository URL to the corresponding NWO.
28+
* @param githubUrl The GitHub repository URL
29+
* @return The corresponding NWO, or undefined if the URL is not valid
30+
*/
31+
export function convertGitHubUrlToNwo(githubUrl: string): string | undefined {
32+
try {
33+
const uri = Uri.parse(githubUrl, true);
34+
if (uri.scheme !== "https") {
35+
return;
36+
}
37+
if (uri.authority !== "github.com" && uri.authority !== "www.github.com") {
38+
return;
39+
}
40+
const paths = uri.path.split("/").filter((segment: string) => segment);
41+
const nwo = `${paths[0]}/${paths[1]}`;
42+
if (REPO_REGEX.test(nwo)) {
43+
return nwo;
44+
}
45+
return;
46+
} catch (e) {
47+
// Ignore the error here, since we catch failures at a higher level.
48+
// In particular: returning undefined leads to an error in 'promptImportGithubDatabase'.
49+
return;
50+
}
51+
}

extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import {
88
convertLgtmUrlToDatabaseUrl,
99
looksLikeLgtmUrl,
1010
findDirWithFile,
11-
looksLikeGithubRepo,
1211
} from "../../databaseFetcher";
1312
import * as Octokit from "@octokit/rest";
13+
import { looksLikeGithubRepo } from "../../databases/github-nwo";
1414

1515
// These tests make API calls and may need extra time to complete.
1616
jest.setTimeout(10000);

0 commit comments

Comments
 (0)