From bf963ebf170ea18a37ebd918c735a3128c6eec8d Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Tue, 19 Nov 2024 14:24:34 +0800 Subject: [PATCH] chore: repo overall update --- .../src/ci_providers/index.ts | 2 + .../src/ci_providers/provider_vercel.ts | 67 +++++++++++++++++++ .../babel-plugin-canyon/src/helpers/logger.ts | 64 ++++++++++++++++++ .../src/helpers/provider.ts | 53 +++++++++++++++ plugins/babel-plugin-canyon/src/index.ts | 29 ++++---- 5 files changed, 201 insertions(+), 14 deletions(-) create mode 100644 plugins/babel-plugin-canyon/src/ci_providers/provider_vercel.ts create mode 100644 plugins/babel-plugin-canyon/src/helpers/logger.ts create mode 100644 plugins/babel-plugin-canyon/src/helpers/provider.ts diff --git a/plugins/babel-plugin-canyon/src/ci_providers/index.ts b/plugins/babel-plugin-canyon/src/ci_providers/index.ts index 6e532c23..7534d0e3 100644 --- a/plugins/babel-plugin-canyon/src/ci_providers/index.ts +++ b/plugins/babel-plugin-canyon/src/ci_providers/index.ts @@ -2,10 +2,12 @@ import { IProvider } from '../types' import * as providerGitHubactions from './provider_githubactions' import * as providerGitLabci from './provider_gitlabci' +import * as providerVercel from './provider_vercel' const providerList: IProvider[] = [ providerGitHubactions, providerGitLabci, + providerVercel ] export default providerList diff --git a/plugins/babel-plugin-canyon/src/ci_providers/provider_vercel.ts b/plugins/babel-plugin-canyon/src/ci_providers/provider_vercel.ts new file mode 100644 index 00000000..8b5aff39 --- /dev/null +++ b/plugins/babel-plugin-canyon/src/ci_providers/provider_vercel.ts @@ -0,0 +1,67 @@ +import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types' + + +export function detect(envs: UploaderEnvs): boolean { + return Boolean(envs.VERCEL) +} + +function _getBranch(inputs: UploaderInputs): string { + const { args, envs } = inputs + const branchRegex = /refs\/heads\/(.*)/ + const branchMatches = branchRegex.exec(envs.VERCEL_GIT_COMMIT_REF || '') + let branch + if (branchMatches) { + branch = branchMatches[1] + } + + if (envs.VERCEL_GIT_COMMIT_REF && envs.VERCEL_GIT_COMMIT_REF !== '') { + branch = envs.VERCEL_GIT_COMMIT_REF + } + return args.branch || branch || '' +} + + +function _getService(): string { + return 'vercel' +} + +export function getServiceName(): string { + return 'Vercel' +} + +function _getSHA(inputs: UploaderInputs): string { + const { args, envs } = inputs + return args.sha || envs.VERCEL_GIT_COMMIT_SHA +} + +function _getSlug(inputs: UploaderInputs): string { + const { args, envs } = inputs + // if (args.slug !== '') return args.slug + return envs.VERCEL_GIT_REPO_ID || '' +} + +export function getServiceParams(inputs: UploaderInputs): IServiceParams { + return { + branch: _getBranch(inputs), + // build: _getBuild(inputs), + // buildURL: await _getBuildURL(inputs), + commit: _getSHA(inputs), + // job: _getJob(inputs.envs), + // pr: _getPR(inputs), + service: _getService(), + slug: _getSlug(inputs), + } +} + +export function getEnvVarNames(): string[] { + return [ + 'GITHUB_ACTION', + 'GITHUB_HEAD_REF', + 'GITHUB_REF', + 'GITHUB_REPOSITORY', + 'GITHUB_RUN_ID', + 'GITHUB_SERVER_URL', + 'GITHUB_SHA', + 'GITHUB_WORKFLOW', + ] +} diff --git a/plugins/babel-plugin-canyon/src/helpers/logger.ts b/plugins/babel-plugin-canyon/src/helpers/logger.ts new file mode 100644 index 00000000..8f7e797c --- /dev/null +++ b/plugins/babel-plugin-canyon/src/helpers/logger.ts @@ -0,0 +1,64 @@ +/** + * We really only need three log levels + * * Error + * * Info + * * Verbose + */ + +function _getTimestamp() { + return new Date().toISOString() +} + +/** + * + * @param {string} message - message to log + * @param {boolean} shouldVerbose - value of the verbose flag + * @return void + */ +export function verbose(message: string, shouldVerbose: boolean): void { + if (shouldVerbose === true) { + console.debug(`[${_getTimestamp()}] ['verbose'] ${message}`) + } +} + +/** + * + * @param {string} message - message to log + * @return void + */ +export function logError(message: string): void { + console.error(`[${_getTimestamp()}] ['error'] ${message}`) +} + +/** + * + * @param {string} message - message to log + * @return void + */ +export function info(message: string): void { + console.log(`[${_getTimestamp()}] ['info'] ${message}`) +} + +export class UploadLogger { + private static _instance: UploadLogger + logLevel = 'info' + + private constructor() { + // Intentionally empty + } + + static getInstance(): UploadLogger { + if (!UploadLogger._instance) { + UploadLogger._instance = new UploadLogger() + } + return UploadLogger._instance; + } + + static setLogLevel(level: string) { + UploadLogger.getInstance().logLevel = level + } + + static verbose(message: string) { + verbose(message, UploadLogger.getInstance().logLevel === 'verbose') + } +} diff --git a/plugins/babel-plugin-canyon/src/helpers/provider.ts b/plugins/babel-plugin-canyon/src/helpers/provider.ts new file mode 100644 index 00000000..5aa93f8e --- /dev/null +++ b/plugins/babel-plugin-canyon/src/helpers/provider.ts @@ -0,0 +1,53 @@ +import providers from '../ci_providers' +import { info, logError, UploadLogger } from '../helpers/logger' +import { IServiceParams, UploaderInputs } from '../types' + +export function detectProvider( + inputs: UploaderInputs, + hasToken = false, +): Partial { + const { args } = inputs + let serviceParams: Partial | undefined + + // check if we have a complete set of manual overrides (slug, SHA) + if (args.sha && (args.slug || hasToken)) { + // We have the needed args for a manual override + info(`Using manual override from args.`) + serviceParams = { + commit: args.sha, + ...(hasToken ? {} : { slug: args.slug }), + } + } else { + serviceParams = undefined + } + + // loop though all providers + try { + const serviceParams = walkProviders(inputs) + return { ...serviceParams, ...serviceParams } + } catch (error) { + // if fails, display message explaining failure, and explaining that SHA and slug need to be set as args + if (typeof serviceParams !== 'undefined') { + logError(`Error detecting repos setting using git: ${error}`) + } else { + // throw new Error( + // '\nUnable to detect SHA and slug, please specify them manually.\nSee the help for more details.', + // ) + } + } + return serviceParams +} + +export function walkProviders(inputs: UploaderInputs): IServiceParams { + for (const provider of providers) { + if (provider.detect(inputs.envs)) { + info(`Detected ${provider.getServiceName()} as the CI provider.`) + UploadLogger.verbose('-> Using the following env variables:') + for (const envVarName of provider.getEnvVarNames()) { + UploadLogger.verbose(` ${envVarName}: ${inputs.envs[envVarName]}`) + } + return provider.getServiceParams(inputs) + } + } + throw new Error(`Unable to detect provider.`) +} diff --git a/plugins/babel-plugin-canyon/src/index.ts b/plugins/babel-plugin-canyon/src/index.ts index e8f66322..0b20219b 100644 --- a/plugins/babel-plugin-canyon/src/index.ts +++ b/plugins/babel-plugin-canyon/src/index.ts @@ -1,8 +1,9 @@ -import generate from "@babel/generator"; +// import generate from "@babel/generator"; import { declare } from "@babel/helper-plugin-utils"; -import packageJson from "../package.json"; +// import packageJson from "../package.json"; import {visitorProgramExit} from "./visitor-program-exit"; -import providers from './ci_providers' +// import providers from './ci_providers' +import {detectProvider} from "./helpers/provider"; export default declare((api, config) => { api.assertVersion(7); @@ -10,18 +11,18 @@ export default declare((api, config) => { visitor: { Program: { exit: (path) => { - for (const provider of providers) { - if (provider.detect(process.env)) { - const con = provider.getServiceParams({ - envs: process.env, - // @ts-ignore - args: {} - }) - console.log(con) + // 侦测流水线 + // 优先级:手动设置 > CI/CD提供商 + const serviceParams = detectProvider({ + envs: process.env, + args: { + projectID: config.projectID, + sha: config.sha, + instrumentCwd: config.instrumentCwd, + branch: config.branch, } - } - // throw new Error(`Unable to detect provider.`) - // console.log("Babel Plugin Canyon") + }) + console.log(serviceParams,'serviceParams') visitorProgramExit(api,path) } },