From 5bab340865af7358508d55b97f6cc7036a6f1abf Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 5 Nov 2024 10:14:16 -0300 Subject: [PATCH 1/6] feat(bitbucket): Add more logging to the PR cache --- lib/modules/platform/bitbucket/index.ts | 2 +- lib/modules/platform/bitbucket/pr-cache.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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.ts b/lib/modules/platform/bitbucket/pr-cache.ts index 06b9b4f33fc078..8576db0ac9a8f3 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -24,6 +24,7 @@ export class BitbucketPrCache { | BitbucketPrCacheData | undefined; if (!pullRequestCache || pullRequestCache.author !== author) { + logger.debug('Initializing new PR cache at repository cache'); pullRequestCache = { items: {}, updated_on: null, @@ -66,6 +67,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 +137,13 @@ 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`); + + this.reconcile(items); + + logger.debug(`Total PRs cached: ${Object.values(this.cache.items).length}`); return this; } } From 3ffca1cf3f1eded6fde33eb16a0239dbe20b6bbf Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 5 Nov 2024 15:34:36 -0300 Subject: [PATCH 2/6] Fix --- .../platform/bitbucket/pr-cache.spec.ts | 49 +++++++++++++++++++ lib/modules/platform/bitbucket/pr-cache.ts | 9 +++- 2 files changed, 57 insertions(+), 1 deletion(-) 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 8576db0ac9a8f3..defc2b3ca9a7ce 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -23,13 +23,20 @@ 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, + author, + }; } repoCache.platform.bitbucket.pullRequestsCache = pullRequestCache; this.cache = pullRequestCache; From 35632967f10587f66f08536f86ace3c0df5aee4b Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 5 Nov 2024 15:43:29 -0300 Subject: [PATCH 3/6] Add trace for entire cache --- lib/modules/platform/bitbucket/pr-cache.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/modules/platform/bitbucket/pr-cache.ts b/lib/modules/platform/bitbucket/pr-cache.ts index defc2b3ca9a7ce..080d285dacb275 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -8,6 +8,7 @@ import { repoCacheProvider } from '../../../util/http/cache/repository-http-cach import type { Pr } from '../types'; import type { BitbucketPrCacheData, PagedResult, PrResponse } from './types'; import { prFieldsFilter, prInfo, prStates } from './utils'; +import { clone } from '../../../util/clone'; export class BitbucketPrCache { private cache: BitbucketPrCacheData; @@ -150,7 +151,16 @@ export class BitbucketPrCache { this.reconcile(items); + const oldCache = clone(this.cache.items); logger.debug(`Total PRs cached: ${Object.values(this.cache.items).length}`); + logger.trace( + { + items, + oldCache, + newCache: this.cache.items, + }, + `Re`, + ); return this; } } From d289ce2fd8c8f4dd06d1affc4ddbe11be08e7824 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 5 Nov 2024 15:43:46 -0300 Subject: [PATCH 4/6] Fix --- lib/modules/platform/bitbucket/pr-cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/platform/bitbucket/pr-cache.ts b/lib/modules/platform/bitbucket/pr-cache.ts index 080d285dacb275..7c550ad1477f54 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -159,7 +159,7 @@ export class BitbucketPrCache { oldCache, newCache: this.cache.items, }, - `Re`, + `PR cache sync finished`, ); return this; } From 5b0f5a5fcc0218df876c8e5f83f68cb4399cf8c9 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Tue, 5 Nov 2024 15:49:26 -0300 Subject: [PATCH 5/6] Fix lint --- lib/modules/platform/bitbucket/pr-cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/platform/bitbucket/pr-cache.ts b/lib/modules/platform/bitbucket/pr-cache.ts index 7c550ad1477f54..0889940892329c 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -3,12 +3,12 @@ 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'; import type { BitbucketPrCacheData, PagedResult, PrResponse } from './types'; import { prFieldsFilter, prInfo, prStates } from './utils'; -import { clone } from '../../../util/clone'; export class BitbucketPrCache { private cache: BitbucketPrCacheData; From 524579db736dfa99dc2a8f24f5db78e510e37e42 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Wed, 6 Nov 2024 09:35:11 -0300 Subject: [PATCH 6/6] Fix --- lib/modules/platform/bitbucket/pr-cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/platform/bitbucket/pr-cache.ts b/lib/modules/platform/bitbucket/pr-cache.ts index 0889940892329c..a270f45b6aa9cc 100644 --- a/lib/modules/platform/bitbucket/pr-cache.ts +++ b/lib/modules/platform/bitbucket/pr-cache.ts @@ -148,10 +148,10 @@ export class BitbucketPrCache { const items = res.body.values; logger.debug(`Fetched ${items.length} PRs to sync with cache`); + const oldCache = clone(this.cache.items); this.reconcile(items); - const oldCache = clone(this.cache.items); logger.debug(`Total PRs cached: ${Object.values(this.cache.items).length}`); logger.trace( {