feat: support partial GITHUB_REPOSITORY

This commit is contained in:
rlespinasse
2021-01-25 20:49:01 +01:00
committed by Romain Lespinasse
parent 49e1056c70
commit 75ce03b6a0
16 changed files with 467 additions and 71 deletions

View File

@@ -1,5 +1,9 @@
import * as core from '@actions/core'
import {slugref, slug, slugurl, slugurlref, shortsha} from './slug'
import {slugref, slug, slugurl, slugurlref} from './slug'
import {shortsha} from './short'
import {get_first_part, get_second_part} from './partial'
const SEPARATOR = '/'
/**
* Inputs environments variables keys from Github actions job
@@ -12,20 +16,40 @@ const GITHUB_BASE_REF = 'GITHUB_BASE_REF'
const GITHUB_SHA = 'GITHUB_SHA'
const GITHUB_EVENT_PATH = 'GITHUB_EVENT_PATH'
/**
* Partial outputs environments variables keys
*/
const GITHUB_REPOSITORY_OWNER_PART = 'GITHUB_REPOSITORY_OWNER_PART'
const GITHUB_REPOSITORY_NAME_PART = 'GITHUB_REPOSITORY_NAME_PART'
/**
* Slugged outputs environments variables keys
*/
const GITHUB_REPOSITORY_SLUG = 'GITHUB_REPOSITORY_SLUG'
const GITHUB_REPOSITORY_SLUG_URL = 'GITHUB_REPOSITORY_SLUG_URL'
const GITHUB_REPOSITORY_OWNER_PART_SLUG = 'GITHUB_REPOSITORY_OWNER_PART_SLUG'
const GITHUB_REPOSITORY_NAME_PART_SLUG = 'GITHUB_REPOSITORY_NAME_PART_SLUG'
const GITHUB_REF_SLUG = 'GITHUB_REF_SLUG'
const GITHUB_HEAD_REF_SLUG = 'GITHUB_HEAD_REF_SLUG'
const GITHUB_BASE_REF_SLUG = 'GITHUB_BASE_REF_SLUG'
const GITHUB_EVENT_REF_SLUG = 'GITHUB_EVENT_REF_SLUG'
/**
* URL-Slugged outputs environments variables keys
*/
const GITHUB_REPOSITORY_SLUG_URL = 'GITHUB_REPOSITORY_SLUG_URL'
const GITHUB_REPOSITORY_OWNER_PART_SLUG_URL =
'GITHUB_REPOSITORY_OWNER_PART_SLUG_URL'
const GITHUB_REPOSITORY_NAME_PART_SLUG_URL =
'GITHUB_REPOSITORY_NAME_PART_SLUG_URL'
const GITHUB_REF_SLUG_URL = 'GITHUB_REF_SLUG_URL'
const GITHUB_HEAD_REF_SLUG_URL = 'GITHUB_HEAD_REF_SLUG_URL'
const GITHUB_BASE_REF_SLUG_URL = 'GITHUB_BASE_REF_SLUG_URL'
const GITHUB_SHA_SHORT = 'GITHUB_SHA_SHORT'
const GITHUB_EVENT_REF_SLUG = 'GITHUB_EVENT_REF_SLUG'
const GITHUB_EVENT_REF_SLUG_URL = 'GITHUB_EVENT_REF_SLUG_URL'
/**
* Shorted outputs environments variables keys
*/
const GITHUB_SHA_SHORT = 'GITHUB_SHA_SHORT'
const GITHUB_EVENT_PULL_REQUEST_HEAD_SHA_SHORT =
'GITHUB_EVENT_PULL_REQUEST_HEAD_SHA_SHORT'
@@ -48,9 +72,32 @@ async function run(): Promise<void> {
}
}
exportFirstPart(GITHUB_REPOSITORY, SEPARATOR, GITHUB_REPOSITORY_OWNER_PART)
exportSecondPart(GITHUB_REPOSITORY, SEPARATOR, GITHUB_REPOSITORY_NAME_PART)
exportSlug(GITHUB_REPOSITORY, GITHUB_REPOSITORY_SLUG)
exportFirstPartSlug(
GITHUB_REPOSITORY,
SEPARATOR,
GITHUB_REPOSITORY_OWNER_PART_SLUG
)
exportSecondPartSlug(
GITHUB_REPOSITORY,
SEPARATOR,
GITHUB_REPOSITORY_NAME_PART_SLUG
)
exportSlugUrl(GITHUB_REPOSITORY, GITHUB_REPOSITORY_SLUG_URL)
exportFirstPartSlugUrl(
GITHUB_REPOSITORY,
SEPARATOR,
GITHUB_REPOSITORY_OWNER_PART_SLUG_URL
)
exportSecondPartSlugUrl(
GITHUB_REPOSITORY,
SEPARATOR,
GITHUB_REPOSITORY_NAME_PART_SLUG_URL
)
exportSlugRef(GITHUB_REF, GITHUB_REF_SLUG)
exportSlugRef(GITHUB_HEAD_REF, GITHUB_HEAD_REF_SLUG)
@@ -66,6 +113,28 @@ async function run(): Promise<void> {
}
}
function exportFirstPart(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
core.exportVariable(outputKey, get_first_part(envVar, separator))
}
}
function exportSecondPart(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
core.exportVariable(outputKey, get_second_part(envVar, separator))
}
}
function exportSlug(inputKey: string, outputKey: string): void {
const envVar = process.env[inputKey]
if (envVar) {
@@ -73,6 +142,30 @@ function exportSlug(inputKey: string, outputKey: string): void {
}
}
function exportFirstPartSlug(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
const value = get_first_part(envVar, separator)
core.exportVariable(outputKey, slug(value))
}
}
function exportSecondPartSlug(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
const value = get_second_part(envVar, separator)
core.exportVariable(outputKey, slug(value))
}
}
function exportSlugRef(inputKey: string, outputKey: string): void {
const envVar = process.env[inputKey]
if (envVar) {
@@ -87,6 +180,30 @@ function exportSlugUrl(inputKey: string, outputKey: string): void {
}
}
function exportFirstPartSlugUrl(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
const value = get_first_part(envVar, separator)
core.exportVariable(outputKey, slugurl(value))
}
}
function exportSecondPartSlugUrl(
inputKey: string,
separator: string,
outputKey: string
): void {
const envVar = process.env[inputKey]
if (envVar) {
const value = get_second_part(envVar, separator)
core.exportVariable(outputKey, slugurl(value))
}
}
function exportSlugUrlRef(inputKey: string, outputKey: string): void {
const envVar = process.env[inputKey]
if (envVar) {

17
src/partial.ts Normal file
View File

@@ -0,0 +1,17 @@
/**
* Get the first part of envVar
* @param envVar to be split
* @param separator of the split
*/
export function get_first_part(envVar: string, separator: string): string {
return envVar.replace(RegExp(`${separator}.*$`), '')
}
/**
* Get the second part of envVar
* @param envVar to be split
* @param separator of the split
*/
export function get_second_part(envVar: string, separator: string): string {
return envVar.replace(RegExp(`^.*${separator}`), '')
}

10
src/short.ts Normal file
View File

@@ -0,0 +1,10 @@
const SHORT_SHA_SIZE = 8
/**
* slug will take envVar and then :
* - limit the string size to 8 characters
* @param envVar to be slugged
*/
export function shortsha(envVar: string): string {
return envVar.substring(0, SHORT_SHA_SIZE)
}

View File

@@ -1,5 +1,4 @@
const MAX_SLUG_STRING_SIZE = 63
const SHORT_SHA_SIZE = 8
/**
* slug will take envVar and then :
@@ -53,15 +52,6 @@ export function slugurlref(envVar: string): string {
return slugurl(slugref(envVar))
}
/**
* slug will take envVar and then :
* - limit the string size to 8 characters
* @param envVar to be slugged
*/
export function shortsha(envVar: string): string {
return envVar.substring(0, SHORT_SHA_SIZE)
}
function trailHyphen(envVar: string): string {
return envVar.replace(RegExp('^-*', 'g'), '').replace(RegExp('-*$', 'g'), '')
}