From 472f89bd3d459c8b5c29de8a808381bf19c8c72b Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Wed, 2 Jan 2019 14:46:31 -0800 Subject: feat(monorepo-scripts): Alert to discord when new publish happens --- packages/monorepo-scripts/package.json | 1 + packages/monorepo-scripts/src/constants.ts | 1 + packages/monorepo-scripts/src/prepublish_checks.ts | 7 ++++++ packages/monorepo-scripts/src/publish.ts | 12 +++++++++- packages/monorepo-scripts/src/utils/discord.ts | 27 ++++++++++++++++++++++ .../src/utils/github_release_utils.ts | 9 ++++++-- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 packages/monorepo-scripts/src/utils/discord.ts diff --git a/packages/monorepo-scripts/package.json b/packages/monorepo-scripts/package.json index 0483e87c8..a64f101fe 100644 --- a/packages/monorepo-scripts/package.json +++ b/packages/monorepo-scripts/package.json @@ -47,6 +47,7 @@ "typescript": "3.0.1" }, "dependencies": { + "@0x/utils": "^2.0.8", "@lerna/batch-packages": "^3.0.0-beta.18", "@types/depcheck": "^0.6.0", "async-child-process": "^1.1.1", diff --git a/packages/monorepo-scripts/src/constants.ts b/packages/monorepo-scripts/src/constants.ts index acb4b211e..8f907ffef 100644 --- a/packages/monorepo-scripts/src/constants.ts +++ b/packages/monorepo-scripts/src/constants.ts @@ -5,5 +5,6 @@ export const constants = { stagingWebsite: 'http://staging-0xproject.s3-website-us-east-1.amazonaws.com', lernaExecutable: path.join('node_modules', '@0x-lerna-fork', 'lerna', 'cli.js'), githubPersonalAccessToken: process.env.GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS, + discordAlertWebhookUrl: process.env.DISCORD_GITHUB_RELEASE_WEBHOOK_URL, dependenciesUpdatedMessage: 'Dependencies updated', }; diff --git a/packages/monorepo-scripts/src/prepublish_checks.ts b/packages/monorepo-scripts/src/prepublish_checks.ts index 36e61714b..82eaf5cf9 100644 --- a/packages/monorepo-scripts/src/prepublish_checks.ts +++ b/packages/monorepo-scripts/src/prepublish_checks.ts @@ -140,6 +140,13 @@ async function checkPublishRequiredSetupAsync(): Promise { ); } + // Check to see if discord URL is set up + if (_.isUndefined(constants.discordAlertWebhookUrl)) { + throw new Error( + 'You must have a discord webhook URL set to an envVar named `DISCORD_GITHUB_RELEASE_WEBHOOK_URL`. Add it then try again.', + ); + } + // Check Yarn version is 1.X utils.log('Checking the yarn version...'); const result = await execAsync(`yarn --version`); diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 854a72b86..06b0d72ae 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -14,6 +14,7 @@ import { constants } from './constants'; import { Package, PackageToNextVersion, VersionChangelog } from './types'; import { changelogUtils } from './utils/changelog_utils'; import { configs } from './utils/configs'; +import { alertDiscord } from './utils/discord'; import { DocGenerateAndUploadUtils } from './utils/doc_generate_and_upload_utils'; import { publishReleaseNotesAsync } from './utils/github_release_utils'; import { utils } from './utils/utils'; @@ -84,7 +85,16 @@ async function confirmAsync(message: string): Promise { await generateAndUploadDocJsonsAsync(packagesWithDocs, isStaging, shouldUploadDocs); } const isDryRun = configs.IS_LOCAL_PUBLISH; - await publishReleaseNotesAsync(updatedPublicPackages, isDryRun); + const releaseNotes = await publishReleaseNotesAsync(updatedPublicPackages, isDryRun); + utils.log('Published release notes'); + + if (!isDryRun && releaseNotes) { + try { + alertDiscord(releaseNotes); + } catch (e) { + utils.log("Couldn't alert discord, error: ", e.message, '. Please alert manually.'); + } + } })().catch(err => { utils.log(err); process.exit(1); diff --git a/packages/monorepo-scripts/src/utils/discord.ts b/packages/monorepo-scripts/src/utils/discord.ts new file mode 100644 index 000000000..2482a18f0 --- /dev/null +++ b/packages/monorepo-scripts/src/utils/discord.ts @@ -0,0 +1,27 @@ +import { fetchAsync } from '@0x/utils'; + +import { constants } from '../constants'; + +import { utils } from './utils'; + +export const alertDiscord = async (releaseNotes: string): Promise => { + const webhookUrl = constants.discordAlertWebhookUrl; + if (!webhookUrl) { + utils.log('Not alerting to discord because webhook url not set'); + return false; + } + + utils.log('Alerting discord...'); + const payload = { + content: `New monorepo package released! View at https://github.com/0xProject/0x-monorepo/releases \n\n ${releaseNotes}`, + }; + await fetchAsync(webhookUrl, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + return true; +}; diff --git a/packages/monorepo-scripts/src/utils/github_release_utils.ts b/packages/monorepo-scripts/src/utils/github_release_utils.ts index e63244b46..1044c25e9 100644 --- a/packages/monorepo-scripts/src/utils/github_release_utils.ts +++ b/packages/monorepo-scripts/src/utils/github_release_utils.ts @@ -12,7 +12,10 @@ import { utils } from './utils'; const publishReleaseAsync = promisify(publishRelease); // tslint:disable-next-line:completed-docs -export async function publishReleaseNotesAsync(packagesToPublish: Package[], isDryRun: boolean): Promise { +export async function publishReleaseNotesAsync( + packagesToPublish: Package[], + isDryRun: boolean, +): Promise { // Git push a tag representing this publish (publish-{commit-hash}) (truncate hash) const result = await execAsync('git log -n 1 --pretty=format:"%H"', { cwd: constants.monorepoRootPath }); const latestGitCommit = result.stdout; @@ -75,6 +78,8 @@ export async function publishReleaseNotesAsync(packagesToPublish: Package[], isD utils.log('Publishing release notes ', releaseName, '...'); await publishReleaseAsync(publishReleaseConfigs); + + return aggregateNotes; } // Asset paths should described from the monorepo root. This method prefixes @@ -88,7 +93,7 @@ function adjustAssetPaths(assets: string[]): string[] { return finalAssets; } -function getReleaseNotesForPackage(packageLocation: string, packageName: string): string { +export function getReleaseNotesForPackage(packageLocation: string, packageName: string): string { const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json'); const changelogJSON = readFileSync(changelogJSONPath, 'utf-8'); const changelogs = JSON.parse(changelogJSON); -- cgit v1.2.3 From 9f47f90c6e80ba9a61bcb6065fed1e2c6be8c5b7 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Wed, 2 Jan 2019 15:16:45 -0800 Subject: remove unused export --- packages/monorepo-scripts/src/utils/github_release_utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/monorepo-scripts/src/utils/github_release_utils.ts b/packages/monorepo-scripts/src/utils/github_release_utils.ts index 1044c25e9..48704f3aa 100644 --- a/packages/monorepo-scripts/src/utils/github_release_utils.ts +++ b/packages/monorepo-scripts/src/utils/github_release_utils.ts @@ -93,7 +93,7 @@ function adjustAssetPaths(assets: string[]): string[] { return finalAssets; } -export function getReleaseNotesForPackage(packageLocation: string, packageName: string): string { +function getReleaseNotesForPackage(packageLocation: string, packageName: string): string { const changelogJSONPath = path.join(packageLocation, 'CHANGELOG.json'); const changelogJSON = readFileSync(changelogJSONPath, 'utf-8'); const changelogs = JSON.parse(changelogJSON); -- cgit v1.2.3 From 92d45a19d1b8f894add205f87bee77962848699b Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Wed, 2 Jan 2019 15:19:38 -0800 Subject: await alerting discord --- packages/monorepo-scripts/src/publish.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 06b0d72ae..923248360 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -90,7 +90,7 @@ async function confirmAsync(message: string): Promise { if (!isDryRun && releaseNotes) { try { - alertDiscord(releaseNotes); + await alertDiscord(releaseNotes); } catch (e) { utils.log("Couldn't alert discord, error: ", e.message, '. Please alert manually.'); } -- cgit v1.2.3 From 9131ca15620362565ca90a54971a3fc609876e9f Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Wed, 2 Jan 2019 15:22:07 -0800 Subject: Use constant --- packages/monorepo-scripts/src/constants.ts | 1 + packages/monorepo-scripts/src/utils/discord.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/monorepo-scripts/src/constants.ts b/packages/monorepo-scripts/src/constants.ts index 8f907ffef..c15bcabf4 100644 --- a/packages/monorepo-scripts/src/constants.ts +++ b/packages/monorepo-scripts/src/constants.ts @@ -6,5 +6,6 @@ export const constants = { lernaExecutable: path.join('node_modules', '@0x-lerna-fork', 'lerna', 'cli.js'), githubPersonalAccessToken: process.env.GITHUB_PERSONAL_ACCESS_TOKEN_0X_JS, discordAlertWebhookUrl: process.env.DISCORD_GITHUB_RELEASE_WEBHOOK_URL, + releasesUrl: 'https://github.com/0xProject/0x-monorepo/releases', dependenciesUpdatedMessage: 'Dependencies updated', }; diff --git a/packages/monorepo-scripts/src/utils/discord.ts b/packages/monorepo-scripts/src/utils/discord.ts index 2482a18f0..116b1b908 100644 --- a/packages/monorepo-scripts/src/utils/discord.ts +++ b/packages/monorepo-scripts/src/utils/discord.ts @@ -13,7 +13,7 @@ export const alertDiscord = async (releaseNotes: string): Promise => { utils.log('Alerting discord...'); const payload = { - content: `New monorepo package released! View at https://github.com/0xProject/0x-monorepo/releases \n\n ${releaseNotes}`, + content: `New monorepo package released! View at ${constants.releasesUrl} \n\n ${releaseNotes}`, }; await fetchAsync(webhookUrl, { method: 'POST', -- cgit v1.2.3 From 442de09bbe36cb4a1c2d32cca6e65aee710f6382 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 4 Jan 2019 08:54:44 -0800 Subject: Add async suffix --- packages/monorepo-scripts/src/publish.ts | 4 ++-- packages/monorepo-scripts/src/utils/discord.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index 923248360..f11f998b8 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -14,7 +14,7 @@ import { constants } from './constants'; import { Package, PackageToNextVersion, VersionChangelog } from './types'; import { changelogUtils } from './utils/changelog_utils'; import { configs } from './utils/configs'; -import { alertDiscord } from './utils/discord'; +import { alertDiscordAsync } from './utils/discord'; import { DocGenerateAndUploadUtils } from './utils/doc_generate_and_upload_utils'; import { publishReleaseNotesAsync } from './utils/github_release_utils'; import { utils } from './utils/utils'; @@ -90,7 +90,7 @@ async function confirmAsync(message: string): Promise { if (!isDryRun && releaseNotes) { try { - await alertDiscord(releaseNotes); + await alertDiscordAsync(releaseNotes); } catch (e) { utils.log("Couldn't alert discord, error: ", e.message, '. Please alert manually.'); } diff --git a/packages/monorepo-scripts/src/utils/discord.ts b/packages/monorepo-scripts/src/utils/discord.ts index 116b1b908..ed10b1cc3 100644 --- a/packages/monorepo-scripts/src/utils/discord.ts +++ b/packages/monorepo-scripts/src/utils/discord.ts @@ -4,7 +4,7 @@ import { constants } from '../constants'; import { utils } from './utils'; -export const alertDiscord = async (releaseNotes: string): Promise => { +export const alertDiscordAsync = async (releaseNotes: string): Promise => { const webhookUrl = constants.discordAlertWebhookUrl; if (!webhookUrl) { utils.log('Not alerting to discord because webhook url not set'); -- cgit v1.2.3 From f1a7efc97ea4eb4305836a3fb1713a72cc12ac94 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 4 Jan 2019 09:01:13 -0800 Subject: Return void and throw error instead of logging and returning boolean --- packages/monorepo-scripts/src/utils/discord.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/monorepo-scripts/src/utils/discord.ts b/packages/monorepo-scripts/src/utils/discord.ts index ed10b1cc3..2f3afef45 100644 --- a/packages/monorepo-scripts/src/utils/discord.ts +++ b/packages/monorepo-scripts/src/utils/discord.ts @@ -4,11 +4,10 @@ import { constants } from '../constants'; import { utils } from './utils'; -export const alertDiscordAsync = async (releaseNotes: string): Promise => { +export const alertDiscordAsync = async (releaseNotes: string): Promise => { const webhookUrl = constants.discordAlertWebhookUrl; if (!webhookUrl) { - utils.log('Not alerting to discord because webhook url not set'); - return false; + throw new Error("No discord webhook url, can't alert"); } utils.log('Alerting discord...'); @@ -23,5 +22,5 @@ export const alertDiscordAsync = async (releaseNotes: string): Promise }, body: JSON.stringify(payload), }); - return true; + return; }; -- cgit v1.2.3 From 99016cc5a6cc018eac08d715e649fadbe952fe21 Mon Sep 17 00:00:00 2001 From: Fabio B Date: Mon, 7 Jan 2019 09:43:35 -0800 Subject: Use explicit undefined check Co-Authored-By: steveklebanoff --- packages/monorepo-scripts/src/utils/discord.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/monorepo-scripts/src/utils/discord.ts b/packages/monorepo-scripts/src/utils/discord.ts index 2f3afef45..3a0458769 100644 --- a/packages/monorepo-scripts/src/utils/discord.ts +++ b/packages/monorepo-scripts/src/utils/discord.ts @@ -6,7 +6,7 @@ import { utils } from './utils'; export const alertDiscordAsync = async (releaseNotes: string): Promise => { const webhookUrl = constants.discordAlertWebhookUrl; - if (!webhookUrl) { + if (webhookUrl === undefined) { throw new Error("No discord webhook url, can't alert"); } -- cgit v1.2.3 From 571bc736e573312bc1ca1992c2bfc34269d5899c Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 7 Jan 2019 09:45:04 -0800 Subject: Make error message less scary --- packages/monorepo-scripts/src/publish.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/monorepo-scripts/src/publish.ts b/packages/monorepo-scripts/src/publish.ts index f11f998b8..e0602a74f 100644 --- a/packages/monorepo-scripts/src/publish.ts +++ b/packages/monorepo-scripts/src/publish.ts @@ -92,7 +92,7 @@ async function confirmAsync(message: string): Promise { try { await alertDiscordAsync(releaseNotes); } catch (e) { - utils.log("Couldn't alert discord, error: ", e.message, '. Please alert manually.'); + utils.log("Publish successful, but couldn't auto-alert discord (", e.message, '), Please alert manually.'); } } })().catch(err => { -- cgit v1.2.3