-
Notifications
You must be signed in to change notification settings - Fork 226
Extract github url identifier helper #1888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
extensions/ql-vscode/src/common/github-url-identifier-helper.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||
| import { OWNER_REGEX, REPO_REGEX } from "../pure/helpers-pure"; | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a string is a valid GitHub NWO. | ||||||
| * @param identifier The GitHub NWO | ||||||
| * @returns | ||||||
| */ | ||||||
| export function validGitHubNwo(identifier: string): boolean { | ||||||
| return validGitHubNwoOrOwner(identifier, "nwo"); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Checks if a string is a valid GitHub owner. | ||||||
| * @param identifier The GitHub owner | ||||||
| * @returns | ||||||
| */ | ||||||
| export function validGitHubOwner(identifier: string): boolean { | ||||||
| return validGitHubNwoOrOwner(identifier, "owner"); | ||||||
| } | ||||||
|
|
||||||
| function validGitHubNwoOrOwner( | ||||||
| identifier: string, | ||||||
| kind: "owner" | "nwo", | ||||||
| ): boolean { | ||||||
| return kind === "owner" | ||||||
| ? OWNER_REGEX.test(identifier) | ||||||
| : REPO_REGEX.test(identifier); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Extracts an NOW from a GitHub URL. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor :)
Suggested change
|
||||||
| * @param githubUrl The GitHub repository URL | ||||||
| * @return The corresponding NWO, or undefined if the URL is not valid | ||||||
| */ | ||||||
| export function getNwoFromGitHubUrl(githubUrl: string): string | undefined { | ||||||
| return getNwoOrOwnerFromGitHubUrl(githubUrl, "nwo"); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Extracts an owner from a GitHub URL. | ||||||
| * @param githubUrl The GitHub repository URL | ||||||
| * @return The corresponding Owner, or undefined if the URL is not valid | ||||||
| */ | ||||||
| export function getOwnerFromGitHubUrl(githubUrl: string): string | undefined { | ||||||
| return getNwoOrOwnerFromGitHubUrl(githubUrl, "owner"); | ||||||
| } | ||||||
|
|
||||||
| function getNwoOrOwnerFromGitHubUrl( | ||||||
| githubUrl: string, | ||||||
| kind: "owner" | "nwo", | ||||||
| ): string | undefined { | ||||||
| try { | ||||||
| const uri = new URL(githubUrl); | ||||||
| if (uri.protocol !== "https:") { | ||||||
| return; | ||||||
| } | ||||||
| if (uri.hostname !== "github.com" && uri.hostname !== "www.github.com") { | ||||||
| return; | ||||||
| } | ||||||
| const paths = uri.pathname.split("/").filter((segment: string) => segment); | ||||||
| const owner = `${paths[0]}`; | ||||||
| if (kind === "owner") { | ||||||
| return owner ? owner : undefined; | ||||||
| } | ||||||
| const nwo = `${paths[0]}/${paths[1]}`; | ||||||
| return paths[1] ? nwo : undefined; | ||||||
| } catch (e) { | ||||||
| // Ignore the error here, since we catch failures at a higher level. | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,9 @@ import { extLogger } from "./common"; | |
| import { Credentials } from "./authentication"; | ||
| import { getErrorMessage } from "./pure/helpers-pure"; | ||
| import { | ||
| convertGitHubUrlToNwo, | ||
| looksLikeGithubRepo, | ||
| } from "./databases/github-nwo"; | ||
| getNwoFromGitHubUrl, | ||
| validGitHubNwo, | ||
| } from "./common/github-url-identifier-helper"; | ||
|
|
||
| /** | ||
| * Prompts a user to fetch a database from a remote location. Database is assumed to be an archive file. | ||
|
|
@@ -100,19 +100,16 @@ export async function promptImportGithubDatabase( | |
| return; | ||
| } | ||
|
|
||
| if (!looksLikeGithubRepo(githubRepo)) { | ||
| const nwo = getNwoFromGitHubUrl(githubRepo) || githubRepo; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for tidying up this section! 🧹 |
||
| if (!validGitHubNwo(nwo)) { | ||
| throw new Error(`Invalid GitHub repository: ${githubRepo}`); | ||
| } | ||
|
|
||
| const octokit = credentials | ||
| ? await credentials.getOctokit(true) | ||
| : new Octokit.Octokit({ retry }); | ||
|
|
||
| const result = await convertGithubNwoToDatabaseUrl( | ||
| githubRepo, | ||
| octokit, | ||
| progress, | ||
| ); | ||
| const result = await convertGithubNwoToDatabaseUrl(nwo, octokit, progress); | ||
| if (!result) { | ||
| return; | ||
| } | ||
|
|
@@ -446,7 +443,7 @@ export async function findDirWithFile( | |
| } | ||
|
|
||
| export async function convertGithubNwoToDatabaseUrl( | ||
| githubRepo: string, | ||
| nwo: string, | ||
| octokit: Octokit.Octokit, | ||
| progress: ProgressCallback, | ||
| ): Promise< | ||
|
|
@@ -458,7 +455,6 @@ export async function convertGithubNwoToDatabaseUrl( | |
| | undefined | ||
| > { | ||
| try { | ||
| const nwo = convertGitHubUrlToNwo(githubRepo) || githubRepo; | ||
| const [owner, repo] = nwo.split("/"); | ||
|
|
||
| const response = await octokit.request( | ||
|
|
@@ -480,7 +476,7 @@ export async function convertGithubNwoToDatabaseUrl( | |
| }; | ||
| } catch (e) { | ||
| void extLogger.log(`Error: ${getErrorMessage(e)}`); | ||
| throw new Error(`Unable to get database for '${githubRepo}'`); | ||
| throw new Error(`Unable to get database for '${nwo}'`); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
extensions/ql-vscode/test/pure-tests/common/github-url-identifier-helper.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { | ||
| getNwoFromGitHubUrl, | ||
| getOwnerFromGitHubUrl, | ||
| validGitHubNwo, | ||
| validGitHubOwner, | ||
| } from "../../../src/common/github-url-identifier-helper"; | ||
|
|
||
| describe("github url identifier helper", () => { | ||
| describe("valid GitHub Nwo Or Owner method", () => { | ||
| it("should return true for valid owner", () => { | ||
| expect(validGitHubOwner("github")).toBe(true); | ||
| }); | ||
| it("should return true for valid NWO", () => { | ||
| expect(validGitHubNwo("github/codeql")).toBe(true); | ||
| }); | ||
| it("should return false for invalid owner", () => { | ||
| expect(validGitHubOwner("github/codeql")).toBe(false); | ||
| }); | ||
| it("should return false for invalid NWO", () => { | ||
| expect(validGitHubNwo("githubl")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getNwoFromGitHubUrl method", () => { | ||
| it("should handle invalid urls", () => { | ||
| expect(getNwoFromGitHubUrl("")).toBe(undefined); | ||
| expect(getNwoFromGitHubUrl("http://github.com/foo/bar")).toBe(undefined); | ||
| expect(getNwoFromGitHubUrl("https://ww.github.com/foo/bar")).toBe( | ||
| undefined, | ||
| ); | ||
| expect(getNwoFromGitHubUrl("https://www.github.com/foo")).toBe(undefined); | ||
| expect(getNwoFromGitHubUrl("foo")).toBe(undefined); | ||
| expect(getNwoFromGitHubUrl("foo/bar")).toBe(undefined); | ||
| }); | ||
|
|
||
| it("should handle valid urls", () => { | ||
| expect(getNwoFromGitHubUrl("https://github.com/foo/bar")).toBe("foo/bar"); | ||
| expect(getNwoFromGitHubUrl("https://www.github.com/foo/bar")).toBe( | ||
| "foo/bar", | ||
| ); | ||
| expect(getNwoFromGitHubUrl("https://github.com/foo/bar/sub/pages")).toBe( | ||
| "foo/bar", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getOwnerFromGitHubUrl method", () => { | ||
| it("should handle invalid urls", () => { | ||
| expect(getOwnerFromGitHubUrl("")).toBe(undefined); | ||
| expect(getOwnerFromGitHubUrl("http://github.com/foo/bar")).toBe( | ||
| undefined, | ||
| ); | ||
| expect(getOwnerFromGitHubUrl("https://ww.github.com/foo/bar")).toBe( | ||
| undefined, | ||
| ); | ||
| expect(getOwnerFromGitHubUrl("foo")).toBe(undefined); | ||
| expect(getOwnerFromGitHubUrl("foo/bar")).toBe(undefined); | ||
| }); | ||
|
|
||
| it("should handle valid urls", () => { | ||
| expect(getOwnerFromGitHubUrl("https://github.com/foo/bar")).toBe("foo"); | ||
| expect(getOwnerFromGitHubUrl("https://www.github.com/foo/bar")).toBe( | ||
| "foo", | ||
| ); | ||
| expect( | ||
| getOwnerFromGitHubUrl("https://github.com/foo/bar/sub/pages"), | ||
| ).toBe("foo"); | ||
| expect(getOwnerFromGitHubUrl("https://www.github.com/foo")).toBe("foo"); | ||
| }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor (feel free to ignore if this would make conflicts etc annoying!) 👍🏽
I'd probably name this function "isValid..." since that sounds more like a true/false check. Similar for the other functions :)