-
Notifications
You must be signed in to change notification settings - Fork 0
accept attachment name or url to get addon config #20
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
ekozilforce
merged 3 commits into
main
from
ekozil/add-attachment-or-url-scoped-authorizations
May 14, 2025
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
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
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,57 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
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.
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. 👍 I'll handle that in a separate pr to avoid noise. |
||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
interface AddonConfig { | ||
apiUrl: string; | ||
token: string; | ||
} | ||
|
||
export function resolveAddonConfigByAttachment( | ||
attachment: string | ||
): AddonConfig { | ||
const apiUrl = process.env[`${attachment.toUpperCase()}_API_URL`]; | ||
const token = process.env[`${attachment.toUpperCase()}_TOKEN`]; | ||
|
||
if (!apiUrl || !token) { | ||
throw Error( | ||
`Heroku Applink config not found under attachment ${attachment}` | ||
); | ||
} | ||
|
||
return { | ||
apiUrl, | ||
token, | ||
}; | ||
} | ||
|
||
export function resolveAddonConfigByUrl(url: string): AddonConfig { | ||
// Find the environment variable ending with _API_URL that matches the given URL | ||
const envVarEntries = Object.entries(process.env); | ||
const matchingApiUrlEntry = envVarEntries.find( | ||
([key, value]) => | ||
key.endsWith("_API_URL") && value.toLowerCase() === url.toLowerCase() | ||
); | ||
|
||
if (!matchingApiUrlEntry) { | ||
throw Error(`Heroku Applink config not found for API URL: ${url}`); | ||
} | ||
|
||
// Extract the prefix from the API_URL environment variable name | ||
const [envVarName] = matchingApiUrlEntry; | ||
const prefix = envVarName.slice(0, -"_API_URL".length); // Remove '_API_URL' suffix | ||
|
||
// Look for corresponding token | ||
const token = process.env[`${prefix}_TOKEN`]; | ||
if (!token) { | ||
throw Error(`Heroku Applink token not found for API URL: ${url}`); | ||
} | ||
|
||
return { | ||
apiUrl: matchingApiUrlEntry[1], | ||
token, | ||
}; | ||
} |
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,122 @@ | ||
/* | ||
* Copyright (c) 2024, salesforce.com, inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { expect } from "chai"; | ||
import { | ||
resolveAddonConfigByAttachment, | ||
resolveAddonConfigByUrl, | ||
} from "../../src/utils/addon-config"; | ||
|
||
const ATTACHMENT = "HEROKU_APPLINK"; | ||
|
||
describe("resolveAddonConfigByAttachment", () => { | ||
let originalEnv: NodeJS.ProcessEnv; | ||
|
||
beforeEach(() => { | ||
originalEnv = { ...process.env }; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it("finds config for specified attachment", () => { | ||
process.env.HEROKU_APPLINK_API_URL = "https://api.example.com"; | ||
process.env.HEROKU_APPLINK_TOKEN = "default-token"; | ||
|
||
const config = resolveAddonConfigByAttachment(ATTACHMENT); | ||
expect(config.apiUrl).to.equal("https://api.example.com"); | ||
expect(config.token).to.equal("default-token"); | ||
}); | ||
|
||
it("finds config ignoring case of attachment name", () => { | ||
process.env.HEROKU_APPLINK_API_URL = "https://api.example.com"; | ||
process.env.HEROKU_APPLINK_TOKEN = "default-token"; | ||
|
||
const config = resolveAddonConfigByAttachment(ATTACHMENT.toLowerCase()); | ||
expect(config.apiUrl).to.equal("https://api.example.com"); | ||
expect(config.token).to.equal("default-token"); | ||
}); | ||
|
||
it("throws if API_URL config not found", () => { | ||
process.env.HEROKU_APPLINK_TOKEN = "token"; | ||
// APPLINK_API_URL intentionally not set | ||
|
||
expect(() => resolveAddonConfigByAttachment(ATTACHMENT)).to.throw( | ||
"Heroku Applink config not found under attachment HEROKU_APPLINK" | ||
); | ||
}); | ||
|
||
it("throws if TOKEN config not found", () => { | ||
process.env.HEROKU_APPLINK_API_URL = "https://api.example.com"; | ||
// APPLINK_TOKEN intentionally not set | ||
|
||
expect(() => resolveAddonConfigByAttachment(ATTACHMENT)).to.throw( | ||
"Heroku Applink config not found under attachment HEROKU_APPLINK" | ||
); | ||
}); | ||
}); | ||
|
||
describe("resolveAddonConfigByUrl", () => { | ||
let originalEnv: NodeJS.ProcessEnv; | ||
|
||
beforeEach(() => { | ||
originalEnv = { ...process.env }; | ||
}); | ||
|
||
afterEach(() => { | ||
process.env = originalEnv; | ||
}); | ||
|
||
it("finds config for matching URL", () => { | ||
const testUrl = "https://api.example.com"; | ||
process.env.HEROKU_APPLINK_API_URL = testUrl; | ||
process.env.HEROKU_APPLINK_TOKEN = "test-token"; | ||
|
||
const config = resolveAddonConfigByUrl(testUrl); | ||
expect(config.apiUrl).to.equal(testUrl); | ||
expect(config.token).to.equal("test-token"); | ||
}); | ||
|
||
it("finds config ignoring url case", () => { | ||
const testUrl = "https://api.example.com"; | ||
process.env.HEROKU_APPLINK_API_URL = testUrl; | ||
process.env.HEROKU_APPLINK_TOKEN = "test-token"; | ||
|
||
const config = resolveAddonConfigByUrl(testUrl.toUpperCase()); | ||
expect(config.apiUrl).to.equal(testUrl); | ||
expect(config.token).to.equal("test-token"); | ||
}); | ||
|
||
it("finds custom prefix config for matching URL", () => { | ||
const testUrl = "https://custom.example.com"; | ||
process.env.CUSTOM_ATTACHMENT_API_URL = testUrl; | ||
process.env.CUSTOM_ATTACHMENT_TOKEN = "custom-token"; | ||
|
||
const config = resolveAddonConfigByUrl(testUrl); | ||
expect(config.apiUrl).to.equal(testUrl); | ||
expect(config.token).to.equal("custom-token"); | ||
}); | ||
|
||
it("throws if no matching URL is found", () => { | ||
const testUrl = "https://nonexistent.example.com"; | ||
|
||
expect(() => resolveAddonConfigByUrl(testUrl)).to.throw( | ||
`Heroku Applink config not found for API URL: ${testUrl}` | ||
); | ||
}); | ||
|
||
it("throws if matching URL found but no corresponding token", () => { | ||
const testUrl = "https://api.example.com"; | ||
process.env.SOME_API_URL = testUrl; | ||
// SOME_TOKEN intentionally not set | ||
|
||
expect(() => resolveAddonConfigByUrl(testUrl)).to.throw( | ||
`Heroku Applink token not found for API URL: ${testUrl}` | ||
); | ||
}); | ||
}); |
Oops, something went wrong.
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.
Might need to rethink the example a bit here given the change to use a developer name instead of org name.
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.
Yeah, agree. Best to be consistent.