- expand configuration specification by offering sorting by alternative properties -> title instead of mergedAt.
- add more testcases to cover the new sort orders - FIX https://github.com/mikepenz/release-changelog-builder-action/issues/757
This commit is contained in:
+11
-2
@@ -3,7 +3,7 @@ export interface Configuration {
|
||||
max_pull_requests: number
|
||||
max_back_track_time_days: number
|
||||
exclude_merge_branches: string[]
|
||||
sort: string // "ASC" or "DESC"
|
||||
sort: Sort | string // "ASC" or "DESC"
|
||||
template: string
|
||||
pr_template: string
|
||||
empty_template: string
|
||||
@@ -23,6 +23,11 @@ export interface Category {
|
||||
exhaustive?: boolean // requires all labels to be present in the PR
|
||||
}
|
||||
|
||||
export interface Sort {
|
||||
order: 'ASC' | 'DESC' // the sorting order
|
||||
on_property: 'mergedAt' | 'title' // the property to sort on. (mergedAt falls back to createdAt)
|
||||
}
|
||||
|
||||
export interface Regex {
|
||||
pattern: string // the regex pattern to match
|
||||
flags?: string // the regex flag to use for RegExp
|
||||
@@ -55,7 +60,11 @@ export const DefaultConfiguration: Configuration = {
|
||||
max_pull_requests: 200, // the amount of pull requests to process
|
||||
max_back_track_time_days: 365, // allow max of 365 days back to check up on pull requests
|
||||
exclude_merge_branches: [], // branches to exclude from counting as PRs (e.g. YourOrg/qa, YourOrg/main)
|
||||
sort: 'ASC', // sorting order for filling the changelog (ASC or DESC) supported
|
||||
sort: {
|
||||
// defines the sorting logic for PRs
|
||||
order: 'ASC', // the sorting order
|
||||
on_property: 'mergedAt' // the property to sort on. (mergedAt falls back to createdAt)
|
||||
},
|
||||
template: '${{CHANGELOG}}', // the global template to host the changelog
|
||||
pr_template: '- ${{TITLE}}\n - PR: #${{NUMBER}}', // the per PR template to pick
|
||||
empty_template: '- no changes', // the template to use if no pull requests are found
|
||||
|
||||
+47
-22
@@ -2,6 +2,7 @@ import * as core from '@actions/core'
|
||||
import {Octokit, RestEndpointMethodTypes} from '@octokit/rest'
|
||||
import {Unpacked} from './utils'
|
||||
import moment from 'moment'
|
||||
import {Sort} from './configuration'
|
||||
|
||||
export interface PullRequestInfo {
|
||||
number: number
|
||||
@@ -89,11 +90,11 @@ export class PullRequests {
|
||||
}
|
||||
|
||||
// bail out early to not keep iterating on PRs super old
|
||||
return sortPullRequests(mergedPRs, true)
|
||||
return sortPrs(mergedPRs)
|
||||
}
|
||||
}
|
||||
|
||||
return sortPullRequests(mergedPRs, true)
|
||||
return sortPrs(mergedPRs)
|
||||
}
|
||||
|
||||
async getOpen(
|
||||
@@ -125,11 +126,11 @@ export class PullRequests {
|
||||
}
|
||||
|
||||
// bail out early to not keep iterating on PRs super old
|
||||
return sortPullRequests(openPrs, true)
|
||||
return sortPrs(openPrs)
|
||||
}
|
||||
}
|
||||
|
||||
return sortPullRequests(openPrs, true)
|
||||
return sortPrs(openPrs)
|
||||
}
|
||||
|
||||
async getReviewers(
|
||||
@@ -155,36 +156,60 @@ export class PullRequests {
|
||||
}
|
||||
}
|
||||
|
||||
function sortPrs(pullRequests: PullRequestInfo[]): PullRequestInfo[] {
|
||||
return sortPullRequests(pullRequests, {
|
||||
order: 'ASC',
|
||||
on_property: 'mergedAt'
|
||||
})
|
||||
}
|
||||
|
||||
export function sortPullRequests(
|
||||
pullRequests: PullRequestInfo[],
|
||||
ascending: Boolean
|
||||
sort: Sort | string
|
||||
): PullRequestInfo[] {
|
||||
if (ascending) {
|
||||
let sortConfig: Sort
|
||||
|
||||
// legacy handling to support string sort config
|
||||
if (typeof sort === 'string') {
|
||||
let order: 'ASC' | 'DESC' = 'ASC'
|
||||
if (sort.toUpperCase() === 'DESC') order = 'DESC'
|
||||
sortConfig = {order, on_property: 'mergedAt'}
|
||||
} else {
|
||||
sortConfig = sort
|
||||
}
|
||||
|
||||
if (sortConfig.order === 'ASC') {
|
||||
pullRequests.sort((a, b) => {
|
||||
const aa = a.mergedAt || a.createdAt
|
||||
const bb = b.mergedAt || b.createdAt
|
||||
if (aa.isBefore(bb)) {
|
||||
return -1
|
||||
} else if (bb.isBefore(aa)) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
return compare(a, b, sortConfig)
|
||||
})
|
||||
} else {
|
||||
pullRequests.sort((b, a) => {
|
||||
const aa = a.mergedAt || a.createdAt
|
||||
const bb = b.mergedAt || b.createdAt
|
||||
if (aa.isBefore(bb)) {
|
||||
return -1
|
||||
} else if (bb.isBefore(aa)) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
return compare(a, b, sortConfig)
|
||||
})
|
||||
}
|
||||
return pullRequests
|
||||
}
|
||||
|
||||
export function compare(
|
||||
a: PullRequestInfo,
|
||||
b: PullRequestInfo,
|
||||
sort: Sort
|
||||
): number {
|
||||
if (sort.on_property === 'mergedAt') {
|
||||
const aa = a.mergedAt || a.createdAt
|
||||
const bb = b.mergedAt || b.createdAt
|
||||
if (aa.isBefore(bb)) {
|
||||
return -1
|
||||
} else if (bb.isBefore(aa)) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
} else {
|
||||
// only else for now `label`
|
||||
return a.title.localeCompare(b.title)
|
||||
}
|
||||
}
|
||||
|
||||
// helper function to add a special open label to prs not merged.
|
||||
function attachSpeciaLabels(
|
||||
status: 'open' | 'merged',
|
||||
|
||||
+2
-3
@@ -15,8 +15,7 @@ export function buildChangelog(
|
||||
// sort to target order
|
||||
const config = options.configuration
|
||||
const sort = config.sort || DefaultConfiguration.sort
|
||||
const sortAsc = sort.toUpperCase() === 'ASC'
|
||||
prs = sortPullRequests(prs, sortAsc)
|
||||
prs = sortPullRequests(prs, sort)
|
||||
core.info(`ℹ️ Sorted all pull requests ascending: ${sort}`)
|
||||
|
||||
// drop duplicate pull requests
|
||||
@@ -44,7 +43,7 @@ export function buildChangelog(
|
||||
core.info(
|
||||
`ℹ️ Removed ${removedElements} pull requests during deduplication`
|
||||
)
|
||||
prs = sortPullRequests(deduplicatedPRs, sortAsc) // resort deduplicatedPRs
|
||||
prs = sortPullRequests(deduplicatedPRs, sort) // resort deduplicatedPRs
|
||||
} else {
|
||||
core.warning(`⚠️ Configured \`duplicate_filter\` invalid.`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user