Add additional check to make sure user was added to our DockerHub org
This commit is contained in:
		@@ -6,31 +6,25 @@ import semverSort = require('semver-sort');
 | 
			
		||||
import { constants } from './constants';
 | 
			
		||||
import { Package } from './types';
 | 
			
		||||
import { changelogUtils } from './utils/changelog_utils';
 | 
			
		||||
import { configs } from './utils/configs';
 | 
			
		||||
import { dockerHubUtils } from './utils/docker_hub_utils';
 | 
			
		||||
import { npmUtils } from './utils/npm_utils';
 | 
			
		||||
import { utils } from './utils/utils';
 | 
			
		||||
 | 
			
		||||
async function prepublishChecksAsync(): Promise<void> {
 | 
			
		||||
    const shouldIncludePrivate = false;
 | 
			
		||||
    const updatedPublicPackages = await utils.getPackagesToPublishAsync(shouldIncludePrivate);
 | 
			
		||||
    // const updatedPublicPackages = await utils.getPackagesToPublishAsync(shouldIncludePrivate);
 | 
			
		||||
 | 
			
		||||
    await checkCurrentVersionMatchesLatestPublishedNPMPackageAsync(updatedPublicPackages);
 | 
			
		||||
    await checkChangelogFormatAsync(updatedPublicPackages);
 | 
			
		||||
    await checkGitTagsForNextVersionAndDeleteIfExistAsync(updatedPublicPackages);
 | 
			
		||||
    await checkPublishRequiredSetupAsync();
 | 
			
		||||
    // await checkCurrentVersionMatchesLatestPublishedNPMPackageAsync(updatedPublicPackages);
 | 
			
		||||
    // await checkChangelogFormatAsync(updatedPublicPackages);
 | 
			
		||||
    // await checkGitTagsForNextVersionAndDeleteIfExistAsync(updatedPublicPackages);
 | 
			
		||||
    // await checkPublishRequiredSetupAsync();
 | 
			
		||||
    await checkDockerHubSetupAsync();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function checkDockerHubSetupAsync(): Promise<void> {
 | 
			
		||||
    try {
 | 
			
		||||
        utils.log('Checking that the user is logged in to docker command...');
 | 
			
		||||
        await execAsync(`echo "$DOCKER_PASS" | docker login -u $DOCKER_USERNAME --password-stdin`);
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
        throw new Error(
 | 
			
		||||
            `Failed to log you into the 'docker' commandline tool. Make sure you have environment variables 'DOCKER_USERNAME; and 'DOCKER_PASS' set. Full error: ${
 | 
			
		||||
                err.message
 | 
			
		||||
            }`,
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
    await dockerHubUtils.checkUserAddedToOrganizationOrThrowAsync(configs.DOCKER_HUB_ORG);
 | 
			
		||||
    await dockerHubUtils.loginUserToDockerCommandlineOrThrowAsync();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
async function checkGitTagsForNextVersionAndDeleteIfExistAsync(updatedPublicPackages: Package[]): Promise<void> {
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,6 @@ import { utils } from './utils/utils';
 | 
			
		||||
 | 
			
		||||
const NPM_NAMESPACE = '@0x/';
 | 
			
		||||
const TODAYS_TIMESTAMP = moment().unix();
 | 
			
		||||
const DOCKER_ORG = '0xorg';
 | 
			
		||||
 | 
			
		||||
async function confirmAsync(message: string): Promise<void> {
 | 
			
		||||
    prompt.start();
 | 
			
		||||
@@ -130,13 +129,13 @@ async function publishImagesToDockerHubAsync(allUpdatedPackages: Package[]): Pro
 | 
			
		||||
        // Tag the docker image with the latest version
 | 
			
		||||
        const version = pkg.packageJson.version;
 | 
			
		||||
        logUtils.log(`Tagging '${dockerHubRepo}' docker image with version ${version}...`);
 | 
			
		||||
        await execAsync(`docker tag ${dockerHubRepo} ${DOCKER_ORG}/${dockerHubRepo}:${version}`);
 | 
			
		||||
        await execAsync(`docker tag ${dockerHubRepo} ${DOCKER_ORG}/${dockerHubRepo}:latest`);
 | 
			
		||||
        await execAsync(`docker tag ${dockerHubRepo} ${configs.DOCKER_HUB_ORG}/${dockerHubRepo}:${version}`);
 | 
			
		||||
        await execAsync(`docker tag ${dockerHubRepo} ${configs.DOCKER_HUB_ORG}/${dockerHubRepo}:latest`);
 | 
			
		||||
 | 
			
		||||
        // Publish to DockerHub
 | 
			
		||||
        logUtils.log(`Pushing '${dockerHubRepo}' docker image to DockerHub...`);
 | 
			
		||||
        await execAsync(`docker push ${DOCKER_ORG}/${dockerHubRepo}:${version}`);
 | 
			
		||||
        await execAsync(`docker push ${DOCKER_ORG}/${dockerHubRepo}:latest`);
 | 
			
		||||
        await execAsync(`docker push ${configs.DOCKER_HUB_ORG}/${dockerHubRepo}:${version}`);
 | 
			
		||||
        await execAsync(`docker push ${configs.DOCKER_HUB_ORG}/${dockerHubRepo}:latest`);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -5,4 +5,5 @@ const REMOTE_NPM_REGISTRY_URL = 'https://registry.npmjs.org';
 | 
			
		||||
export const configs = {
 | 
			
		||||
    IS_LOCAL_PUBLISH,
 | 
			
		||||
    NPM_REGISTRY_URL: IS_LOCAL_PUBLISH ? LOCAL_NPM_REGISTRY_URL : REMOTE_NPM_REGISTRY_URL,
 | 
			
		||||
    DOCKER_HUB_ORG: '0xorg',
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										65
									
								
								packages/monorepo-scripts/src/utils/docker_hub_utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								packages/monorepo-scripts/src/utils/docker_hub_utils.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
import { fetchAsync } from '@0x/utils';
 | 
			
		||||
import { exec as execAsync } from 'promisify-child-process';
 | 
			
		||||
 | 
			
		||||
import { utils } from './utils';
 | 
			
		||||
 | 
			
		||||
const API_ENDPOINT = 'https://hub.docker.com/v2';
 | 
			
		||||
const HTTP_OK_STATUS = 200;
 | 
			
		||||
 | 
			
		||||
export const dockerHubUtils = {
 | 
			
		||||
    async getTokenAsync(): Promise<string> {
 | 
			
		||||
        const payload = {
 | 
			
		||||
            username: process.env.DOCKER_USERNAME,
 | 
			
		||||
            password: process.env.DOCKER_PASS,
 | 
			
		||||
        };
 | 
			
		||||
        const response = await fetchAsync(`${API_ENDPOINT}/users/login`, {
 | 
			
		||||
            method: 'POST',
 | 
			
		||||
            headers: {
 | 
			
		||||
                Accept: 'application/json',
 | 
			
		||||
                'Content-Type': 'application/json',
 | 
			
		||||
            },
 | 
			
		||||
            body: JSON.stringify(payload),
 | 
			
		||||
        });
 | 
			
		||||
        if (response.status !== HTTP_OK_STATUS) {
 | 
			
		||||
            throw new Error(
 | 
			
		||||
                `DockerHub user login failed (status code: ${
 | 
			
		||||
                    response.status
 | 
			
		||||
                }). Make sure you have environment variables 'DOCKER_USERNAME; and 'DOCKER_PASS' set`,
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        const respPayload = await response.json();
 | 
			
		||||
        const token = respPayload.token;
 | 
			
		||||
        return token;
 | 
			
		||||
    },
 | 
			
		||||
    async checkUserAddedToOrganizationOrThrowAsync(organization: string): Promise<void> {
 | 
			
		||||
        utils.log('Checking that the user was added to the 0xorg DockerHub organization...');
 | 
			
		||||
        const token = await dockerHubUtils.getTokenAsync();
 | 
			
		||||
        const response = await fetchAsync(`${API_ENDPOINT}/repositories/${organization}/?page_size=10`, {
 | 
			
		||||
            method: 'GET',
 | 
			
		||||
            headers: {
 | 
			
		||||
                Accept: 'application/json',
 | 
			
		||||
                Authorization: `JWT ${token}`,
 | 
			
		||||
            },
 | 
			
		||||
        });
 | 
			
		||||
        const respPayload = await response.json();
 | 
			
		||||
        if (response.status !== HTTP_OK_STATUS || respPayload.count === 0) {
 | 
			
		||||
            throw new Error(
 | 
			
		||||
                `Failed to fetch org: ${organization}'s list of repos (status code: ${
 | 
			
		||||
                    response.status
 | 
			
		||||
                }). Make sure your account has been added to the '${organization}' org on DockerHub`,
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    async loginUserToDockerCommandlineOrThrowAsync(): Promise<void> {
 | 
			
		||||
        try {
 | 
			
		||||
            utils.log('Checking that the user is logged in to docker command...');
 | 
			
		||||
            await execAsync(`echo "$DOCKER_PASS" | docker login -u $DOCKER_USERNAME --password-stdin`);
 | 
			
		||||
        } catch (err) {
 | 
			
		||||
            throw new Error(
 | 
			
		||||
                `Failed to log you into the 'docker' commandline tool. Make sure you have the 'docker' commandline tool installed. Full error: ${
 | 
			
		||||
                    err.message
 | 
			
		||||
                }`,
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
		Reference in New Issue
	
	Block a user