Skip to content

Commit 22cd55f

Browse files
peterbersese
andauthored
cache changelog feed in CI (#25759)
* cache changelog feed in CI * unbreak unit tests * feedbacked Co-authored-by: Robert Sese <rsese@github.com>
1 parent 876b3d4 commit 22cd55f

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,5 @@ jobs:
135135
- name: Run tests
136136
env:
137137
DIFF_FILE: get_diff_files.txt
138+
CHANGELOG_CACHE_FILE_PATH: tests/fixtures/changelog-feed.json
138139
run: npm test -- tests/${{ matrix.test-group }}/

lib/changelog.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from 'path'
44

55
import Parser from 'rss-parser'
66

7+
const CHANGELOG_CACHE_FILE_PATH = process.env.CHANGELOG_CACHE_FILE_PATH
8+
79
async function getRssFeed(url) {
810
const parser = new Parser({ timeout: 5000 })
911
const feedUrl = `${url}/feed`
@@ -19,9 +21,11 @@ async function getRssFeed(url) {
1921
return feed
2022
}
2123

22-
export async function getChangelogItems(prefix, feedUrl) {
23-
const fromCache = getChangelogItemsFromCache(prefix, feedUrl)
24-
if (fromCache) return fromCache
24+
export async function getChangelogItems(prefix, feedUrl, ignoreCache = false) {
25+
if (!ignoreCache) {
26+
const fromCache = getChangelogItemsFromCache(prefix, feedUrl)
27+
if (fromCache) return fromCache
28+
}
2529

2630
const feed = await getRssFeed(feedUrl)
2731

@@ -62,6 +66,9 @@ function getChangelogCacheKey(prefix, feedUrl) {
6266
function getDiskCachePath(prefix, feedUrl) {
6367
// When in local development or in tests, use disk caching
6468
if (process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'development') {
69+
if (CHANGELOG_CACHE_FILE_PATH) {
70+
return CHANGELOG_CACHE_FILE_PATH
71+
}
6572
const cacheKey = getChangelogCacheKey(prefix, feedUrl)
6673
const date = new Date().toISOString().split('T')[0]
6774
const fileName = `changelogcache-${cacheKey}-${date}.json`

tests/fixtures/changelog-feed.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}]

tests/unit/get-rss-feeds.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ describe('getChangelogItems module', () => {
1818

1919
changelog = await getChangelogItems(
2020
'GitHub Actions:',
21-
'https://github.blog/changelog/label/packages'
21+
'https://github.blog/changelog/label/packages',
22+
// This means: Don't use the cache even if it's present.
23+
// The reason we're doing this is because all other tests, the
24+
// cache is prepopulated from a file from the test fixtures. But
25+
// in this particular file, we really do want to execute that code
26+
// that executes on a cache miss. But this particular file special
27+
// because it explicitly uses nock() to mock the HTTP socket.
28+
// So even if we say "Don't use the cache" here, it still won't
29+
// depend on Internet access because we're using `nock` here.
30+
true
2231
)
2332
})
2433

0 commit comments

Comments
 (0)