Skip to content

Commit

Permalink
fix(gitea): handle null PR as temporary error (#33623)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jan 15, 2025
1 parent bddef47 commit e9bc921
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
REPOSITORY_CHANGED,
REPOSITORY_EMPTY,
REPOSITORY_MIRRORED,
TEMPORARY_ERROR,
} from '../../../constants/error-messages';
import type { logger as _logger } from '../../../logger';
import type * as _git from '../../../util/git';
Expand Down Expand Up @@ -1309,6 +1310,18 @@ describe('modules/platform/gitea/index', () => {

expect(res).toBeNull();
});

it('should throw temporary error for null pull request', async () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.reply(200, [null]); // TODO: 404 should be handled
await initFakePlatform(scope);
await initFakeRepo(scope);

await expect(gitea.getPr(42)).rejects.toThrow(TEMPORARY_ERROR);
});
});

describe('findPr', () => {
Expand Down
13 changes: 11 additions & 2 deletions lib/modules/platform/gitea/pr-cache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { dequal } from 'dequal';
import { DateTime } from 'luxon';
import { TEMPORARY_ERROR } from '../../../constants/error-messages';
import { logger } from '../../../logger';
import * as memCache from '../../../util/cache/memory';
import { getCache } from '../../../util/cache/repository';
import type { GiteaHttp } from '../../../util/http/gitea';
Expand Down Expand Up @@ -83,14 +85,20 @@ export class GiteaPrCache {
prCache.setPr(item);
}

private reconcile(rawItems: PR[]): boolean {
private reconcile(rawItems: (PR | null)[]): boolean {
const { items } = this.cache;
let { updated_at } = this.cache;
const cacheTime = updated_at ? DateTime.fromISO(updated_at) : null;

let needNextPage = true;

for (const rawItem of rawItems) {
if (!rawItem) {
logger.warn('Gitea PR is empty, throwing temporary error');
// Gitea API sometimes returns empty PRs, so we throw a temporary error
// https://github.com/go-gitea/gitea/blob/fcd096231ac2deaefbca10a7db1b9b01f1da93d7/services/convert/pull.go#L34-L52
throw new Error(TEMPORARY_ERROR);
}
const id = rawItem.number;

const newItem = toRenovatePR(rawItem, this.author);
Expand Down Expand Up @@ -127,7 +135,8 @@ export class GiteaPrCache {
`${API_PATH}/repos/${this.repo}/pulls?${query}`;

while (url) {
const res: HttpResponse<PR[]> = await http.getJson<PR[]>(url, {
// TODO: use zod, typescript can't infer the type of the response #22198
const res: HttpResponse<(PR | null)[]> = await http.getJson(url, {
memCache: false,
paginate: false,
});
Expand Down

0 comments on commit e9bc921

Please sign in to comment.