-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dbaa48
commit dd74d5b
Showing
6 changed files
with
450 additions
and
6 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
24 changes: 24 additions & 0 deletions
24
packages/manager/src/managers/git/buildGitRepoSpecifier.ts
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,24 @@ | ||
import { GitRepoSpecifier } from "./types"; | ||
|
||
/** | ||
* Builds a Git repository specifier from its individual parts. | ||
* | ||
* @example | ||
* | ||
* ```typescript | ||
* buildGitRepoSpecifier({ | ||
* provider: "gitHub", | ||
* owner: "foo", | ||
* name: "bar", | ||
* }); | ||
* ``` | ||
* | ||
* @param repoSpecifier - The Git repository specifier. | ||
* | ||
* @returns The specifier in the form of `provider@owner/name`. | ||
*/ | ||
export const buildGitRepoSpecifier = ( | ||
repoSpecifier: GitRepoSpecifier, | ||
): string => { | ||
return `${repoSpecifier.provider}@${repoSpecifier.owner}/${repoSpecifier.name}`; | ||
}; |
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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
export type Namespace = { | ||
export type Owner = { | ||
provider: "gitHub"; | ||
id: string; | ||
name: string; | ||
type: "user" | "team" | null; | ||
}; | ||
|
||
export type GitRepo = { | ||
provider: "gitHub"; | ||
id: string; | ||
owner: string; | ||
name: string; | ||
url: string; | ||
pushedAt: Date; | ||
}; | ||
|
||
export type GitRepoSpcifier = { | ||
export type GitRepoSpecifier = { | ||
provider: "gitHub"; | ||
owner: string; | ||
name: string; | ||
}; |
102 changes: 102 additions & 0 deletions
102
packages/manager/test/SliceMachineManager-git-checkHasWriteAPIToken.test.ts
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,102 @@ | ||
import { expect, it } from "vitest"; | ||
|
||
import { UnauthenticatedError, UnauthorizedError } from "../src"; | ||
|
||
it("returns true if linked repositories have a Prismic Write API token", async ({ | ||
manager, | ||
api, | ||
login, | ||
}) => { | ||
const prismic = { domain: "domain" }; | ||
const git = { provider: "gitHub", owner: "owner", name: "name" } as const; | ||
|
||
api.mockSliceMachineV1( | ||
"./git/linked-repos/write-api-token", | ||
{ hasWriteAPIToken: true }, | ||
{ | ||
searchParams: { | ||
repository: prismic.domain, | ||
git: `${git.provider}@${git.owner}/${git.name}`, | ||
}, | ||
}, | ||
); | ||
|
||
await login(); | ||
const res = await manager.git.checkHasWriteAPIToken({ prismic, git }); | ||
|
||
expect(res).toStrictEqual(true); | ||
}); | ||
|
||
it("returns false if linked repositories do not have a Prismic Write API token", async ({ | ||
manager, | ||
api, | ||
login, | ||
}) => { | ||
const prismic = { domain: "domain" }; | ||
const git = { provider: "gitHub", owner: "owner", name: "name" } as const; | ||
|
||
api.mockSliceMachineV1( | ||
"./git/linked-repos/write-api-token", | ||
{ hasWriteAPIToken: false }, | ||
{ | ||
searchParams: { | ||
repository: prismic.domain, | ||
git: `${git.provider}@${git.owner}/${git.name}`, | ||
}, | ||
}, | ||
); | ||
|
||
await login(); | ||
const res = await manager.git.checkHasWriteAPIToken({ prismic, git }); | ||
|
||
expect(res).toStrictEqual(false); | ||
}); | ||
|
||
it("throws UnauthenticatedError if the API returns 403", async ({ | ||
manager, | ||
api, | ||
login, | ||
}) => { | ||
api.mockSliceMachineV1("./git/linked-repos/write-api-token", undefined, { | ||
statusCode: 401, | ||
}); | ||
|
||
await login(); | ||
await expect(() => | ||
manager.git.checkHasWriteAPIToken({ | ||
prismic: { domain: "domain" }, | ||
git: { provider: "gitHub", owner: "owner", name: "name" }, | ||
}), | ||
).rejects.toThrow(UnauthenticatedError); | ||
}); | ||
|
||
it("throws UnauthorizedError if the API returns 403", async ({ | ||
manager, | ||
api, | ||
login, | ||
}) => { | ||
api.mockSliceMachineV1("./git/linked-repos/write-api-token", undefined, { | ||
statusCode: 403, | ||
}); | ||
|
||
await login(); | ||
await expect(() => | ||
manager.git.checkHasWriteAPIToken({ | ||
prismic: { domain: "domain" }, | ||
git: { provider: "gitHub", owner: "owner", name: "name" }, | ||
}), | ||
).rejects.toThrow(UnauthorizedError); | ||
}); | ||
|
||
it("throws UnauthenticatedError if the user is logged out", async ({ | ||
manager, | ||
}) => { | ||
await manager.user.logout(); | ||
|
||
await expect(() => | ||
manager.git.checkHasWriteAPIToken({ | ||
prismic: { domain: "domain" }, | ||
git: { provider: "gitHub", owner: "owner", name: "name" }, | ||
}), | ||
).rejects.toThrow(UnauthenticatedError); | ||
}); |
Oops, something went wrong.