Skip to content

Commit

Permalink
fix(platform): ensure order for cached pr's on gitea and bitbucket
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jan 2, 2025
1 parent 5390390 commit 902c9fa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/modules/platform/bitbucket/pr-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ describe('modules/platform/bitbucket/pr-cache', () => {
);

expect(res).toMatchObject([
{ number: 1, title: 'title' },
{ number: 2, title: 'title' },
{ number: 1, title: 'title' },
]);
expect(cache).toEqual({
httpCache: {},
Expand Down
15 changes: 14 additions & 1 deletion lib/modules/platform/bitbucket/pr-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { BitbucketPrCacheData, PagedResult, PrResponse } from './types';
import { prFieldsFilter, prInfo, prStates } from './utils';

export class BitbucketPrCache {
private items: Pr[] = [];
private cache: BitbucketPrCacheData;

private constructor(
Expand Down Expand Up @@ -41,6 +42,7 @@ export class BitbucketPrCache {
}
repoCache.platform.bitbucket.pullRequestsCache = pullRequestCache;
this.cache = pullRequestCache;
this.updateItems();
}

private static async init(
Expand All @@ -62,7 +64,7 @@ export class BitbucketPrCache {
}

private getPrs(): Pr[] {
return Object.values(this.cache.items);
return this.items;
}

static async getPrs(
Expand All @@ -77,6 +79,7 @@ export class BitbucketPrCache {
private setPr(pr: Pr): void {
logger.debug(`Adding PR #${pr.number} to the PR cache`);
this.cache.items[pr.number] = pr;
this.updateItems();
}

static async setPr(
Expand Down Expand Up @@ -161,6 +164,16 @@ export class BitbucketPrCache {
},
`PR cache sync finished`,
);

this.updateItems();
return this;
}

/**
* Ensure the pr cache starts with the most recent PRs.
* JavaScript ensures that the cache is sorted by PR number.
*/
private updateItems(): void {
this.items = Object.values(this.cache.items).reverse();
}
}
20 changes: 10 additions & 10 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,10 +1166,10 @@ describe('modules/platform/gitea/index', () => {

const res = await gitea.getPrList();
expect(res).toMatchObject([
{ number: 1, title: 'Some PR' },
{ number: 2, title: 'Other PR' },
{ number: 3, title: 'Draft PR' },
{ number: 4, title: 'Merged PR' },
{ number: 3, title: 'Draft PR' },
{ number: 2, title: 'Other PR' },
{ number: 1, title: 'Some PR' },
]);
});

Expand Down Expand Up @@ -1209,10 +1209,10 @@ describe('modules/platform/gitea/index', () => {
const res = await gitea.getPrList();

expect(res).toMatchObject([
{ number: 1, title: 'Some PR' },
{ number: 2, title: 'Other PR' },
{ number: 3, title: 'Draft PR' },
{ number: 4, title: 'Merged PR' },
{ number: 3, title: 'Draft PR' },
{ number: 2, title: 'Other PR' },
{ number: 1, title: 'Some PR' },
]);
});

Expand Down Expand Up @@ -1244,16 +1244,16 @@ describe('modules/platform/gitea/index', () => {
await initFakeRepo(scope);

const res1 = await gitea.getPrList();
expect(res1).toMatchObject([{ number: 1 }, { number: 2 }]);
expect(res1).toMatchObject([{ number: 2 }, { number: 1 }]);

memCache.set('gitea-pr-cache-synced', false);

const res2 = await gitea.getPrList();
expect(res2).toMatchObject([
{ number: 1 },
{ number: 2 },
{ number: 3 },
{ number: 4 },
{ number: 3 },
{ number: 2 },
{ number: 1 },
]);
});
});
Expand Down
15 changes: 14 additions & 1 deletion lib/modules/platform/gitea/pr-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { API_PATH, toRenovatePR } from './utils';

export class GiteaPrCache {
private cache: GiteaPrCacheData;
private items: Pr[] = [];

private constructor(
private repo: string,
Expand All @@ -31,6 +32,7 @@ export class GiteaPrCache {
}
repoCache.platform.gitea.pullRequestsCache = pullRequestCache;
this.cache = pullRequestCache;
this.updateItems();
}

static forceSync(): void {
Expand All @@ -54,7 +56,7 @@ export class GiteaPrCache {
}

private getPrs(): Pr[] {
return Object.values(this.cache.items);
return this.items;
}

static async getPrs(
Expand All @@ -68,6 +70,7 @@ export class GiteaPrCache {

private setPr(item: Pr): void {
this.cache.items[item.number] = item;
this.updateItems();
}

static async setPr(
Expand Down Expand Up @@ -137,6 +140,16 @@ export class GiteaPrCache {
url = parseLinkHeader(res.headers.link)?.next?.url;
}

this.updateItems();

return this;
}

/**
* Ensure the pr cache starts with the most recent PRs.
* JavaScript ensures that the cache is sorted by PR number.
*/
private updateItems(): void {
this.items = Object.values(this.cache.items).reverse();
}
}

0 comments on commit 902c9fa

Please sign in to comment.