Skip to content

Commit

Permalink
feat(bitbucket): Add more logging to the PR cache (#32339)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored Nov 6, 2024
1 parent ccc2d45 commit 13f4b9a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ function matchesState(state: string, desiredState: string): boolean {
}

export async function getPrList(): Promise<Pr[]> {
logger.debug('getPrList()');
logger.trace('getPrList()');
return await BitbucketPrCache.getPrs(
bitbucketHttp,
config.repository,
Expand Down
49 changes: 49 additions & 0 deletions lib/modules/platform/bitbucket/pr-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,55 @@ describe('modules/platform/bitbucket/pr-cache', () => {
});
});

it('resets cache for not matching authors', async () => {
cache.platform = {
bitbucket: {
pullRequestsCache: {
items: {
'1': prInfo(pr1),
},
author: 'some-other-author',
updated_on: '2020-01-01T00:00:00.000Z',
},
},
};

httpMock
.scope('https://api.bitbucket.org')
.get(`/2.0/repositories/some-workspace/some-repo/pullrequests`)
.query(true)
.reply(200, {
values: [pr1],
});

const res = await BitbucketPrCache.getPrs(
http,
'some-workspace/some-repo',
'some-author',
);

expect(res).toMatchObject([
{
number: 1,
title: 'title',
},
]);
expect(cache).toEqual({
httpCache: {},
platform: {
bitbucket: {
pullRequestsCache: {
author: 'some-author',
items: {
'1': prInfo(pr1),
},
updated_on: '2020-01-01T00:00:00.000Z',
},
},
},
});
});

it('syncs cache', async () => {
cache.platform = {
bitbucket: {
Expand Down
29 changes: 27 additions & 2 deletions lib/modules/platform/bitbucket/pr-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DateTime } from 'luxon';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import { getCache } from '../../../util/cache/repository';
import { clone } from '../../../util/clone';
import type { BitbucketHttp } from '../../../util/http/bitbucket';
import { repoCacheProvider } from '../../../util/http/cache/repository-http-cache-provider';
import type { Pr } from '../types';
Expand All @@ -23,7 +24,15 @@ export class BitbucketPrCache {
let pullRequestCache = repoCache.platform.bitbucket.pullRequestsCache as
| BitbucketPrCacheData
| undefined;
if (!pullRequestCache || pullRequestCache.author !== author) {
if (!pullRequestCache) {
logger.debug('Initializing new PR cache at repository cache');
pullRequestCache = {
items: {},
updated_on: null,
author,
};
} else if (pullRequestCache.author !== author) {
logger.debug('Resetting PR cache because authors do not match');
pullRequestCache = {
items: {},
updated_on: null,
Expand Down Expand Up @@ -66,6 +75,7 @@ export class BitbucketPrCache {
}

private addPr(pr: Pr): void {
logger.debug(`Adding PR #${pr.number} to the PR cache`);
this.cache.items[pr.number] = pr;
}

Expand Down Expand Up @@ -135,7 +145,22 @@ export class BitbucketPrCache {
cacheProvider: repoCacheProvider,
};
const res = await http.getJson<PagedResult<PrResponse>>(url, opts);
this.reconcile(res.body.values);

const items = res.body.values;
logger.debug(`Fetched ${items.length} PRs to sync with cache`);
const oldCache = clone(this.cache.items);

this.reconcile(items);

logger.debug(`Total PRs cached: ${Object.values(this.cache.items).length}`);
logger.trace(
{
items,
oldCache,
newCache: this.cache.items,
},
`PR cache sync finished`,
);
return this;
}
}

0 comments on commit 13f4b9a

Please sign in to comment.