Skip to content

Commit

Permalink
Merge pull request #2 from dancastillo/feat-add-cache-pkg-fix-tests
Browse files Browse the repository at this point in the history
feat: add cache pkg and fix tests
  • Loading branch information
dancastillo authored Jan 20, 2023
2 parents df38f3c + f79a5c9 commit 90e3ebb
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
with:
node-version-file: '.nvmrc'
- run: |
npm ci
npm i
npm run lint
npm test
Expand Down
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const fastify = require('fastify')
const cache = require('@dancastillo/cache')

const { fetchIssues, getGithubClient } = require('./fetchIssues')

Expand Down Expand Up @@ -45,7 +46,8 @@ function build(opts = {}) {
includeBody,
labels,
org,
getGithubClient()
getGithubClient(),
cache()
)

reply.send({ results: issues })
Expand Down
Empty file removed cache.js
Empty file.
24 changes: 9 additions & 15 deletions fetchIssues.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

const { Octokit } = require('@octokit/rest')

let cache = {
org: '',
labels: [],
results: {}
}

/* istanbul ignore next */
const getGithubClient = () => {
/* istanbul ignore next */
Expand All @@ -16,14 +10,18 @@ const getGithubClient = () => {
})
}

const fetchIssues = async (includeBody, labels, org, client) => {
if (cache.org === org && cache.labels.join('') === labels.join('')) {
return cache.results
const fetchIssues = async (includeBody, labels, org, client, cache) => {
const data = cache.get(`${org}_${labels.join('_')}`)
if (data) {
return data
}
const itemSearchResults = await Promise.all(
labels.map(async label => {
const issues = await client.search.issuesAndPullRequests({
q: `is:issue is:open sort:updated-desc label:"${label}" org:"${org}"`
q: `is:issue is:open label:"${label}" org:"${org}"`,
sort: 'updated',
order: 'desc',
per_page: 100
})
return issues.data.items
})
Expand Down Expand Up @@ -59,11 +57,7 @@ const fetchIssues = async (includeBody, labels, org, client) => {
labels: item.labels.map(({ name }) => name)
})
}
cache = {
org,
labels,
results: Array.from(dedupeMap.values())
}
cache.set(`${org}_${labels.join('_')}`, Array.from(dedupeMap.values()))
return Array.from(dedupeMap.values())
}

Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
"main": "server.js",
"scripts": {
"lint": "eslint .",
"test": "tap test.js",
"lint:fix": "eslint . --fix",
"test": "tap test/*.js",
"prepare": "npx husky install",
"start": "node ."
"start": "node .",
"dev": "nodemon ."
},
"repository": {
"type": "git",
Expand All @@ -28,6 +30,7 @@
"eslint-plugin-prettier": "^4.2.1",
"husky": "^8.0.2",
"lint-staged": "^13.1.0",
"nodemon": "^2.0.20",
"pino-pretty": "^9.1.1",
"prettier": "^2.8.1",
"sinon": "^15.0.1",
Expand All @@ -37,6 +40,7 @@
"*.{js,jsx}": "eslint --cache --fix"
},
"dependencies": {
"@dancastillo/cache": "^0.0.3",
"@fastify/cors": "^8.2.0",
"@octokit/rest": "^19.0.5",
"dotenv": "^16.0.3",
Expand Down
93 changes: 87 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 38 additions & 3 deletions test.js → test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const tap = require('tap')
const sinon = require('sinon')

const { fetchIssues } = require('./fetchIssues')
const { fetchIssues } = require('../fetchIssues')

const mockIssue = {
url: 'https://api.github.com/repos/fastify/help/issues/478',
Expand Down Expand Up @@ -107,8 +107,14 @@ tap.test('tests the "/api/find-issues" route', async t => {
data: { ...mockIssuesData, items: [mockIssue2] }
})

const build = t.mock('./app', {
'./fetchIssues': {
const build = t.mock('../app', {
'@dancastillo/cache': () => ({
get: () => {
return null
},
set: () => {}
}),
'../fetchIssues': {
fetchIssues,
getGithubClient: () => {
return {
Expand Down Expand Up @@ -185,3 +191,32 @@ tap.test('tests the "/api/find-issues" route', async t => {
tap.equal(JSON.parse(response2.body).results[0].body, mockIssue.body)
tap.equal(JSON.parse(response2.body).results[1].body, mockIssue.body)
})

tap.test('tests the "/api/find-issues" route with cache', async t => {
const build = t.mock('../app', {
'@dancastillo/cache': () => ({
get: () => {
return []
},
set: () => {}
}),
'../fetchIssues': {
fetchIssues,
getGithubClient: () => {
return {
search: { issuesAndPullRequests: () => undefined }
}
}
}
})
const app = build()

// test with defaults
const response = await app.inject({
method: 'GET',
url: '/api/find-issues?org=test&includeBody=true&labels=1&labels=2'
})

tap.equal(response.statusCode, 200, 'returns a status code of 200')
tap.equal(response.body, JSON.stringify({ results: [] }))
})

0 comments on commit 90e3ebb

Please sign in to comment.