Skip to content

Commit 14ca75a

Browse files
fix: website address fallback when not set (#85)
1 parent 03a7bd5 commit 14ca75a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

app.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getMessage,
1313
isCLARequired,
1414
isMessageAfterMergeRequired,
15+
getWebsiteAddress,
1516
} from "./src/helpers.js";
1617
import Slack from "./src/services/Slack.js";
1718

@@ -24,7 +25,6 @@ try {
2425
console.error("Failed to get the version number");
2526
}
2627
console.log(`Application version: ${APP_VERSION}`);
27-
console.log(`Website address: ${process.env.WEBSITE_ADDRESS}`);
2828

2929
// Set configured values
3030
const appId = process.env.APP_ID;
@@ -211,6 +211,7 @@ app.webhooks.onError((error) => {
211211
const port = process.env.PORT || 3000;
212212
const webhookPath = "/api/webhook";
213213
const localWebhookUrl = `http://localhost:${port}${webhookPath}`;
214+
const publicWebhookUrl = getWebsiteAddress() + webhookPath;
214215

215216
// See https://github.com/octokit/webhooks.js/#createnodemiddleware for all options
216217
const middleware = createNodeMiddleware(app.webhooks, { path: webhookPath });
@@ -258,9 +259,13 @@ http
258259
}
259260
})
260261
.listen(port, () => {
261-
console.log(`Server is listening for events at: ${localWebhookUrl}`);
262262
console.log(
263-
"Server is also serving the homepage at: http://localhost:" + port
263+
"Server is running at:",
264+
`\n Local: http://localhost:${port}`,
265+
`\n Public: ${getWebsiteAddress()}`
264266
);
267+
console.log("Listening for webhook events at:",
268+
`\n Local webhook url: ${localWebhookUrl}`,
269+
`\n Public webhook url: ${publicWebhookUrl}`);
265270
console.log("Press Ctrl + C to quit.");
266271
});

src/helpers.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export function getMessage(name, context) {
365365
switch (name) {
366366
case "ask-to-sign-cla":
367367
const CLA_LINK =
368-
process.env.WEBSITE_ADDRESS +
368+
getWebsiteAddress() +
369369
"/cla" +
370370
`?org=${context.org}&repo=${context.repo}&prNumber=${context.pr_number}&username=${context.username}`;
371371
message = `Thank you @${context.username} for contributing this PR.
@@ -793,3 +793,21 @@ export async function getPullRequestDetail(app, owner, repo, number) {
793793
});
794794
return pr;
795795
}
796+
797+
export function getWebsiteAddress() {
798+
// 1: WEBSITE_ADDRESS if set by the dev
799+
if (process.env.WEBSITE_ADDRESS) {
800+
return process.env.WEBSITE_ADDRESS;
801+
}
802+
const port = process.env.PORT || 3000;
803+
// 2: Construct url for the staging server on CodeSandbox
804+
if (process.env.CODESANDBOX_HOST) {
805+
return `https://${process.env.HOSTNAME}-${port}.csb.app`;
806+
}
807+
if (process.env.NODE_ENV === 'production'){
808+
console.error("Admin Notice: WEBSITE_ADDRESS is not set in env. This will break CLA functionality.");
809+
return "WEBSITE_ADDRESS_NOT_SET: Contact admin";
810+
}
811+
// 3: Last resort: localhost
812+
return `http://localhost:${port}`;
813+
}

0 commit comments

Comments
 (0)