mirror of
https://github.com/ButlerLogic/action-autotag.git
synced 2025-09-08 22:24:03 +07:00
Updated to use new strategies with controlled Docker environment.
This commit is contained in:
15
app/lib/docker.js
Normal file
15
app/lib/docker.js
Normal file
@ -0,0 +1,15 @@
|
||||
import Regex from './regex.js'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
export default class Dockerfile extends Regex {
|
||||
constructor (root = null) {
|
||||
root = path.join(process.env.GITHUB_WORKSPACE, root)
|
||||
|
||||
if (fs.statSync(root).isDirectory()) {
|
||||
root = path.join(root, 'Dockerfile')
|
||||
}
|
||||
|
||||
super(root, /LABEL[\s\t]+version=[\t\s+]?[\"\']?([0-9\.]+)[\"\']?/i)
|
||||
}
|
||||
}
|
23
app/lib/package.js
Normal file
23
app/lib/package.js
Normal file
@ -0,0 +1,23 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export default class Package {
|
||||
constructor (root = './') {
|
||||
root = path.join(process.env.GITHUB_WORKSPACE, root)
|
||||
|
||||
if (fs.statSync(root).isDirectory()) {
|
||||
root = path.join(root, 'package.json')
|
||||
}
|
||||
|
||||
if (!fs.existsSync(root)) {
|
||||
throw new Error(`package.json does not exist at ${root}.`)
|
||||
}
|
||||
|
||||
this.root = root
|
||||
this.data = JSON.parse(fs.readFileSync(root))
|
||||
}
|
||||
|
||||
get version () {
|
||||
return this.data.version
|
||||
}
|
||||
}
|
34
app/lib/regex.js
Normal file
34
app/lib/regex.js
Normal file
@ -0,0 +1,34 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export default class Regex {
|
||||
constructor (root = null, pattern) {
|
||||
root = path.join(process.env.GITHUB_WORKSPACE, root)
|
||||
|
||||
if (fs.statSync(root).isDirectory()) {
|
||||
throw new Error(`${root} is a directory. The Regex tag identification strategy requires a file.`)
|
||||
}
|
||||
|
||||
if (!fs.existsSync(root)) {
|
||||
throw new Error(`"${root}" does not exist.`)
|
||||
}
|
||||
|
||||
this.content = fs.readFileSync(root).toString()
|
||||
|
||||
let content = pattern.exec(this.content)
|
||||
if (!content) {
|
||||
this._version = null
|
||||
// throw new Error(`Could not find pattern matching "${pattern.toString()}" in "${root}".`)
|
||||
} else {
|
||||
this._version = content[1]
|
||||
}
|
||||
}
|
||||
|
||||
get version () {
|
||||
return this._version
|
||||
}
|
||||
|
||||
get versionFound () {
|
||||
return this._version !== null
|
||||
}
|
||||
}
|
33
app/lib/setup.js
Normal file
33
app/lib/setup.js
Normal file
@ -0,0 +1,33 @@
|
||||
import core from '@actions/core'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
export default class Setup {
|
||||
static debug () {
|
||||
// Metadate for debugging
|
||||
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}`)
|
||||
}
|
||||
|
||||
static requireAnyEnv () {
|
||||
for (const arg of arguments) {
|
||||
if (!process.env.hasOwnProperty(arg)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error('At least one of the following environment variables is required: ' + Array.slice(arguments).join(', '))
|
||||
}
|
||||
}
|
135
app/lib/tag.js
Normal file
135
app/lib/tag.js
Normal file
@ -0,0 +1,135 @@
|
||||
import core from '@actions/core'
|
||||
import os from 'os'
|
||||
import { GitHub, context } from '@actions/github'
|
||||
|
||||
// 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
|
||||
|
||||
export default class Tag {
|
||||
constructor (prefix, version, postfix) {
|
||||
this.prefix = prefix
|
||||
this.version = version
|
||||
this.postfix = postfix
|
||||
this._tags = null
|
||||
this._message = null
|
||||
this._exists = null
|
||||
}
|
||||
|
||||
get name () {
|
||||
return `${this.prefix.trim()}${this.version.trim()}${this.postfix.trim()}`
|
||||
}
|
||||
|
||||
set message (value) {
|
||||
if (value && value.length > 0) {
|
||||
this._message = value
|
||||
}
|
||||
}
|
||||
|
||||
async getMessage () {
|
||||
if (this._message !== null) {
|
||||
return this._message
|
||||
}
|
||||
|
||||
try {
|
||||
const changelog = await github.repos.compareCommits({ owner, repo, base: tags.data.shift().name, head: 'master' })
|
||||
|
||||
return changelog.data.commits
|
||||
.map(
|
||||
(commit, i) =>
|
||||
`${i + 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 + os.EOL)
|
||||
return `Version ${this.version}`
|
||||
}
|
||||
}
|
||||
|
||||
async getTags () {
|
||||
if (this._tags !== null) {
|
||||
return this._tags.data
|
||||
}
|
||||
|
||||
this._tags = await github.repos.listTags({ owner, repo, per_page: 100 })
|
||||
|
||||
return this._tags.data
|
||||
}
|
||||
|
||||
async exists () {
|
||||
if (this._exists !== null) {
|
||||
return this._exists
|
||||
}
|
||||
const currentTag = this.name
|
||||
const tags = await this.getTags()
|
||||
|
||||
for (const tag of tags) {
|
||||
if (tag.name === currentTag) {
|
||||
this._exists = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
this._exists = false
|
||||
return false
|
||||
}
|
||||
|
||||
async push () {
|
||||
let tagexists = await this.exists()
|
||||
|
||||
if (!tagexists) {
|
||||
// Create tag
|
||||
const newTag = await github.git.createTag({
|
||||
owner,
|
||||
repo,
|
||||
tag: this.name,
|
||||
message: this.message,
|
||||
object: process.env.GITHUB_SHA,
|
||||
type: 'commit'
|
||||
})
|
||||
|
||||
core.warning(`Created new tag: ${newTag.data.tag}`)
|
||||
|
||||
// Create reference
|
||||
let newReference
|
||||
try {
|
||||
newReference = await github.git.createRef({
|
||||
owner,
|
||||
repo,
|
||||
ref: `refs/tags/${newTag.data.tag}`,
|
||||
sha: newTag.data.sha
|
||||
})
|
||||
} catch (e) {
|
||||
core.warning({
|
||||
owner,
|
||||
repo,
|
||||
ref: `refs/tags/${newTag.data.tag}`,
|
||||
sha: newTag.data.sha
|
||||
})
|
||||
|
||||
throw e
|
||||
}
|
||||
|
||||
core.warning(`Reference ${newReference.data.ref} available at ${newReference.data.url}` + os.EOL)
|
||||
|
||||
// Store values for other actions
|
||||
if (typeof newTag === 'object' && typeof newReference === 'object') {
|
||||
core.setOutput('tagname', this.name)
|
||||
core.setOutput('tagsha', newTag.data.sha)
|
||||
core.setOutput('taguri', newReference.data.url)
|
||||
core.setOutput('tagmessage', this.message)
|
||||
core.setOutput('tagref', newReference.data.ref)
|
||||
core.setOutput('tagcreated', 'yes')
|
||||
}
|
||||
} else {
|
||||
core.warning('Cannot push tag (it already exists).')
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user