- introduce proper approach to retrieve tag before a given tag
- add configuration options for - path - configuration - fromTag, toTag - token - allow to specify transformers to adjust information to a specific form - allow to specify different templates - speed up by limiting information to pull - add logic to automatically resolve current - use github actions logger
This commit is contained in:
+34
-45
@@ -1,38 +1,22 @@
|
||||
import { Octokit } from "@octokit/rest"
|
||||
|
||||
import { Commits } from "./commits"
|
||||
import * as formatters from "./formatters"
|
||||
import { Logger } from "./logger"
|
||||
import { PullRequestInfo, PullRequests } from "./pullRequests"
|
||||
import { buildChangelog } from './transform';
|
||||
import * as core from '@actions/core';
|
||||
import { Tags } from './tags';
|
||||
|
||||
export interface ReleaseNotesOptions {
|
||||
owner: string
|
||||
repo: string
|
||||
fromTag: string
|
||||
fromTag: string | null
|
||||
toTag: string
|
||||
formatter: {
|
||||
pullRequestTitle: (pullRequest?: PullRequestInfo) => string
|
||||
pullRequestNotable: (pullRequest?: PullRequestInfo) => string
|
||||
notableChanges: (notableChanges?: string) => string
|
||||
allChanges: (allChanges?: string) => string
|
||||
}
|
||||
configuration: Configuration
|
||||
}
|
||||
|
||||
export class ReleaseNotes {
|
||||
static get defaultFormatter() {
|
||||
return {
|
||||
pullRequestTitle: formatters.defaultPullRequestTitleFormatter,
|
||||
pullRequestNotable: formatters.defaultPullRequestNotableFormatter,
|
||||
notableChanges: formatters.defaultNotableChangesFormatter,
|
||||
allChanges: formatters.defaultAllChangesFormatter
|
||||
}
|
||||
}
|
||||
|
||||
constructor(private options: ReleaseNotesOptions) {
|
||||
options.formatter = {
|
||||
...ReleaseNotes.defaultFormatter,
|
||||
...options.formatter
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async pull(token?: string): Promise<string> {
|
||||
@@ -40,20 +24,36 @@ export class ReleaseNotes {
|
||||
auth: `token ${token || process.env.GITHUB_TOKEN}`
|
||||
})
|
||||
|
||||
const mergedPullRequests = await this.getMergedPullRequests(octokit)
|
||||
const notableChanges = this.getFormatedChanges(mergedPullRequests, this.options.formatter.pullRequestNotable)
|
||||
const allChanges = this.getFormatedChanges(mergedPullRequests, this.options.formatter.pullRequestTitle)
|
||||
const format = this.options.formatter
|
||||
const { owner, repo, fromTag, toTag, configuration } = this.options
|
||||
|
||||
return `${format.notableChanges(notableChanges)}${format.allChanges(allChanges)}`
|
||||
if(fromTag == null) {
|
||||
const tagsApi = new Tags(octokit)
|
||||
|
||||
const previousTag = await tagsApi.findPredecessorTag(owner, repo, toTag)
|
||||
if(previousTag == null) {
|
||||
core.error(`Unable to retrieve previous tag given ${toTag}`)
|
||||
return configuration.empty_template ? configuration.empty_template : DefaultConfiguration.empty_template
|
||||
}
|
||||
|
||||
this.options.fromTag = previousTag.name
|
||||
}
|
||||
|
||||
const mergedPullRequests = await this.getMergedPullRequests(octokit)
|
||||
|
||||
if (mergedPullRequests.length == 0) {
|
||||
core.warning(`No pull requests found for between ${fromTag}...${toTag}`)
|
||||
return configuration.empty_template ? configuration.empty_template : DefaultConfiguration.empty_template
|
||||
}
|
||||
|
||||
return buildChangelog(mergedPullRequests, configuration)
|
||||
}
|
||||
|
||||
private async getMergedPullRequests(octokit: Octokit): Promise<PullRequestInfo[]> {
|
||||
const { owner, repo, fromTag, toTag } = this.options
|
||||
Logger.log("Comparing", `${owner}/${repo}`, `${fromTag}...${toTag}`)
|
||||
core.info(`Comparing ${owner}/${repo} ${fromTag}...${toTag}`)
|
||||
|
||||
const commitsApi = new Commits(octokit)
|
||||
const commits = await commitsApi.getDiff(owner, repo, fromTag, toTag)
|
||||
const commits = await commitsApi.getDiff(owner, repo, fromTag!!, toTag)
|
||||
|
||||
if (commits.length === 0) {
|
||||
return []
|
||||
@@ -64,12 +64,12 @@ export class ReleaseNotes {
|
||||
const fromDate = firstCommit.date
|
||||
const toDate = lastCommit.date
|
||||
|
||||
Logger.log(`Fetching PRs between dates ${fromDate.toISOString()} ${toDate.toISOString()} for ${owner}/${repo}`)
|
||||
core.info(`Fetching PRs between dates ${fromDate.toISOString()} ${toDate.toISOString()} for ${owner}/${repo}`)
|
||||
|
||||
const pullRequestsApi = new PullRequests(octokit)
|
||||
const pullRequests = await pullRequestsApi.getBetweenDates(owner, repo, fromDate, toDate)
|
||||
|
||||
Logger.log(`Found ${pullRequests.length} merged PRs for ${owner}/${repo}`)
|
||||
core.info(`Found ${pullRequests.length} merged PRs for ${owner}/${repo}`)
|
||||
|
||||
const prCommits = pullRequestsApi.filterCommits(commits)
|
||||
const filteredPullRequests = []
|
||||
@@ -89,30 +89,19 @@ export class ReleaseNotes {
|
||||
if (pullRequestsByNumber[commit.prNumber]) {
|
||||
filteredPullRequests.push(pullRequestsByNumber[commit.prNumber])
|
||||
} else if (fromDate.toISOString() === toDate.toISOString()) {
|
||||
Logger.log(`${prRef} not in date range, fetching explicitly`)
|
||||
core.info(`${prRef} not in date range, fetching explicitly`)
|
||||
const pullRequest = await pullRequestsApi.getSingle(owner, repo, commit.prNumber)
|
||||
|
||||
if (pullRequest) {
|
||||
filteredPullRequests.push(pullRequest)
|
||||
} else {
|
||||
Logger.warn(`${prRef} not found! Commit text: ${commit.summary}`)
|
||||
core.warning(`${prRef} not found! Commit text: ${commit.summary}`)
|
||||
}
|
||||
} else {
|
||||
Logger.log(`${prRef} not in date range, likely a merge commit from a fork-to-fork PR`)
|
||||
core.info(`${prRef} not in date range, likely a merge commit from a fork-to-fork PR`)
|
||||
}
|
||||
}
|
||||
|
||||
return filteredPullRequests
|
||||
}
|
||||
|
||||
private getFormatedChanges(pullRequests: PullRequestInfo[], formatter: (pr?: PullRequestInfo) => string): string {
|
||||
if (pullRequests.length) {
|
||||
return pullRequests.reduce((result, pr) => {
|
||||
let formated = formatter(pr)
|
||||
formated = !!formated ? `${formated}\n` : ""
|
||||
return `${result}${formated}`
|
||||
}, "")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user