Skip to content

Commit c852716

Browse files
authored
Merge pull request #12889 from github/repo-sync
repo sync
2 parents 222d28d + 9aae501 commit c852716

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

lib/changelog.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Parser from 'rss-parser'
22

3-
export async function getRssFeed(url) {
3+
async function getRssFeed(url) {
44
const parser = new Parser({ timeout: 5000 })
55
const feedUrl = `${url}/feed`
66
let feed
@@ -15,7 +15,16 @@ export async function getRssFeed(url) {
1515
return feed
1616
}
1717

18-
export async function getChangelogItems(prefix, feed) {
18+
const globalCache = new Map()
19+
20+
export async function getChangelogItems(prefix, feedUrl) {
21+
const cacheKey = prefix + feedUrl
22+
if (globalCache.get(cacheKey)) {
23+
return globalCache.get(cacheKey)
24+
}
25+
26+
const feed = await getRssFeed(feedUrl)
27+
1928
if (!feed || !feed.items) {
2029
console.log(feed)
2130
console.error('feed is not valid or has no items')
@@ -34,7 +43,10 @@ export async function getChangelogItems(prefix, feed) {
3443
}
3544
})
3645

46+
// We don't cache the raw payload we'd get from the network request
47+
// because it would waste memory. Instead we store the "serialized"
48+
// object that's created from the raw payload.
49+
globalCache.set(cacheKey, changelog)
50+
3751
return changelog
3852
}
39-
40-
export default { getRssFeed, getChangelogItems }

middleware/contextualizers/whats-new-changelog.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { getRssFeed, getChangelogItems } from '../../lib/changelog.js'
1+
import { getChangelogItems } from '../../lib/changelog.js'
22
import getApplicableVersions from '../../lib/get-applicable-versions.js'
33

44
export default async function whatsNewChangelog(req, res, next) {
55
if (!req.context.page) return next()
66
if (!req.context.page.changelog) return next()
7-
const label = req.context.page.changelog.label
7+
const label = req.context.page.changelog.label.split(/\s+/g).join('')
88

99
// If there is no `versions` prop in the changelog frontmatter, assume the changelog should display in all versions.
1010
if (req.context.page.changelog.versions) {
@@ -23,7 +23,9 @@ export default async function whatsNewChangelog(req, res, next) {
2323

2424
req.context.changelogUrl = labelUrls[label] || `https://github.blog/changelog/label/${label}`
2525

26-
const feed = await getRssFeed(req.context.changelogUrl)
27-
req.context.whatsNewChangelog = await getChangelogItems(req.context.page.changelog.prefix, feed)
26+
req.context.whatsNewChangelog = await getChangelogItems(
27+
req.context.page.changelog.prefix,
28+
req.context.changelogUrl
29+
)
2830
return next()
2931
}

tests/unit/get-rss-feeds.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
import Parser from 'rss-parser'
2-
import { getChangelogItems } from '../../lib/changelog.js'
31
import fs from 'fs/promises'
42
import path from 'path'
5-
const parser = new Parser({ timeout: 5000 })
6-
const rssFeedContent = await fs.readFile(
7-
path.join(process.cwd(), 'tests/fixtures/rss-feed.xml'),
8-
'utf8'
9-
)
3+
4+
import nock from 'nock'
5+
6+
import { getChangelogItems } from '../../lib/changelog.js'
107

118
describe('getChangelogItems module', () => {
129
let changelog
10+
1311
beforeAll(async () => {
14-
const feed = await parser.parseString(rssFeedContent)
15-
changelog = await getChangelogItems('GitHub Actions:', feed)
12+
const rssFeedContent = await fs.readFile(
13+
path.join(process.cwd(), 'tests/fixtures/rss-feed.xml'),
14+
'utf8'
15+
)
16+
17+
nock('https://github.blog').get('/changelog/label/packages/feed').reply(200, rssFeedContent)
18+
19+
changelog = await getChangelogItems(
20+
'GitHub Actions:',
21+
'https://github.blog/changelog/label/packages'
22+
)
1623
})
1724

25+
afterAll(() => nock.cleanAll())
26+
1827
it('changelog contains 3 items', async () => {
1928
expect(changelog.length).toEqual(3)
2029
})

0 commit comments

Comments
 (0)