From 38fe2a342899c2d038f9cae81b1625c52162ce8b Mon Sep 17 00:00:00 2001 From: Mathias Beugnon Date: Tue, 22 Oct 2019 13:40:28 +0100 Subject: [PATCH] Fix owner --- action.yml | 4 +-- lib/main.js | 92 +++++++++++++++++++++++++++++------------------------ 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/action.yml b/action.yml index 1e02d11..384c88e 100644 --- a/action.yml +++ b/action.yml @@ -2,14 +2,14 @@ name: 'Autotagger' description: 'Automatically generate new tags when the package.json version changes.' author: 'ButlerLogic' branding: - icon: 'tag' + icon: 'tag' color: 'black' inputs: package_root: description: Autotag will look for the package.json file in in this location. required: false default: './' - tag_prefx: + tag_prefix: description: By default, package.json uses semantic versioning, such as "1.0.0". A prefix can be used to add text before the tag name. For example, if tag_prefx is set to "v", then the tag would be labeled as "v1.0.0". required: false tag_suffix: diff --git a/lib/main.js b/lib/main.js index a9cae47..69da26f 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,4 +1,3 @@ -// import * as core from '@actions/core'; const github = require('@actions/github') const core = require('@actions/core') const fs = require('fs') @@ -6,24 +5,31 @@ const path = require('path') async function run() { try { - core.debug(` Available environment variables:\n -> ${Object.keys(process.env).map(i => i + ' :: ' + process.env[i]).join('\n -> ')}`) + const { repo } = github.context - let dir = fs.readdirSync(path.resolve(process.env.GITHUB_WORKSPACE), { withFileTypes: true }).map(entry => { - return `${entry.isDirectory() ? '> ' : ' - '}${entry.name}` - }).join('\n') + 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') core.debug(` Working Directory: ${process.env.GITHUB_WORKSPACE}:\n${dir}`) - - const pkg_root = core.getInput('package_root', { required: false }) - let pkgfile = path.join(process.env.GITHUB_WORKSPACE, pkg_root, 'package.json') + const pkg_root = core.getInput('package_root', { required: false }) + const 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) - + const pkg = require(pkgfile) core.setOutput('version', pkg.version) core.debug(` Detected version ${pkg.version}`) @@ -34,25 +40,28 @@ async function run() { // Check for existing tag const git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN) - const owner = process.env.GITHUB_ACTOR - const repo = process.env.GITHUB_REPOSITORY.split('/').pop() let tags try { tags = await git.repos.listTags({ - owner, - repo, - per_page: 100 + ...repo, + per_page: 100, }) } catch (e) { tags = { - data: [] + data: [], } } + const getTagName = version => { + const tagPrefix = core.getInput('tag_prefix', { required: false }) + const tagSuffix = core.getInput('tag_suffix', { required: false }) + return `${tagPrefix}${version}${tagSuffix}` + } + // Check for existance of tag and abort (short circuit) if it already exists. for (let tag of tags.data) { - if (tag.name.trim().toLowerCase() === pkg.version.trim().toLowerCase()) { + if (tag.name === getTagName(pkg.version)) { core.warning(`"${tag.name.trim()}" tag already exists.`) core.setOutput('tagname', '') return @@ -60,45 +69,47 @@ async function run() { } // Create the new tag name - let tagName = pkg.version - const tagPrefix = core.getInput('tag_prefix', { required: false }) - const tagSuffix = core.getInput('tag_suffix', { required: false }) + const tagName = getTagName(pkg.version) + let tagMsg = core.getInput('tag_message', { required: false }).trim() - - 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, + ...repo, base: latestTag.name, - head: 'master' + head: 'master', }) - 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') + 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') } catch (e) { core.warning('Failed to generate changelog from commits: ' + e.message) - tagMsg = `${tagPrefix}${tagName}${tagSuffix}` + tagMsg = tagName } } let newTag - try { - tagMsg = tagMsg.trim().length > 0 - ? tagMsg - : `Version ${pkg.version}` + tagMsg = tagMsg.trim().length > 0 ? tagMsg : `Version ${pkg.version}` newTag = await git.git.createTag({ - owner, - repo, + ...repo, tag: tagName, message: tagMsg, object: process.env.GITHUB_SHA, - type: 'commit' + type: 'commit', }) core.warning(`Created new tag: ${newTag.data.tag}`) @@ -108,22 +119,19 @@ async function run() { } let newReference - try { newReference = await git.git.createRef({ - owner, - repo, + ...repo, ref: `refs/tags/${newTag.data.tag}`, - sha: newTag.data.sha + sha: newTag.data.sha, }) core.warning(`Reference ${newReference.data.ref} available at ${newReference.data.url}`) } catch (e) { core.warning({ - owner, - repo, + ...repo, ref: `refs/tags/${newTag.data.tag}`, - sha: newTag.data.sha + sha: newTag.data.sha, }) core.setFailed(e.message)