feat(monorepo-scripts): Alert to discord when new publish happens
This commit is contained in:
		@@ -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",
 | 
			
		||||
 
 | 
			
		||||
@@ -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',
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -140,6 +140,13 @@ async function checkPublishRequiredSetupAsync(): Promise<void> {
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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`);
 | 
			
		||||
 
 | 
			
		||||
@@ -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<void> {
 | 
			
		||||
        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);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								packages/monorepo-scripts/src/utils/discord.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								packages/monorepo-scripts/src/utils/discord.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
import { fetchAsync } from '@0x/utils';
 | 
			
		||||
 | 
			
		||||
import { constants } from '../constants';
 | 
			
		||||
 | 
			
		||||
import { utils } from './utils';
 | 
			
		||||
 | 
			
		||||
export const alertDiscord = async (releaseNotes: string): Promise<boolean> => {
 | 
			
		||||
    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;
 | 
			
		||||
};
 | 
			
		||||
@@ -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<void> {
 | 
			
		||||
export async function publishReleaseNotesAsync(
 | 
			
		||||
    packagesToPublish: Package[],
 | 
			
		||||
    isDryRun: boolean,
 | 
			
		||||
): Promise<string | undefined> {
 | 
			
		||||
    // 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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user