1
0
mirror of https://github.com/benjlevesque/short-sha.git synced 2025-12-08 18:57:49 +01:00

short-sha

This commit is contained in:
Benjamin LEVESQUE
2020-03-09 21:40:47 +01:00
parent 6e8dcce3fd
commit 9f96a4f558
13 changed files with 29878 additions and 6881 deletions

View File

@@ -1,16 +1,17 @@
import * as core from '@actions/core'
import {wait} from './wait'
import {context} from '@actions/github'
import {shorten} from './shorten'
async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
core.debug(`Waiting ${ms} milliseconds ...`)
const sha = context.sha
core.debug(`Sha: ${sha}`)
const length = Number(core.getInput('length'))
core.debug(`Length: ${length}`)
const shortSha = shorten(sha, length)
core.debug(`Output: ${shortSha}`)
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
core.setOutput('time', new Date().toTimeString())
core.setOutput('sha', shortSha)
} catch (error) {
core.setFailed(error.message)
}

12
src/shorten.ts Normal file
View File

@@ -0,0 +1,12 @@
export function shorten(sha: string, length: number): string {
if (!sha) {
throw new Error('sha must be defined')
}
if (length <= 0 || !Number.isInteger(length)) {
throw new Error('length is invalid')
}
if (sha.length < length) {
throw new Error('input is too short')
}
return sha.substring(0, length)
}

View File

@@ -1,9 +0,0 @@
export async function wait(milliseconds: number): Promise<string> {
return new Promise(resolve => {
if (isNaN(milliseconds)) {
throw new Error('milliseconds not a number')
}
setTimeout(() => resolve('done!'), milliseconds)
})
}