-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
500dce0
commit bf963eb
Showing
5 changed files
with
201 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
plugins/babel-plugin-canyon/src/ci_providers/provider_vercel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IServiceParams> { | ||
const { args } = inputs | ||
let serviceParams: Partial<IServiceParams> | 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.`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters