Merge pull request #10 from ButlerLogic/master

Merging into Stable Release
This commit is contained in:
Corey Butler 2020-01-25 10:03:45 -06:00 committed by GitHub
commit fa8ce51023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 71 deletions

2
.github/FUNDING.yml vendored
View File

@ -1,6 +1,6 @@
# These are supported funding model platforms
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
github: coreybutler
patreon: coreybutler
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username

View File

@ -4,12 +4,12 @@ This action will read a `package.json` file and compare the `version` attribute
## Usage
The following is an example `.github/main.workflow` that will execute when a `push` to the `master` branch occurs.
The following is an example `.github/main.workflow` that will execute when a `push` to the `master` branch occurs.
```yaml
name: My Workflow
on:
on:
push:
branches:
- master
@ -45,50 +45,50 @@ The `GITHUB_TOKEN` must be passed in. Without this, it is not possible to create
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
```
The action will automatically extract the token at runtime. **DO NOT MANUALLY ENTER YOUR TOKEN.** If you put the actual token in your workflow file, you're make it accessible in plaintext to anyone who ever views the repository (it wil be in your git history).
The action will automatically extract the token at runtime. **DO NOT MANUALLY ENTER YOUR TOKEN.** If you put the actual token in your workflow file, you'll make it accessible (in plaintext) to anyone who ever views the repository (it will be in your git history).
### Optional Configurations
There are several options to customize how the tag is created.
1. `package_root`
By default, autotag will look for the `package.json` file in the project root. If the file is located in a subdirectory, this option an be used to point to the correct file.
By default, autotag will look for the `package.json` file in the project root. If the file is located in a subdirectory, this option can be used to point to the correct file.
```yaml
- uses: butlerlogic/action-autotag@1.0.0
with:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
package_root: "/path/to/subdirectory"
```
1. `tag_prefx`
1. `tag_prefix`
By default, `package.json` uses [semantic versioning](https://semver.org/), 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`.
```yaml
- uses: butlerlogic/action-autotag@1.0.0
with:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
tag_prefx: "v"
tag_prefix: "v"
```
1. `tag_suffix`
Text can also be applied to the end of the tag by setting `tag_suffix`. For example, if `tag_suffix` is ` (beta)`, the tag would be `1.0.0 (beta)`. Please note this example violates semantic versioning and is merely here to illustrate how to add text to the end of a tag name if you _really_ want to.
```yaml
- uses: butlerlogic/action-autotag@1.0.0
with:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
tag_suffix: " (beta)"
```
1. `tag_message`
This is the annotated commit message associated with the tag. By default, a
changelog will be generated from the commits between the latest tag and the new tag (HEAD). This will override that with a hard-coded message.
changelog will be generated from the commits between the latest tag and the new tag (HEAD). Setting this option will override it witha custom message.
```yaml
- uses: butlerlogic/action-autotag@1.0.0
with:
@ -96,6 +96,18 @@ There are several options to customize how the tag is created.
tag_message: "Custom message goes here."
```
1. `version`
Explicitly set the version instead of automatically detecting from `package.json`.
Useful for non-JavaScript projects where version may be output by a previous action.
```yaml
- uses: butlerlogic/action-autotag@1.0.0
with:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
version: "${{ steps.previous_step.outputs.version }}"
```
## Developer Notes
If you are building an action that runs after this one, be aware this action produces several [outputs](https://help.github.com/en/articles/metadata-syntax-for-github-actions#outputs):

View File

@ -1,15 +1,15 @@
name: 'Autotagger'
description: 'Automatically generate new tags when the package.json version changes.'
author: 'ButlerLogic'
name: "Autotagger"
description: "Automatically generate new tags when the package.json version changes."
author: "ButlerLogic"
branding:
icon: 'tag'
color: 'black'
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:
@ -18,6 +18,9 @@ inputs:
tag_message:
description: This is the annotated commit message associated with the tag. By default, a changelog will be generated from the commits between the latest tag and the new tag (HEAD). This will override that with a hard-coded message.
required: false
version:
description: Explicitly set the version here instead of automatically detecting from `package.json`. Useful for non-JavaScript projects where version may be output by a previous action.
required: false
outputs:
tagname:
description: Returns the new tag value. Empty if a tag is not created.
@ -28,7 +31,7 @@ outputs:
tagmessage:
description: The messge applied to the tag reference (this is what shows up on the tag screen on Github).
version:
description: The version, as defined in package.json.
description: The version, as defined in package.json or explicitly set in the input.
runs:
using: 'node12'
main: 'lib/main.js'
using: "node12"
main: "lib/main.js"

View File

@ -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,27 +5,63 @@ 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}`)
// core.debug(` Available environment variables:\n -> ${Object.keys(process.env).map(i => i + ' :: ' + process.env[i]).join('\n -> ')}`)
// let version = ""
// 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')
// 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')
// 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()
// }
// core.setOutput('version', version)
// core.debug(` Detected version ${version}`)
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN') || process.env.INPUT_GITHUB_TOKEN.trim().length === 0) {
core.setFailed('Invalid or missing GITHUB_TOKEN.')
return
@ -34,25 +69,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 +98,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 +148,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)

View File

@ -16,7 +16,6 @@
"node",
"setup"
],
"type": "module",
"author": "ButlerLogic",
"license": "MIT",
"dependencies": {