-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from Mr-Kanister/28-githubrepourl-value-type
GitHubRepoURL value type
- Loading branch information
Showing
8 changed files
with
131 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,63 @@ | ||
import { URL } from "node:url"; | ||
import { IllegalArgumentException } from "../Exceptions/IllegalArgumentException"; | ||
|
||
|
||
export class GitHubRepoURL { | ||
private readonly url: URL; | ||
|
||
/** | ||
* Value type for GitHub URLs. Only http:// and https:// URLs are supported. | ||
* | ||
* @param input - The URL to be parsed as a string | ||
* @returns The value type object | ||
*/ | ||
constructor(input: string) { | ||
const url = URL.parse(input); | ||
if (!url) | ||
throw new IllegalArgumentException("Invalid URL"); | ||
this.url = url; | ||
|
||
if (!this.isGithubUrl()) | ||
throw new IllegalArgumentException("Not a GitHub URL"); | ||
|
||
if (!this.isValidProtocol()) | ||
throw new IllegalArgumentException("Unsupported protocol"); | ||
|
||
if (!this.isRepo()) | ||
throw new IllegalArgumentException("The URL has no repository structure"); | ||
} | ||
|
||
/** | ||
* @returns The whole URL as a string | ||
*/ | ||
public asString() { | ||
return this.url.href; | ||
} | ||
|
||
/** | ||
* Checks if the URL hostname is github.com | ||
* @returns True if the check succeeds, else false | ||
*/ | ||
private isGithubUrl() { | ||
return this.url.hostname === "github.com" || | ||
this.url.hostname === "www.github.com"; | ||
} | ||
|
||
/** | ||
* Checks if the URL protocol is http or https | ||
* @returns True if the check succeeds, else false | ||
*/ | ||
private isValidProtocol() { | ||
return this.url.protocol === "http:" || | ||
this.url.protocol === "https:"; | ||
} | ||
|
||
/** | ||
* Checks if the URL pathname contains exactly two slashes with something | ||
* behind them | ||
* @returns True if the check succeeds, else false | ||
*/ | ||
private isRepo() { | ||
return (this.url.pathname.match(/\/./g) || []).length === 2; | ||
} | ||
} |
This file contains 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,29 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { GitHubRepoURL } from "../Models/GitHubRepoURL"; | ||
|
||
describe("GitHubRepoURL", () => { | ||
it("should create a new value", () => { | ||
expect(new GitHubRepoURL("https://github.com/riehlegroup/mini-meco")); | ||
}); | ||
|
||
it("asString() should return the whole URL", () => { | ||
const url = new GitHubRepoURL("https://github.com:73/riehlegroup/mini-meco.git"); | ||
expect(url.asString()).toBe("https://github.com:73/riehlegroup/mini-meco.git"); | ||
}); | ||
|
||
it("should throw if URL isn't a GitHub URL", () => { | ||
expect(() => new GitHubRepoURL("https://gitlab.com/riehlegroup/mini-meco")).toThrow(); | ||
}); | ||
|
||
it("should throw if URL isn't of supported structure", () => { | ||
expect(() => new GitHubRepoURL("[email protected]:riehlegroup/mini-meco.git")).toThrow(); | ||
}); | ||
|
||
it("should throw if URL has unsupported protocoll", () => { | ||
expect(() => new GitHubRepoURL("ssh://github.com/riehlegroup/mini-meco")).toThrow(); | ||
}); | ||
|
||
it("should throw if URL has no repo structure", () => { | ||
expect(() => new GitHubRepoURL("https://github.com/riehlegroup")).toThrow(); | ||
}); | ||
}); |
This file contains 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