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:
17
src/main.ts
17
src/main.ts
@@ -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
12
src/shorten.ts
Normal 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)
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user