feat(cache): extend release notes cache TTL (#7596)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Matt R. Wilson 2020-10-29 03:11:12 -06:00 committed by GitHub
parent 6d3577a45d
commit c9727c024a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 16 deletions

View file

@ -1,4 +1,5 @@
import fs from 'fs-extra';
import { DateTime } from 'luxon';
import * as httpMock from '../../../../test/httpMock';
import { getName } from '../../../../test/util';
import { ChangeLogNotes } from './common';
@ -7,6 +8,7 @@ import {
getReleaseList,
getReleaseNotes,
getReleaseNotesMd,
releaseNotesCacheMinutes,
} from './release-notes';
const angularJsChangelogMd = fs.readFileSync(
@ -61,6 +63,24 @@ describe(getName(__filename), () => {
httpMock.reset();
});
describe('releaseNotesCacheMinutes', () => {
const now = DateTime.local();
it.each([
[now, 55],
[now.minus({ week: 2 }), 1435],
[now.minus({ year: 1 }), 14495],
])('works with string date (%s, %i)', (date, minutes) => {
expect(releaseNotesCacheMinutes(date?.toISO())).toEqual(minutes);
});
it('handles date object', () => {
expect(releaseNotesCacheMinutes(new Date())).toEqual(55);
});
it.each([null, undefined, 'fake', 123])('handles invalid: %s', (date) => {
expect(releaseNotesCacheMinutes(date as never)).toEqual(55);
});
});
describe('addReleaseNotes()', () => {
it('returns input if invalid', async () => {
const input = { a: 1 };

View file

@ -1,5 +1,7 @@
import * as URL from 'url';
import is from '@sindresorhus/is';
import { linkify } from 'linkify-markdown';
import { DateTime } from 'luxon';
import MarkdownIt from 'markdown-it';
import { logger } from '../../../logger';
@ -260,6 +262,31 @@ export async function getReleaseNotesMd(
return null;
}
/**
* Determine how long to cache release notes based on when the version was released.
*
* It's not uncommon for release notes to be updated shortly after the release itself,
* so only cache for about an hour when the release is less than a week old. Otherwise,
* cache for days.
*/
export function releaseNotesCacheMinutes(releaseDate?: string | Date): number {
const dt = is.date(releaseDate)
? DateTime.fromJSDate(releaseDate)
: DateTime.fromISO(releaseDate);
const now = DateTime.local();
if (!dt.isValid || now.diff(dt, 'days').days < 7) {
return 55;
}
if (now.diff(dt, 'months').months < 6) {
return 1435; // 5 minutes shy of one day
}
return 14495; // 5 minutes shy of 10 days
}
export async function addReleaseNotes(
input: ChangeLogResult
): Promise<ChangeLogResult> {
@ -285,22 +312,15 @@ export async function addReleaseNotes(
let releaseNotes: ChangeLogNotes;
const cacheKey = getCacheKey(v.version);
releaseNotes = await packageCache.get(cacheNamespace, cacheKey);
// istanbul ignore else: no cache tests
if (!releaseNotes) {
if (input.project.github != null) {
releaseNotes = await getReleaseNotesMd(
repository,
v.version,
input.project.baseUrl,
input.project.apiBaseUrl
);
} else {
releaseNotes = await getReleaseNotesMd(
repository,
v.version,
input.project.baseUrl,
input.project.apiBaseUrl
);
}
// istanbul ignore else: should be tested
if (!releaseNotes) {
releaseNotes = await getReleaseNotes(
repository,
@ -314,7 +334,7 @@ export async function addReleaseNotes(
if (!releaseNotes && v.compare.url) {
releaseNotes = { url: v.compare.url };
}
const cacheMinutes = 55;
const cacheMinutes = releaseNotesCacheMinutes(v.date);
await packageCache.set(
cacheNamespace,
cacheKey,