- 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:
+10
-95
@@ -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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import {Configuration, DefaultConfiguration} from './configuration'
|
||||
import * as github from '@actions/github'
|
||||
import * as core from '@actions/core'
|
||||
import {createCommandManager} from './gitHelper'
|
||||
import {failOrError} from './utils'
|
||||
import {Octokit} from '@octokit/rest'
|
||||
import {Tags} from './tags'
|
||||
import {ReleaseNotes} from './releaseNotes'
|
||||
|
||||
export class ReleaseNotesBuilder {
|
||||
constructor(
|
||||
private token: string | null,
|
||||
private repositoryPath: string,
|
||||
private owner: string | null,
|
||||
private repo: string | null,
|
||||
private fromTag: string | null,
|
||||
private toTag: string | null,
|
||||
private failOnError: boolean,
|
||||
private ignorePreReleases: boolean,
|
||||
private configuration: Configuration
|
||||
) {}
|
||||
|
||||
async build(): Promise<string | null> {
|
||||
// ensure to resolve the toTag if it was not provided
|
||||
if (!this.toTag) {
|
||||
// if not specified try to retrieve tag from github.context.ref
|
||||
if (github.context.ref.startsWith('refs/tags/')) {
|
||||
this.toTag = github.context.ref.replace('refs/tags/', '')
|
||||
core.info(
|
||||
`🔖 Resolved current tag (${this.toTag}) from the 'github.context.ref'`
|
||||
)
|
||||
} else {
|
||||
// if not specified try to retrieve tag from git
|
||||
const gitHelper = await createCommandManager(this.repositoryPath)
|
||||
const latestTag = await gitHelper.latestTag()
|
||||
this.toTag = latestTag
|
||||
core.info(
|
||||
`🔖 Resolved current tag (${this.toTag}) from 'git rev-list --tags --skip=0 --max-count=1'`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.owner) {
|
||||
failOrError(`💥 Missing or couldn't resolve 'owner'`, this.failOnError)
|
||||
return null
|
||||
} else {
|
||||
core.setOutput('owner', this.owner)
|
||||
core.debug(`Resolved 'owner' as ${this.owner}`)
|
||||
}
|
||||
|
||||
if (!this.repo) {
|
||||
failOrError(`💥 Missing or couldn't resolve 'owner'`, this.failOnError)
|
||||
return null
|
||||
} else {
|
||||
core.setOutput('repo', this.repo)
|
||||
core.debug(`Resolved 'repo' as ${this.repo}`)
|
||||
}
|
||||
|
||||
if (!this.toTag) {
|
||||
failOrError(`💥 Missing or couldn't resolve 'toTag'`, this.failOnError)
|
||||
return null
|
||||
} else {
|
||||
core.setOutput('toTag', this.toTag)
|
||||
core.debug(`Resolved 'toTag' as ${this.toTag}`)
|
||||
}
|
||||
core.endGroup()
|
||||
|
||||
// load octokit instance
|
||||
const octokit = new Octokit({
|
||||
auth: `token ${this.token || process.env.GITHUB_TOKEN}`
|
||||
})
|
||||
|
||||
// ensure to resolve the fromTag if it was not provided specifically
|
||||
if (!this.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(
|
||||
this.owner,
|
||||
this.repo,
|
||||
this.toTag,
|
||||
this.ignorePreReleases,
|
||||
this.configuration.max_tags_to_fetch ||
|
||||
DefaultConfiguration.max_tags_to_fetch
|
||||
)
|
||||
if (previousTag == null) {
|
||||
failOrError(
|
||||
`💥 Unable to retrieve previous tag given ${this.toTag}`,
|
||||
this.failOnError
|
||||
)
|
||||
return null
|
||||
}
|
||||
this.fromTag = previousTag.name
|
||||
core.debug(`fromTag resolved via previousTag as: ${previousTag.name}`)
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
const releaseNotes = new ReleaseNotes(octokit, {
|
||||
owner: this.owner,
|
||||
repo: this.repo,
|
||||
fromTag: this.fromTag,
|
||||
toTag: this.toTag,
|
||||
failOnError: this.failOnError,
|
||||
configuration: this.configuration
|
||||
})
|
||||
|
||||
return (
|
||||
(await releaseNotes.pull()) ||
|
||||
this.configuration.empty_template ||
|
||||
DefaultConfiguration.empty_template
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user