- split logic to validate and read in additional details for changelog generation into new releaseNotesBuilder

- construct main class to be as simple as possible
This commit is contained in:
Mike Penz
2020-10-19 17:28:54 +02:00
parent 0ceacbaa6a
commit 8b6119add8
6 changed files with 307 additions and 174 deletions
+10 -95
View File
@@ -1,15 +1,7 @@
import * as core from '@actions/core'
import {
failOrError,
retrieveRepositoryPath,
resolveConfiguration
} from './utils'
import {ReleaseNotes} from './releaseNotes'
import {createCommandManager} from './gitHelper'
import {retrieveRepositoryPath, resolveConfiguration} from './utils'
import * as github from '@actions/github'
import {DefaultConfiguration} from './configuration'
import {Octokit} from '@octokit/rest'
import {Tags} from './tags'
import {ReleaseNotesBuilder} from './releaseNotesBuilder'
async function run(): Promise<void> {
core.setOutput('failed', false) // mark the action not failed by default
@@ -32,102 +24,25 @@ async function run(): Promise<void> {
const owner = core.getInput('owner') || github.context.repo.owner
const repo = core.getInput('repo') || github.context.repo.repo
// read in from, to tag inputs
let fromTag = core.getInput('fromTag')
let toTag = core.getInput('toTag')
const fromTag = core.getInput('fromTag')
const toTag = core.getInput('toTag')
// read in flags
const ignorePreReleases = core.getInput('ignorePreReleases') === 'true'
const failOnError = core.getInput('failOnError') === 'true'
// ensure to resolve the toTag if it was not provided
if (!toTag) {
// if not specified try to retrieve tag from github.context.ref
if (github.context.ref.startsWith('refs/tags/')) {
toTag = github.context.ref.replace('refs/tags/', '')
core.info(
`🔖 Resolved current tag (${toTag}) from the 'github.context.ref'`
)
} else {
// if not specified try to retrieve tag from git
const gitHelper = await createCommandManager(repositoryPath)
const latestTag = await gitHelper.latestTag()
toTag = latestTag
core.info(
`🔖 Resolved current tag (${toTag}) from 'git rev-list --tags --skip=0 --max-count=1'`
)
}
}
if (!owner) {
failOrError(`💥 Missing or couldn't resolve 'owner'`, failOnError)
return
} else {
core.setOutput('owner', owner)
core.debug(`Resolved 'owner' as ${owner}`)
}
if (!repo) {
failOrError(`💥 Missing or couldn't resolve 'owner'`, failOnError)
return
} else {
core.setOutput('repo', repo)
core.debug(`Resolved 'repo' as ${repo}`)
}
if (!toTag) {
failOrError(`💥 Missing or couldn't resolve 'toTag'`, failOnError)
return
} else {
core.setOutput('toTag', toTag)
core.debug(`Resolved 'toTag' as ${toTag}`)
}
core.endGroup()
// load octokit instance
const octokit = new Octokit({
auth: `token ${token || process.env.GITHUB_TOKEN}`
})
// ensure to resolve the fromTag if it was not provided specifically
if (!fromTag) {
core.startGroup(`🔖 Resolve previous tag`)
core.debug(`fromTag undefined, trying to resolve via API`)
const tagsApi = new Tags(octokit)
const previousTag = await tagsApi.findPredecessorTag(
owner,
repo,
toTag,
ignorePreReleases,
configuration.max_tags_to_fetch ||
DefaultConfiguration.max_tags_to_fetch
)
if (previousTag == null) {
failOrError(
`💥 Unable to retrieve previous tag given ${toTag}`,
failOnError
)
return
}
fromTag = previousTag.name
core.debug(`fromTag resolved via previousTag as: ${previousTag.name}`)
core.endGroup()
}
const releaseNotes = new ReleaseNotes(octokit, {
const result = await new ReleaseNotesBuilder(
token,
repositoryPath,
owner,
repo,
fromTag,
toTag,
failOnError,
ignorePreReleases,
configuration
})
).build()
core.setOutput(
'changelog',
(await releaseNotes.pull()) ||
configuration.empty_template ||
DefaultConfiguration.empty_template
)
core.setOutput('changelog', result)
} catch (error) {
core.setFailed(error.message)
}