Extract github nwo helper functions

This commit is contained in:
shati-patel
2022-12-13 12:58:46 +00:00
parent e805ef37eb
commit 674a126078
3 changed files with 57 additions and 51 deletions

View File

@@ -21,7 +21,11 @@ import { showAndLogInformationMessage, tmpDir } from "./helpers";
import { reportStreamProgress, ProgressCallback } from "./commandRunner";
import { extLogger } from "./common";
import { Credentials } from "./authentication";
import { REPO_REGEX, getErrorMessage } from "./pure/helpers-pure";
import { getErrorMessage } from "./pure/helpers-pure";
import {
convertGitHubUrlToNwo,
looksLikeGithubRepo,
} from "./databases/github-nwo";
/**
* 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(
return;
}
/**
* The URL pattern is https://github.com/{owner}/{name}/{subpages}.
*
* This function accepts any URL that matches the pattern above. It also accepts just the
* name with owner (NWO): `<owner>/<repo>`.
*
* @param githubRepo The GitHub repository URL or NWO
*
* @return true if this looks like a valid GitHub repository URL or NWO
*/
export function looksLikeGithubRepo(
githubRepo: string | undefined,
): githubRepo is string {
if (!githubRepo) {
return false;
}
if (REPO_REGEX.test(githubRepo) || convertGitHubUrlToNwo(githubRepo)) {
return true;
}
return false;
}
/**
* Converts a GitHub repository URL to the corresponding NWO.
* @param githubUrl The GitHub repository URL
* @return The corresponding NWO, or undefined if the URL is not valid
*/
function convertGitHubUrlToNwo(githubUrl: string): string | undefined {
try {
const uri = Uri.parse(githubUrl, true);
if (uri.scheme !== "https") {
return;
}
if (uri.authority !== "github.com" && uri.authority !== "www.github.com") {
return;
}
const paths = uri.path.split("/").filter((segment: string) => segment);
const nwo = `${paths[0]}/${paths[1]}`;
if (REPO_REGEX.test(nwo)) {
return nwo;
}
return;
} catch (e) {
// Ignore the error here, since we catch failures at a higher level.
// In particular: returning undefined leads to an error in 'promptImportGithubDatabase'.
return;
}
}
export async function convertGithubNwoToDatabaseUrl(
githubRepo: string,
octokit: Octokit.Octokit,

View File

@@ -0,0 +1,51 @@
import { Uri } from "vscode";
import { REPO_REGEX } from "../pure/helpers-pure";
/**
* The URL pattern is https://github.com/{owner}/{name}/{subpages}.
*
* This function accepts any URL that matches the pattern above. It also accepts just the
* name with owner (NWO): `<owner>/<repo>`.
*
* @param githubRepo The GitHub repository URL or NWO
*
* @return true if this looks like a valid GitHub repository URL or NWO
*/
export function looksLikeGithubRepo(
githubRepo: string | undefined,
): githubRepo is string {
if (!githubRepo) {
return false;
}
if (REPO_REGEX.test(githubRepo) || convertGitHubUrlToNwo(githubRepo)) {
return true;
}
return false;
}
/**
* Converts a GitHub repository URL to the corresponding NWO.
* @param githubUrl The GitHub repository URL
* @return The corresponding NWO, or undefined if the URL is not valid
*/
export function convertGitHubUrlToNwo(githubUrl: string): string | undefined {
try {
const uri = Uri.parse(githubUrl, true);
if (uri.scheme !== "https") {
return;
}
if (uri.authority !== "github.com" && uri.authority !== "www.github.com") {
return;
}
const paths = uri.path.split("/").filter((segment: string) => segment);
const nwo = `${paths[0]}/${paths[1]}`;
if (REPO_REGEX.test(nwo)) {
return nwo;
}
return;
} catch (e) {
// Ignore the error here, since we catch failures at a higher level.
// In particular: returning undefined leads to an error in 'promptImportGithubDatabase'.
return;
}
}

View File

@@ -8,9 +8,9 @@ import {
convertLgtmUrlToDatabaseUrl,
looksLikeLgtmUrl,
findDirWithFile,
looksLikeGithubRepo,
} from "../../databaseFetcher";
import * as Octokit from "@octokit/rest";
import { looksLikeGithubRepo } from "../../databases/github-nwo";
// These tests make API calls and may need extra time to complete.
jest.setTimeout(10000);