-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
83 lines (66 loc) · 2.91 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const github = require('@actions/github')
const core = require('@actions/core')
const path = require('path')
const fs = require('fs')
async function run() {
try {
let fileName = core.getInput('source_file')
let filePath = path.join(process.env.GITHUB_WORKSPACE, fileName)
if (!fs.existsSync(filePath))
return core.setFailed(`Source file ${fileName} does not exist.`)
let content = fs.readFileSync(filePath)
let regex = new RegExp(core.getInput('extraction_regex'))
let matches = String(content).match(regex)
if (!matches)
return core.setFailed(`No match was found for the regex '${regex.toString()}'.`)
let version = matches[matches.length - 1]
let format = core.getInput('tag_format', { required: false }).trim()
let message = core.getInput('tag_message', { required: false }).trim()
let name = format.replace('{version}', version)
core.setOutput('version', version)
core.setOutput('tagname', name)
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN') || process.env.INPUT_GITHUB_TOKEN.trim().length === 0)
return core.setFailed('Invalid or missing GITHUB_TOKEN.')
let git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN)
let repoID = process.env.GITHUB_REPOSITORY.split('/')
let owner = repoID[0], repo = repoID[1]
let tags
try {
tags = await git.repos.listTags({owner, repo, per_page: 100})
} catch (e) {
core.warning('No tags were listed')
}
if (tags) {
for (let tag of tags.data)
if (tag.name.trim().toLowerCase() === name.trim().toLowerCase())
return core.warning(`"${tag.name.trim()}" tag already exists.`)
if (message.length === 0 && tags.data.length > 0) {
let latest = tags.data.shift()
let changelog = await git.repos.compareCommits({owner, repo, base: latest.name, head: 'master'})
message = '\n'
for (let commit of changelog.data.commits) {
if (commit) {
message += `\n* ${commit.commit.message}`
if (commit.author && commit.author.login)
message += ` (${commit.author.login })`
}
}
message = message.trim()
}
}
core.debug('Making tag...')
let tag = await git.git.createTag({owner, repo, tag: name, message: message.length > 0 ? message : 'Initial tag', object: process.env.GITHUB_SHA, type: 'commit'})
core.warning(`Created tag ${tag.data.sha}`)
core.debug('Making reference...')
let reference = await git.git.createRef({owner, repo, ref: `refs/tags/${tag.data.tag}`, sha: tag.data.sha})
core.warning(`Reference ${reference.data.ref} available at ${reference.data.url}`)
if (typeof tag === 'object' && typeof reference === 'object') {
core.setOutput('tagsha', tag.data.sha)
core.setOutput('taguri', reference.data.url)
core.setOutput('tagmessage', message)
}
} catch (error) {
core.warning(error.message)
}
}
run()