diff --git a/lib/modules/platform/bitbucket/index.ts b/lib/modules/platform/bitbucket/index.ts index 195f4a65b4a2c1..9678aa2443b45e 100644 --- a/lib/modules/platform/bitbucket/index.ts +++ b/lib/modules/platform/bitbucket/index.ts @@ -283,7 +283,7 @@ function matchesState(state: string, desiredState: string): boolean { } export async function getPrList(): Promise { - logger.debug('getPrList()'); + logger.trace('getPrList()'); return await BitbucketPrCache.getPrs( bitbucketHttp, config.repository, diff --git a/lib/modules/platform/bitbucket/pr-cache.spec.ts b/lib/modules/platform/bitbucket/pr-cache.spec.ts index ef71374fb292c4..ac16cc38a228ea 100644 --- a/lib/modules/platform/bitbucket/pr-cache.spec.ts +++ b/lib/modules/platform/bitbucket/pr-cache.spec.ts @@ -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: { diff --git a/lib/modules/platform/bitbucket/pr-cache.ts b/lib/modules/platform/bitbucket/pr-cache.ts index 06b9b4f33fc078..a270f45b6aa9cc 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -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'; @@ -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, @@ -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; } @@ -135,7 +145,22 @@ export class BitbucketPrCache { cacheProvider: repoCacheProvider, }; const res = await http.getJson>(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; } }