- refactor the caching logic given the GitHub action limit on arguments is too small to allow passing in the whole cache

- adjust the `cache` to now only take the file-path to read the cache from
  - offer temporary backwards compatiblity on passing the cache directly
  - add documentation for cache vars in README
This commit is contained in:
Mike Penz
2023-08-25 08:23:54 +00:00
committed by GitHub
parent fa2cca75cf
commit a72c0bbc47
8 changed files with 152 additions and 30 deletions
+2
View File
@@ -57,6 +57,7 @@ async function run(): Promise<void> {
const commitMode = core.getInput('commitMode') === 'true'
const exportCache = core.getInput('exportCache') === 'true'
const exportOnly = core.getInput('exportOnly') === 'true'
const cache = core.getInput('cache')
const result = await new ReleaseNotesBuilder(
baseUrl,
@@ -76,6 +77,7 @@ async function run(): Promise<void> {
commitMode,
exportCache,
exportOnly,
cache,
configuration
).build()
+13 -5
View File
@@ -1,12 +1,13 @@
import * as core from '@actions/core'
import {Configuration} from './configuration'
import {checkExportedData} from './utils'
import {checkExportedData, writeCacheData} from './utils'
import {PullRequestData, buildChangelog} from './transform'
import {PullRequestCollector} from './pr-collector/prCollector'
import {failOrError} from './pr-collector/utils'
import {TagInfo} from './pr-collector/tags'
import {DiffInfo} from './pr-collector/commits'
import {PullRequestInfo} from './pr-collector/pullRequests'
import * as fs from 'fs'
export interface ReleaseNotesOptions {
owner: string // the owner of the repository
@@ -47,11 +48,18 @@ export class ReleaseNotesBuilder {
private commitMode = false,
private exportCache = false,
private exportOnly = false,
private cache: string | null = null,
private configuration: Configuration
) {}
async build(): Promise<string | null> {
const releaseNotesData = checkExportedData()
let releaseNotesData: Data | null
try {
releaseNotesData = checkExportedData(this.exportCache, this.cache)
} catch (error) {
failOrError(`${error}`, this.failOnError)
return null
}
if (releaseNotesData == null) {
if (!this.owner) {
failOrError(`💥 Missing or couldn't resolve 'owner'`, this.failOnError)
@@ -109,13 +117,13 @@ export class ReleaseNotesBuilder {
this.setOutputs(options, diffInfo, mergedPullRequests)
if (this.exportCache) {
const cache = {
const cacheData = {
mergedPullRequests,
diffInfo,
options
}
core.setOutput(`cache`, JSON.stringify(cache))
//fs.writeFileSync(path.resolve('cache.json'), JSON.stringify(cache))
writeCacheData(cacheData, this.cache)
if (this.exportOnly) {
core.info(`️ Enabled 'exportOnly' will not generate changelog`)
+43 -4
View File
@@ -6,6 +6,7 @@ import moment from 'moment'
import {DiffInfo} from './pr-collector/commits'
import {PullRequestInfo} from './pr-collector/pullRequests'
import {Data, ReleaseNotesOptions} from './releaseNotesBuilder'
import {env} from 'process'
/**
* Resolves the repository path, relatively to the GITHUB_WORKSPACE
*/
@@ -23,15 +24,53 @@ export function retrieveRepositoryPath(providedPath: string): string {
return repositoryPath
}
export function writeCacheData(data: Data, cacheOutput: string | null): void {
let cacheFile: string
if (cacheOutput && !cacheOutput.startsWith('{')) {
// legacy handling, originally we allowed cache as direct string.
// However, this can result in a "Argument list too long" exception for very long caches
cacheFile = cacheOutput
} else {
if (env.RUNNER_TEMP) {
fs.mkdirSync(env.RUNNER_TEMP)
}
cacheFile = `${env.RUNNER_TEMP}/rcba-cache.json`
core.debug(`Defined cacheFile as ${cacheFile}`)
}
try {
fs.writeFileSync(cacheFile, JSON.stringify(data))
core.setOutput(`cache`, cacheFile)
} catch (error) {
core.warning(`Failed to write cache file. (${error})`)
}
}
/**
* Retrieves the exported information from a previous run of the `release-changelog-builder-action`.
* If available, return a [ReleaseNotesData].
*/
export function checkExportedData(): Data | null {
const rawCache = core.getInput(`cache`)
export function checkExportedData(exportCache: boolean, cacheInput: string | null): Data | null {
if (exportCache) {
return null
}
if (cacheInput) {
// legacy handling, originally we allowed cache as direct string.
// However, this can result in a "Argument list too long" exception for very long caches
const legacyJsonCache = cacheInput.startsWith('{')
let cache: Data
if (legacyJsonCache) {
cache = JSON.parse(cacheInput)
} else {
if (!fs.existsSync(cacheInput)) {
throw new Error(`💥 The provided cache file does not exist`)
} else {
cache = JSON.parse(fs.readFileSync(cacheInput, 'utf8'))
}
}
if (rawCache) {
const cache: Data = JSON.parse(rawCache)
const diffInfo: DiffInfo = cache.diffInfo
const mergedPullRequests: PullRequestInfo[] = cache.mergedPullRequests