Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bitbucket): Add more logging to the PR cache #32339

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
zharinov marked this conversation as resolved.
Show resolved Hide resolved

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;
}
}