2019-09-03 04:34:28 +07:00
|
|
|
// import * as core from '@actions/core';
|
2019-09-03 05:05:03 +07:00
|
|
|
const github = require('@actions/github')
|
2019-09-03 04:34:28 +07:00
|
|
|
const core = require('@actions/core')
|
2019-09-03 04:40:03 +07:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2019-09-03 04:11:57 +07:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
try {
|
2019-09-03 06:45:33 +07:00
|
|
|
core.debug(` Available environment variables:\n -> ${Object.keys(process.env).map(i => i + ' :: ' + process.env[i]).join('\n -> ')}`)
|
2019-09-03 05:19:01 +07:00
|
|
|
|
2019-12-18 21:28:33 +07:00
|
|
|
let version = ""
|
2019-09-03 05:16:02 +07:00
|
|
|
|
2019-12-18 21:28:33 +07:00
|
|
|
if (!process.env.hasOwnProperty('INPUT_VERSION') || process.env.INPUT_VERSION.trim().length === 0) {
|
|
|
|
let dir = fs.readdirSync(path.resolve(process.env.GITHUB_WORKSPACE), { withFileTypes: true }).map(entry => {
|
|
|
|
return `${entry.isDirectory() ? '> ' : ' - '}${entry.name}`
|
|
|
|
}).join('\n')
|
2019-09-03 06:45:33 +07:00
|
|
|
|
2019-12-18 21:28:33 +07:00
|
|
|
core.debug(` Working Directory: ${process.env.GITHUB_WORKSPACE}:\n${dir}`)
|
2019-09-03 06:45:33 +07:00
|
|
|
|
2019-12-18 21:28:33 +07:00
|
|
|
const pkg_root = core.getInput('package_root', { required: false })
|
|
|
|
let pkgfile = path.join(process.env.GITHUB_WORKSPACE, pkg_root, 'package.json')
|
|
|
|
|
|
|
|
if (!fs.existsSync(pkgfile)) {
|
|
|
|
core.setFailed('package.json does not exist.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let pkg = require(pkgfile)
|
|
|
|
version = pkg.version
|
|
|
|
} else {
|
|
|
|
version = process.env.INPUT_VERSION.trim()
|
|
|
|
}
|
2019-09-03 06:45:33 +07:00
|
|
|
|
2019-12-18 21:28:33 +07:00
|
|
|
core.setOutput('version', version)
|
|
|
|
core.debug(` Detected version ${version}`)
|
2019-09-03 06:45:33 +07:00
|
|
|
|
|
|
|
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN') || process.env.INPUT_GITHUB_TOKEN.trim().length === 0) {
|
|
|
|
core.setFailed('Invalid or missing GITHUB_TOKEN.')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-11 00:26:37 +07:00
|
|
|
// Check for existing tag
|
2019-09-03 06:45:33 +07:00
|
|
|
const git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN)
|
|
|
|
const owner = process.env.GITHUB_ACTOR
|
|
|
|
const repo = process.env.GITHUB_REPOSITORY.split('/').pop()
|
|
|
|
|
2019-09-11 00:26:37 +07:00
|
|
|
let tags
|
|
|
|
try {
|
|
|
|
tags = await git.repos.listTags({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
per_page: 100
|
|
|
|
})
|
|
|
|
} catch (e) {
|
2019-09-13 05:43:51 +07:00
|
|
|
tags = {
|
|
|
|
data: []
|
|
|
|
}
|
2019-09-11 00:26:37 +07:00
|
|
|
}
|
2019-09-03 06:45:33 +07:00
|
|
|
|
|
|
|
// Check for existance of tag and abort (short circuit) if it already exists.
|
|
|
|
for (let tag of tags.data) {
|
2019-12-18 21:28:33 +07:00
|
|
|
if (tag.name.trim().toLowerCase() === version.trim().toLowerCase()) {
|
2019-09-03 06:45:33 +07:00
|
|
|
core.warning(`"${tag.name.trim()}" tag already exists.`)
|
|
|
|
core.setOutput('tagname', '')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new tag name
|
2019-12-18 21:28:33 +07:00
|
|
|
let tagName = version
|
2019-09-03 06:45:33 +07:00
|
|
|
const tagPrefix = core.getInput('tag_prefix', { required: false })
|
|
|
|
const tagSuffix = core.getInput('tag_suffix', { required: false })
|
2019-09-03 07:04:57 +07:00
|
|
|
let tagMsg = core.getInput('tag_message', { required: false }).trim()
|
2019-09-03 06:45:33 +07:00
|
|
|
|
|
|
|
tagName = `${tagPrefix}${tagName}${tagSuffix}`
|
|
|
|
|
|
|
|
if (tagMsg.length === 0 && tags.data.length > 0) {
|
|
|
|
try {
|
|
|
|
latestTag = tags.data.shift()
|
|
|
|
|
|
|
|
let changelog = await git.repos.compareCommits({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
base: latestTag.name,
|
|
|
|
head: 'master'
|
|
|
|
})
|
|
|
|
|
2019-09-11 00:26:37 +07:00
|
|
|
tagMsg = changelog.data.commits.map(commit => `**1) ${commit.commit.message}**${commit.hasOwnProperty('author') ? (commit.author.hasOwnProperty('login') ? ' (' + commit.author.login + ')' : '') : ''}\n(SHA: ${commit.sha})\n`).join('\n')
|
2019-09-03 06:45:33 +07:00
|
|
|
} catch (e) {
|
2019-09-19 05:48:16 +07:00
|
|
|
core.warning('Failed to generate changelog from commits: ' + e.message)
|
|
|
|
tagMsg = `${tagPrefix}${tagName}${tagSuffix}`
|
2019-09-03 06:45:33 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let newTag
|
|
|
|
|
|
|
|
try {
|
2019-09-03 07:04:57 +07:00
|
|
|
tagMsg = tagMsg.trim().length > 0
|
|
|
|
? tagMsg
|
2019-12-18 21:28:33 +07:00
|
|
|
: `Version ${version}`
|
2019-09-03 07:04:57 +07:00
|
|
|
|
2019-09-03 06:45:33 +07:00
|
|
|
newTag = await git.git.createTag({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
tag: tagName,
|
2019-09-03 07:04:57 +07:00
|
|
|
message: tagMsg,
|
2019-09-03 06:45:33 +07:00
|
|
|
object: process.env.GITHUB_SHA,
|
|
|
|
type: 'commit'
|
|
|
|
})
|
|
|
|
|
|
|
|
core.warning(`Created new tag: ${newTag.data.tag}`)
|
|
|
|
} catch (e) {
|
|
|
|
core.setFailed(e.message)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let newReference
|
|
|
|
|
|
|
|
try {
|
|
|
|
newReference = await git.git.createRef({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
ref: `refs/tags/${newTag.data.tag}`,
|
|
|
|
sha: newTag.data.sha
|
|
|
|
})
|
|
|
|
|
|
|
|
core.warning(`Reference ${newReference.data.ref} available at ${newReference.data.url}`)
|
|
|
|
} catch (e) {
|
|
|
|
core.warning({
|
|
|
|
owner,
|
|
|
|
repo,
|
|
|
|
ref: `refs/tags/${newTag.data.tag}`,
|
|
|
|
sha: newTag.data.sha
|
|
|
|
})
|
2019-09-03 05:46:08 +07:00
|
|
|
|
2019-09-03 06:45:33 +07:00
|
|
|
core.setFailed(e.message)
|
|
|
|
return
|
|
|
|
}
|
2019-09-03 05:46:08 +07:00
|
|
|
|
2019-09-03 06:45:33 +07:00
|
|
|
// Store values for other actions
|
|
|
|
if (typeof newTag === 'object' && typeof newReference === 'object') {
|
|
|
|
core.setOutput('tagname', tagName)
|
|
|
|
core.setOutput('tagsha', newTag.data.sha)
|
|
|
|
core.setOutput('taguri', newReference.data.url)
|
2019-09-03 07:04:57 +07:00
|
|
|
core.setOutput('tagmessage', tagMsg.trim())
|
2019-09-03 06:45:33 +07:00
|
|
|
}
|
2019-09-03 04:11:57 +07:00
|
|
|
} catch (error) {
|
2019-09-03 06:45:33 +07:00
|
|
|
core.warning(error.message)
|
|
|
|
core.setOutput('tagname', '')
|
|
|
|
core.setOutput('tagsha', '')
|
|
|
|
core.setOutput('taguri', '')
|
2019-09-03 07:04:57 +07:00
|
|
|
core.setOutput('tagmessage', '')
|
2019-09-03 04:11:57 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run()
|