cache changelog feed in CI (#25759)
* cache changelog feed in CI * unbreak unit tests * feedbacked Co-authored-by: Robert Sese <rsese@github.com>
This commit is contained in:
1
.github/workflows/test.yml
vendored
1
.github/workflows/test.yml
vendored
@@ -135,4 +135,5 @@ jobs:
|
||||
- name: Run tests
|
||||
env:
|
||||
DIFF_FILE: get_diff_files.txt
|
||||
CHANGELOG_CACHE_FILE_PATH: tests/fixtures/changelog-feed.json
|
||||
run: npm test -- tests/${{ matrix.test-group }}/
|
||||
|
||||
@@ -4,6 +4,8 @@ import path from 'path'
|
||||
|
||||
import Parser from 'rss-parser'
|
||||
|
||||
const CHANGELOG_CACHE_FILE_PATH = process.env.CHANGELOG_CACHE_FILE_PATH
|
||||
|
||||
async function getRssFeed(url) {
|
||||
const parser = new Parser({ timeout: 5000 })
|
||||
const feedUrl = `${url}/feed`
|
||||
@@ -19,9 +21,11 @@ async function getRssFeed(url) {
|
||||
return feed
|
||||
}
|
||||
|
||||
export async function getChangelogItems(prefix, feedUrl) {
|
||||
export async function getChangelogItems(prefix, feedUrl, ignoreCache = false) {
|
||||
if (!ignoreCache) {
|
||||
const fromCache = getChangelogItemsFromCache(prefix, feedUrl)
|
||||
if (fromCache) return fromCache
|
||||
}
|
||||
|
||||
const feed = await getRssFeed(feedUrl)
|
||||
|
||||
@@ -62,6 +66,9 @@ function getChangelogCacheKey(prefix, feedUrl) {
|
||||
function getDiskCachePath(prefix, feedUrl) {
|
||||
// When in local development or in tests, use disk caching
|
||||
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
|
||||
if (CHANGELOG_CACHE_FILE_PATH) {
|
||||
return CHANGELOG_CACHE_FILE_PATH
|
||||
}
|
||||
const cacheKey = getChangelogCacheKey(prefix, feedUrl)
|
||||
const date = new Date().toISOString().split('T')[0]
|
||||
const fileName = `changelogcache-${cacheKey}-${date}.json`
|
||||
|
||||
1
tests/fixtures/changelog-feed.json
vendored
Normal file
1
tests/fixtures/changelog-feed.json
vendored
Normal file
@@ -0,0 +1 @@
|
||||
[{"title":"Authentication token format updates are generally available","date":"2021-03-31T22:22:03.000Z","href":"https://github.blog/changelog/2021-03-31-authentication-token-format-updates-are-generally-available"},{"title":"Compare REST API now supports pagination","date":"2021-03-23T02:49:54.000Z","href":"https://github.blog/changelog/2021-03-22-compare-rest-api-now-supports-pagination"},{"title":"GitHub Discussions GraphQL API public beta","date":"2021-02-23T18:21:40.000Z","href":"https://github.blog/changelog/2021-02-23-github-discussions-graphql-api-public-beta"}]
|
||||
@@ -18,7 +18,16 @@ describe('getChangelogItems module', () => {
|
||||
|
||||
changelog = await getChangelogItems(
|
||||
'GitHub Actions:',
|
||||
'https://github.blog/changelog/label/packages'
|
||||
'https://github.blog/changelog/label/packages',
|
||||
// This means: Don't use the cache even if it's present.
|
||||
// The reason we're doing this is because all other tests, the
|
||||
// cache is prepopulated from a file from the test fixtures. But
|
||||
// in this particular file, we really do want to execute that code
|
||||
// that executes on a cache miss. But this particular file special
|
||||
// because it explicitly uses nock() to mock the HTTP socket.
|
||||
// So even if we say "Don't use the cache" here, it still won't
|
||||
// depend on Internet access because we're using `nock` here.
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user