Skip to content

Commit c2283d6

Browse files
committed
Refactor caching system
1 parent bddce58 commit c2283d6

File tree

3 files changed

+41
-46
lines changed

3 files changed

+41
-46
lines changed

src/reporter/github.js

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ export default class GitHub {
3030
this.commonParams = { owner, repo };
3131

3232
this.issuesCache = new Map();
33-
this.cacheLoadedPromise = null;
33+
this._issuesPromise = null;
34+
}
35+
36+
get issues() {
37+
if (!this._issuesPromise) {
38+
logger.info('Loading issues from GitHub…');
39+
this._issuesPromise = this.loadAllIssues();
40+
}
41+
42+
return this._issuesPromise;
43+
}
44+
45+
clearCache() {
46+
this.issuesCache.clear();
47+
this._issuesPromise = null;
48+
logger.info('Issues cache cleared');
3449
}
3550

3651
async initialize() {
@@ -74,29 +89,13 @@ export default class GitHub {
7489
}
7590
});
7691

77-
logger.info(`Cached ${onlyIssues.length} issues from the repository`);
78-
} catch (error) {
79-
logger.error(`Failed to load issues: ${error.message}`);
80-
}
81-
}
92+
logger.info(`Cached ${onlyIssues.length} issues from the GitHub repository`);
8293

83-
async refreshIssuesCache() {
84-
try {
85-
logger.info('Refreshing issues cache from GitHub…');
86-
this.issuesCache.clear();
87-
this.cacheLoadedPromise = this.loadAllIssues();
88-
await this.cacheLoadedPromise;
89-
logger.info('Issues cache refreshed successfully');
94+
return this.issuesCache;
9095
} catch (error) {
91-
logger.error(`Failed to refresh issues cache: ${error.message}`);
92-
}
93-
}
94-
95-
async ensureCacheLoaded() {
96-
if (!this.cacheLoadedPromise) {
97-
this.cacheLoadedPromise = this.loadAllIssues();
96+
logger.error(`Failed to load issues: ${error.message}`);
97+
throw error;
9898
}
99-
await this.cacheLoadedPromise;
10099
}
101100

102101
async getRepositoryLabels() {
@@ -114,9 +113,11 @@ export default class GitHub {
114113
});
115114
}
116115

117-
async createIssue({ title, description: body, labels }) {
118-
await this.ensureCacheLoaded();
116+
async getIssue(title) {
117+
return (await this.issues).get(title);
118+
}
119119

120+
async createIssue({ title, description: body, labels }) {
120121
const { data: issue } = await this.octokit.request('POST /repos/{owner}/{repo}/issues', {
121122
...this.commonParams,
122123
title,
@@ -130,8 +131,6 @@ export default class GitHub {
130131
}
131132

132133
async updateIssue(issue, { state, labels }) {
133-
await this.ensureCacheLoaded();
134-
135134
const { data: updatedIssue } = await this.octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
136135
...this.commonParams,
137136
issue_number: issue.number,
@@ -144,13 +143,7 @@ export default class GitHub {
144143
return updatedIssue;
145144
}
146145

147-
getIssue(title) {
148-
return this.issuesCache.get(title) || null;
149-
}
150-
151146
async addCommentToIssue({ issue, comment: body }) {
152-
await this.ensureCacheLoaded();
153-
154147
const { data: comment } = await this.octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
155148
...this.commonParams,
156149
issue_number: issue.number,
@@ -162,9 +155,7 @@ export default class GitHub {
162155

163156
async closeIssueWithCommentIfExists({ title, comment }) {
164157
try {
165-
await this.ensureCacheLoaded();
166-
167-
const issue = this.getIssue(title);
158+
const issue = await this.getIssue(title);
168159

169160
if (!issue || issue.state == GitHub.ISSUE_STATE_CLOSED) {
170161
return;
@@ -183,9 +174,7 @@ export default class GitHub {
183174

184175
async createOrUpdateIssue({ title, description, label }) {
185176
try {
186-
await this.ensureCacheLoaded();
187-
188-
const issue = this.getIssue(title);
177+
const issue = await this.getIssue(title);
189178

190179
if (!issue) {
191180
const createdIssue = await this.createIssue({ title, description, labels: [label] });
@@ -194,7 +183,6 @@ export default class GitHub {
194183
}
195184

196185
const managedLabelsNames = this.MANAGED_LABELS.map(label => label.name);
197-
198186
const labelsNotManagedToKeep = issue.labels.map(label => label.name).filter(label => !managedLabelsNames.includes(label));
199187
const [managedLabel] = issue.labels.filter(label => managedLabelsNames.includes(label.name)); // It is assumed that only one specific reason for failure is possible at a time, making managed labels mutually exclusive
200188

src/reporter/github.test.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('GitHub', function () {
2222
.get('/repos/owner/repo/issues')
2323
.query(true)
2424
.reply(200, [ EXISTING_OPEN_ISSUE, EXISTING_CLOSED_ISSUE ]);
25-
await github.refreshIssuesCache();
25+
await github.clearCache();
2626
});
2727

2828
describe('#initialize', () => {
@@ -143,15 +143,22 @@ describe('GitHub', function () {
143143
});
144144

145145
describe('#getIssue', () => {
146+
before(() => {
147+
nock('https://api.github.com')
148+
.get('/repos/owner/repo/issues')
149+
.query(true)
150+
.reply(200, [ EXISTING_OPEN_ISSUE, EXISTING_CLOSED_ISSUE ]);
151+
});
152+
146153
context('when the issue exists in the cache', () => {
147-
it('returns the cached issue', () => {
148-
expect(github.getIssue(EXISTING_OPEN_ISSUE.title)).to.deep.equal(EXISTING_OPEN_ISSUE);
154+
it('returns the cached issue', async () => {
155+
expect(await github.getIssue(EXISTING_OPEN_ISSUE.title)).to.deep.equal(EXISTING_OPEN_ISSUE);
149156
});
150157
});
151158

152159
context('when the issue does not exist in the cache', () => {
153-
it('returns null', () => {
154-
expect(github.getIssue('Non-existent Issue')).to.be.null;
160+
it('returns undefined', async () => {
161+
expect(await github.getIssue('Non-existent Issue')).to.be.undefined;
155162
});
156163
});
157164
});
@@ -264,7 +271,7 @@ describe('GitHub', function () {
264271
});
265272

266273
describe('#createOrUpdateIssue', () => {
267-
before(async () => {
274+
before(() => {
268275
github.MANAGED_LABELS = require('./labels.json');
269276
});
270277

@@ -315,7 +322,7 @@ describe('GitHub', function () {
315322
});
316323

317324
after(() => {
318-
github.issuesCache.delete(EXISTING_CLOSED_ISSUE);
325+
github.issuesCache.delete(EXISTING_CLOSED_ISSUE.title);
319326
github.issuesCache.set(EXISTING_CLOSED_ISSUE.title, EXISTING_CLOSED_ISSUE);
320327
});
321328

src/reporter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class Reporter {
5050
}
5151

5252
onTrackingStarted() {
53-
return this.github.refreshIssuesCache();
53+
return this.github.clearCache();
5454
}
5555

5656
async onVersionRecorded(version) {

0 commit comments

Comments
 (0)