-
Notifications
You must be signed in to change notification settings - Fork 45
[FEAT] add getInstallationUrl method
#542
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
wolfy1339
merged 16 commits into
octokit:main
from
rpmccarter:ronan/add-getInstallationUrl-method
Jun 6, 2024
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1d9ad28
add implementation
rpmccarter ce3e354
throw if response is null
rpmccarter 43a69fd
turn fn into factory
rpmccarter 59e842f
delete logs
rpmccarter 4bacd49
add tests
rpmccarter 88e1128
cache promise to avoid multiple requests race condition
rpmccarter d96a379
add docs without state string
rpmccarter 3b9e692
add state handling
rpmccarter eda2a2c
use more idiomatic stringifier
rpmccarter 5c53ef1
add handling for target_id
rpmccarter ed926ec
add tests for target_id
rpmccarter 7cd4cfa
update readme
rpmccarter d4cd0fe
Merge branch 'main' into ronan/add-getInstallationUrl-method
rpmccarter 76f70e7
add info to readme
rpmccarter b502acc
make options param optional
rpmccarter 81d7d0a
Merge branch 'main' into ronan/add-getInstallationUrl-method
wolfy1339 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
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
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,37 @@ | ||
| import type { App } from "./index.js"; | ||
| import type { GetInstallationUrlOptions } from "./types.js"; | ||
|
|
||
| export function getInstallationUrlFactory(app: App) { | ||
| let installationUrlBasePromise: Promise<string> | undefined; | ||
|
|
||
| return async function getInstallationUrl(options: GetInstallationUrlOptions) { | ||
| if (!installationUrlBasePromise) { | ||
| installationUrlBasePromise = getInstallationUrlBase(app); | ||
| } | ||
|
|
||
| const installationUrlBase = await installationUrlBasePromise; | ||
| const installationUrl = new URL(installationUrlBase); | ||
|
|
||
| if (options.target_id !== undefined) { | ||
| installationUrl.pathname += "/permissions"; | ||
| installationUrl.searchParams.append( | ||
| "target_id", | ||
| options.target_id.toFixed(), | ||
| ); | ||
| } | ||
|
|
||
| if (options.state !== undefined) { | ||
| installationUrl.searchParams.append("state", options.state); | ||
| } | ||
|
|
||
| return installationUrl.href; | ||
| }; | ||
| } | ||
|
|
||
| async function getInstallationUrlBase(app: App) { | ||
| const { data: appInfo } = await app.octokit.request("GET /app"); | ||
| if (!appInfo) { | ||
| throw new Error("[@octokit/app] unable to fetch metadata for app"); | ||
| } | ||
| return `${appInfo.html_url}/installations/new`; | ||
| } | ||
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
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
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,166 @@ | ||
| import { Octokit } from "@octokit/core"; | ||
| import fetchMock from "fetch-mock"; | ||
| import MockDate from "mockdate"; | ||
|
|
||
| const APP_ID = 1; | ||
| const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY----- | ||
| MIIEpAIBAAKCAQEA1c7+9z5Pad7OejecsQ0bu3aozN3tihPmljnnudb9G3HECdnH | ||
| lWu2/a1gB9JW5TBQ+AVpum9Okx7KfqkfBKL9mcHgSL0yWMdjMfNOqNtrQqKlN4kE | ||
| p6RD++7sGbzbfZ9arwrl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Ff36mIThGHGiHY6 | ||
| y5bZPPWFDI3hUMa1Hu/35XS85kYOP6sGJjf4kTLyirEcNKJUWH7CXY+00cwvTkOC | ||
| S4Iz64Aas8AilIhRZ1m3eQKBgQCUW1s9azQRxgeZGFrzC3R340LL530aCeta/6FW | ||
| CQVOJ9nv84DLYohTVqvVowdNDTb+9Epw/JDxtDJ7Y0YU0cVtdxPOHcocJgdUGHrX | ||
| ZcJjRIt8w8g/s4X6MhKasBYm9s3owALzCuJjGzUKcDHiO2DKu1xXAb0SzRcTzUCn | ||
| 7daCswKBgQDOYPZ2JGmhibqKjjLFm0qzpcQ6RPvPK1/7g0NInmjPMebP0K6eSPx0 | ||
| 9/49J6WTD++EajN7FhktUSYxukdWaCocAQJTDNYP0K88G4rtC2IYy5JFn9SWz5oh | ||
| x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w== | ||
| -----END RSA PRIVATE KEY-----`; | ||
| const CLIENT_ID = "0123"; | ||
| const CLIENT_SECRET = "0123secret"; | ||
| const WEBHOOK_SECRET = "secret"; | ||
|
|
||
| import { App } from "../src/index.ts"; | ||
|
|
||
| describe("app.getInstallationUrl", () => { | ||
| let app: InstanceType<typeof App>; | ||
| let mock: typeof fetchMock; | ||
|
|
||
| beforeEach(() => { | ||
| MockDate.set(0); | ||
| mock = fetchMock.sandbox(); | ||
|
|
||
| app = new App({ | ||
| appId: APP_ID, | ||
| privateKey: PRIVATE_KEY, | ||
| oauth: { | ||
| clientId: CLIENT_ID, | ||
| clientSecret: CLIENT_SECRET, | ||
| }, | ||
| webhooks: { | ||
| secret: WEBHOOK_SECRET, | ||
| }, | ||
| Octokit: Octokit.defaults({ | ||
| request: { | ||
| fetch: mock, | ||
| }, | ||
| }), | ||
| }); | ||
| }); | ||
|
|
||
| test("throws when response is null", async () => { | ||
| mock.getOnce("path:/app", { | ||
| body: "null", | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); | ||
|
|
||
| await expect(app.getInstallationUrl({})).rejects.toThrow( | ||
| "[@octokit/app] unable to fetch metadata for app", | ||
| ); | ||
|
|
||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("returns correct url", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
|
|
||
| const url = await app.getInstallationUrl({}); | ||
|
|
||
| expect(url).toEqual("https://github.com/apps/octokit/installations/new"); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("caches url", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
|
|
||
| const urls = await Promise.all([ | ||
| app.getInstallationUrl({}), | ||
| app.getInstallationUrl({}), | ||
| app.getInstallationUrl({}), | ||
| ]); | ||
|
|
||
| expect(urls).toEqual( | ||
| new Array(3).fill("https://github.com/apps/octokit/installations/new"), | ||
| ); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("does not cache state", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
| const state = "abc123"; | ||
|
|
||
| const urlWithoutState = await app.getInstallationUrl({}); | ||
| const urlWithState = await app.getInstallationUrl({ state }); | ||
|
|
||
| expect(urlWithoutState).toEqual( | ||
| "https://github.com/apps/octokit/installations/new", | ||
| ); | ||
| expect(urlWithState).toEqual( | ||
| `https://github.com/apps/octokit/installations/new?state=${state}`, | ||
| ); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("adds the url-encoded state string to the url", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
| const state = "abc123%/{"; | ||
|
|
||
| const url = await app.getInstallationUrl({ state }); | ||
|
|
||
| expect(url).toEqual( | ||
| `https://github.com/apps/octokit/installations/new?state=${encodeURIComponent(state)}`, | ||
| ); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("appends /permissions to the url when target_id present", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
| const target_id = 456; | ||
|
|
||
| const url = await app.getInstallationUrl({ target_id }); | ||
|
|
||
| expect(url).toEqual( | ||
| `https://github.com/apps/octokit/installations/new/permissions?target_id=${target_id}`, | ||
| ); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
|
|
||
| test("adds both state and target_id to the url", async () => { | ||
| mock.getOnce("path:/app", { | ||
| html_url: "https://github.com/apps/octokit", | ||
| }); | ||
| const state = "abc123"; | ||
| const target_id = 456; | ||
|
|
||
| const url = await app.getInstallationUrl({ state, target_id }); | ||
|
|
||
| expect(url).toEqual( | ||
| `https://github.com/apps/octokit/installations/new/permissions?target_id=${target_id}&state=${state}`, | ||
| ); | ||
| expect(mock.done()).toBe(true); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.