autotag/lib/main.js

175 lines
5.0 KiB
JavaScript
Raw Normal View History

2019-09-03 04:34:28 +07:00
const core = require('@actions/core')
2020-01-31 07:33:32 +07:00
const { GitHub, context } = require('@actions/github')
2019-09-03 04:40:03 +07:00
const fs = require('fs')
const path = require('path')
const os = require('os')
2019-09-03 04:11:57 +07:00
async function run() {
try {
2019-10-22 19:40:28 +07:00
core.debug(
` Available environment variables:\n -> ${Object.keys(process.env)
.map(i => i + ' :: ' + process.env[i])
.join('\n -> ')}`
)
const dir = fs
.readdirSync(path.resolve(process.env.GITHUB_WORKSPACE), { withFileTypes: true })
.map(entry => {
return `${entry.isDirectory() ? '> ' : ' - '}${entry.name}`
})
.join('\n')
2019-09-03 05:16:02 +07:00
2019-09-03 06:45:33 +07:00
core.debug(` Working Directory: ${process.env.GITHUB_WORKSPACE}:\n${dir}`)
if (!process.env.hasOwnProperty('GITHUB_TOKEN')) {
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN')) {
2020-01-31 06:25:31 +07:00
core.setFailed('Invalid or missing GITHUB_TOKEN.')
return
}
2020-01-31 07:33:32 +07:00
}
2020-01-31 06:25:31 +07:00
2019-10-22 19:40:28 +07:00
const pkg_root = core.getInput('package_root', { required: false })
const pkgfile = path.join(process.env.GITHUB_WORKSPACE, pkg_root, 'package.json')
2019-09-03 06:45:33 +07:00
if (!fs.existsSync(pkgfile)) {
core.setFailed('package.json does not exist.')
return
}
2019-10-22 19:40:28 +07:00
const pkg = require(pkgfile)
2019-09-03 06:45:33 +07:00
core.setOutput('version', pkg.version)
core.debug(` Detected version ${pkg.version}`)
2020-01-31 07:33:32 +07:00
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
const github = new GitHub(process.env.GITHUB_TOKEN || process.env.INPUT_GITHUB_TOKEN)
// Get owner and repo from context of payload that triggered the action
const { owner, repo } = context.repo
// // Check for existing tag
// const git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN || process.env.GITHUB_TOKEN)
// const owner = process.env.GITHUB_REPOSITORY.split('/').shift()
// const repo = process.env.GITHUB_REPOSITORY.split('/').pop()
2019-09-03 06:45:33 +07:00
let tags
try {
2020-01-31 07:33:32 +07:00
tags = await github.repos.listTags({
owner,
repo,
2019-10-22 19:40:28 +07:00
per_page: 100,
})
} catch (e) {
2019-09-13 05:43:51 +07:00
tags = {
2019-10-22 19:40:28 +07:00
data: [],
2019-09-13 05:43:51 +07:00
}
}
2019-09-03 06:45:33 +07:00
2020-01-31 07:33:32 +07:00
const tagPrefix = core.getInput('tag_prefix', { required: false })
const tagSuffix = core.getInput('tag_suffix', { required: false })
2019-10-22 19:40:28 +07:00
const getTagName = version => {
return `${tagPrefix}${version}${tagSuffix}`
}
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-10-22 19:40:28 +07:00
if (tag.name === getTagName(pkg.version)) {
core.warning(`"${tag.name.trim()}" tag already exists.` + os.EOL)
2019-09-03 06:45:33 +07:00
core.setOutput('tagname', '')
return
}
}
// Create the new tag name
2019-10-22 19:40:28 +07:00
const tagName = getTagName(pkg.version)
2019-09-03 06:45:33 +07:00
2019-10-22 19:40:28 +07:00
let tagMsg = core.getInput('tag_message', { required: false }).trim()
2019-09-03 06:45:33 +07:00
if (tagMsg.length === 0 && tags.data.length > 0) {
try {
latestTag = tags.data.shift()
2020-01-31 07:33:32 +07:00
let changelog = await github.repos.compareCommits({
owner,
repo,
2019-09-03 06:45:33 +07:00
base: latestTag.name,
2019-10-22 19:40:28 +07:00
head: 'master',
2019-09-03 06:45:33 +07:00
})
2019-10-22 19:40:28 +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) {
core.warning('Failed to generate changelog from commits: ' + e.message + os.EOL)
2019-10-22 19:40:28 +07:00
tagMsg = tagName
2019-09-03 06:45:33 +07:00
}
}
let newTag
try {
2019-10-22 19:40:28 +07:00
tagMsg = tagMsg.trim().length > 0 ? tagMsg : `Version ${pkg.version}`
2019-09-03 07:04:57 +07:00
2020-01-31 07:33:32 +07:00
newTag = await github.git.createTag({
owner,
repo,
2019-09-03 06:45:33 +07:00
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,
2020-01-31 07:33:32 +07:00
type: 'commit'
2019-09-03 06:45:33 +07:00
})
2020-01-31 07:33:32 +07:00
core.warning(`Created new tag: ${newTag.data.tag}`)
2019-09-03 06:45:33 +07:00
} catch (e) {
core.setFailed(e.message)
return
}
let newReference
try {
2020-01-31 07:33:32 +07:00
newReference = await github.git.createRef({
owner,
repo,
2019-09-03 06:45:33 +07:00
ref: `refs/tags/${newTag.data.tag}`,
2019-10-22 19:40:28 +07:00
sha: newTag.data.sha,
2019-09-03 06:45:33 +07:00
})
2020-01-31 07:33:32 +07:00
core.warning(`Reference ${newReference.data.ref} available at ${newReference.data.url}` + os.EOL)
2019-09-03 06:45:33 +07:00
} catch (e) {
core.warning({
2020-01-31 07:33:32 +07:00
owner,
repo,
2019-09-03 06:45:33 +07:00
ref: `refs/tags/${newTag.data.tag}`,
2019-10-22 19:40:28 +07:00
sha: newTag.data.sha,
2019-09-03 06:45:33 +07:00
})
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())
2020-01-28 07:14:23 +07:00
core.setOutput('tagref', newReference.data.ref)
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', '')
2020-01-28 07:14:23 +07:00
core.setOutput('tagref', '')
2019-09-03 04:11:57 +07:00
}
}
run()