Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overall improvements (and fix array placeholders) #1031

Merged
merged 5 commits into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 79 additions & 48 deletions README.md

Large diffs are not rendered by default.

103 changes: 37 additions & 66 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,20 @@ async function run(): Promise<void> {
})
if (configurationJson) {
configJson = parseConfiguration(configurationJson)
if (configJson) {
core.info(`ℹ️ Retreived configuration via 'configurationJson'.`)
}
}
// read in the configuration from the file if possible
const configurationFile: string = core.getInput('configuration')
const configFile = resolveConfiguration(repositoryPath, configurationFile)
if (configFile) {
core.info(`ℹ️ Retreived configuration via 'configuration' (via file).`)
}

if (!configurationFile && !configFile) {
core.info(`ℹ️ No configuration provided. Using Defaults.`)
}

// merge configs, use default values from DefaultConfig on missing definition
const configuration = mergeConfiguration(configJson, configFile)
Expand Down
18 changes: 0 additions & 18 deletions src/pullRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ type PullData = RestEndpointMethodTypes['pulls']['get']['response']['data']

type PullsListData = RestEndpointMethodTypes['pulls']['list']['response']['data']

type PullReviewData = RestEndpointMethodTypes['pulls']['listReviews']['response']['data']

type PullReviewsData = RestEndpointMethodTypes['pulls']['listReviews']['response']['data']

export class PullRequests {
Expand Down Expand Up @@ -143,22 +141,6 @@ export class PullRequests {
return sortPrs(openPrs)
}

async getReviewers(owner: string, repo: string, pr: PullRequestInfo): Promise<void> {
const options = this.octokit.pulls.listReviews.endpoint.merge({
owner,
repo,
pull_number: pr.number
})

for await (const response of this.octokit.paginate.iterator(options)) {
const reviews: PullReviewData = response.data as PullReviewData
pr.approvedReviewers = reviews
.filter(r => r.state === 'APPROVED')
.map(r => r.user?.login)
.filter(r => !!r) as string[]
}
}

async getReviews(owner: string, repo: string, pr: PullRequestInfo): Promise<void> {
const options = this.octokit.pulls.listReviews.endpoint.merge({
owner,
Expand Down
33 changes: 14 additions & 19 deletions src/releaseNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,33 +165,28 @@ export class ReleaseNotes {
})

if (baseBranches.length !== 0) {
core.info(`ℹ️ Retrieved ${mergedPullRequests.length} PRs for ${owner}/${repo} filtered by the 'base_branches' configuration.`)
core.info(`ℹ️ Retrieved ${finalPrs.length} PRs for ${owner}/${repo} filtered by the 'base_branches' configuration.`)
}

if (fetchReviewers) {
core.info(`ℹ️ Fetching reviewers was enabled`)
// update PR information with reviewers who approved
for (const pr of finalPrs) {
await pullRequestsApi.getReviewers(owner, repo, pr)
if (pr.approvedReviewers.length > 0) {
core.info(`ℹ️ Retrieved ${pr.approvedReviewers.length} reviewer(s) for PR ${owner}/${repo}/#${pr.number}`)
}
}
} else {
core.debug(`ℹ️ Fetching reviewers was disabled`)
}

if (fetchReviews) {
core.info(`ℹ️ Fetching reviews was enabled`)
// fetch reviewers only if enabled (requires an additional API request per PR)
if (fetchReviews || fetchReviewers) {
core.info(`ℹ️ Fetching reviews (or reviewers) was enabled`)
// update PR information with reviewers who approved
for (const pr of finalPrs) {
await pullRequestsApi.getReviews(owner, repo, pr)
if ((pr.reviews?.length || 0) > 0) {
core.info(`ℹ️ Retrieved ${pr.reviews?.length || 0} review(s) for PR ${owner}/${repo}/#${pr.number}`)

const reviews = pr.reviews
if (reviews && (reviews?.length || 0) > 0) {
core.info(`ℹ️ Retrieved ${reviews.length || 0} review(s) for PR ${owner}/${repo}/#${pr.number}`)

// backwards compatiblity
pr.approvedReviewers = reviews.filter(r => r.state === 'APPROVED').map(r => r.author)
} else {
core.debug(`No reviewer(s) for PR ${owner}/${repo}/#${pr.number}`)
}
}
} else {
core.debug(`ℹ️ Fetching reviews was disabled`)
core.debug(`ℹ️ Fetching reviews (or reviewers) was disabled`)
}

return [diffInfo, finalPrs]
Expand Down
10 changes: 5 additions & 5 deletions src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function replaceEmptyTemplate(template: string, options: ReleaseNotesOpti
}
const placeholderMap = new Map<string, string>()
fillAdditionalPlaceholders(options, placeholderMap)
return replacePlaceholders(template, new Map<string, string>(), placeholderMap, placeholders, undefined, options.configuration)
return replacePlaceholders(template, EMPTY_MAP, placeholderMap, placeholders, undefined, options.configuration)
}

function fillAdditionalPlaceholders(
Expand Down Expand Up @@ -394,9 +394,9 @@ function fillArrayPlaceholders(
values: string[]
): void {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${key}[${i}]}}`, values[i])
placeholderMap.set(`${key}[${i}]`, values[i])
}
placeholderMap.set(`\${{${key}[*]}}`, values.join(', '))
placeholderMap.set(`${key}[*]`, values.join(', '))
}

function fillReviewPlaceholders(
Expand All @@ -407,10 +407,10 @@ function fillReviewPlaceholders(
// retrieve the keys from the CommentInfo object
for (const childKey of Object.keys(EMPTY_COMMENT_INFO)) {
for (let i = 0; i < values.length; i++) {
placeholderMap.set(`\${{${parentKey}[${i}].${childKey}}}`, values[i][childKey as keyof CommentInfo]?.toLocaleString('en') || '')
placeholderMap.set(`${parentKey}[${i}].${childKey}`, values[i][childKey as keyof CommentInfo]?.toLocaleString('en') || '')
}
placeholderMap.set(
`\${{${parentKey}[*].${childKey}}}`,
`${parentKey}[*].${childKey}`,
values.map(value => value[childKey as keyof CommentInfo]?.toLocaleString('en') || '').join(', ')
)
}
Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@ export function failOrError(message: string | Error, failOnError: boolean): void
/**
* Retrieves the configuration given the file path, if not found it will fallback to the `DefaultConfiguration`
*/
export function resolveConfiguration(githubWorkspacePath: string, configurationFile: string): Configuration {
let configuration = DefaultConfiguration
export function resolveConfiguration(githubWorkspacePath: string, configurationFile: string): Configuration | undefined {
if (configurationFile) {
const configurationPath = path.resolve(githubWorkspacePath, configurationFile)
core.debug(`configurationPath = '${configurationPath}'`)
const providedConfiguration = readConfiguration(configurationPath)
if (providedConfiguration) {
configuration = providedConfiguration
const configuration = providedConfiguration
core.info(`ℹ️ Configuration successfully loaded.`)
if (core.isDebug()) {
core.debug(`configuration = ${JSON.stringify(configuration)}`)
}
return configuration
} else {
core.debug(`Configuration file could not be read.`)
}
} else {
core.info(`ℹ️ Configuration not provided. Using Defaults.`)
core.debug(`Configuration file not provided.`)
}
return configuration
return undefined
}

/**
Expand Down