Skip to content

Commit

Permalink
feat(gitea/forgejo): set poster server filter for pr cache (renovateb…
Browse files Browse the repository at this point in the history
…ot#34374)

Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
  • Loading branch information
viceice and secustor authored Feb 21, 2025
1 parent 718e197 commit c7737be
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 46 deletions.
69 changes: 37 additions & 32 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ describe('modules/platform/gitea/index', () => {
const repoResult = { ...mockRepo, ...repo };
const repository = repoResult.full_name;
scope.get(`/repos/${repository}`).reply(200, repoResult);
await gitea.initRepo({ repository, ...config });
await gitea.initRepo({ repository, ignorePrAuthor: true, ...config });
}

describe('initPlatform()', () => {
Expand Down Expand Up @@ -1160,7 +1160,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand Down Expand Up @@ -1196,7 +1196,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [
thirdPartyPr,
...mockPRs.map((pr) => ({
Expand All @@ -1221,16 +1221,21 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({
state: 'all',
sort: 'recentupdate',
limit: 100,
poster: mockUser.username,
})
.reply(200, mockPRs.slice(0, 2), {
// test correct pagination handling, domain should be ignored
link: '<https://domain.test/api/v1/repos/some/repo/pulls?state=all&sort=recentupdate&page=2>; rel="next",<https://domain.test/api/v1/repos/some/repo/pulls?state=all&sort=recentupdate&page=4512>; rel="last"',
link: '<https://domain.test/some/path/api/v1/repos/some/repo/pulls?state=all&sort=recentupdate&limit=50&page=2>; rel="next",<https://domain.test/some/path/api/v1/repos/some/repo/pulls?state=all&sort=recentupdate&page=4512>; rel="last"',
})
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate', page: 2 })
.query({ state: 'all', sort: 'recentupdate', limit: 50, page: 2 })
.reply(200, mockPRs.slice(2));
await initFakePlatform(scope);
await initFakeRepo(scope);
await initFakeRepo(scope, undefined, { ignorePrAuthor: false });

const res1 = await gitea.getPrList();
const res2 = await gitea.getPrList();
Expand All @@ -1242,10 +1247,10 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs.slice(0, 2))
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 20 })
.reply(200, mockPRs.slice(1));
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand Down Expand Up @@ -1274,7 +1279,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1289,7 +1294,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [])
.get('/repos/some/repo/pulls/1')
.reply(200, pr);
Expand All @@ -1305,7 +1310,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [])
.get('/repos/some/repo/pulls/42')
.reply(200); // TODO: 404 should be handled
Expand All @@ -1321,7 +1326,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [null]); // TODO: 404 should be handled
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1335,7 +1340,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1352,7 +1357,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1372,7 +1377,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1392,7 +1397,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1413,7 +1418,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1435,7 +1440,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1457,7 +1462,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1475,7 +1480,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand Down Expand Up @@ -1613,7 +1618,7 @@ describe('modules/platform/gitea/index', () => {
.post('/repos/some/repo/pulls')
.reply(409)
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [mockNewPR]);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -1637,7 +1642,7 @@ describe('modules/platform/gitea/index', () => {
.post('/repos/some/repo/pulls')
.reply(409)
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, [mockNewPR])
.patch('/repos/some/repo/pulls/42')
.reply(200);
Expand Down Expand Up @@ -1890,7 +1895,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.patch('/repos/some/repo/pulls/1', { title: 'New Title' })
.reply(200, pr);
Expand All @@ -1906,7 +1911,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.patch('/repos/some/repo/pulls/1', {
title: 'New Title',
Expand All @@ -1929,7 +1934,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.patch('/repos/some/repo/pulls/1', {
title: 'New Title',
Expand All @@ -1952,7 +1957,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.patch('/repos/some/repo/pulls/3', {
title: 'WIP: New Title',
Expand All @@ -1975,7 +1980,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.patch('/repos/some/repo/pulls/1', {
title: 'New Title',
Expand Down Expand Up @@ -2013,7 +2018,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.get('/repos/some/repo/labels')
.reply(200, mockRepoLabels)
Expand Down Expand Up @@ -2052,7 +2057,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs)
.get('/repos/some/repo/labels')
.reply(200, mockRepoLabels)
Expand Down Expand Up @@ -2807,7 +2812,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand All @@ -2821,7 +2826,7 @@ describe('modules/platform/gitea/index', () => {
const scope = httpMock
.scope('https://gitea.com/api/v1')
.get('/repos/some/repo/pulls')
.query({ state: 'all', sort: 'recentupdate' })
.query({ state: 'all', sort: 'recentupdate', limit: 100 })
.reply(200, mockPRs);
await initFakePlatform(scope);
await initFakeRepo(scope);
Expand Down
34 changes: 30 additions & 4 deletions lib/modules/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
} from './utils';

interface GiteaRepoConfig {
ignorePrAuthor: boolean;
repository: string;
mergeMethod: PRMergeMethod;

Expand Down Expand Up @@ -263,13 +264,15 @@ const platform: Platform = {
cloneSubmodules,
cloneSubmodulesFilter,
gitUrl,
ignorePrAuthor,
}: RepoParams): Promise<RepoResult> {
let repo: Repo;

config = {} as any;
config.repository = repository;
config.cloneSubmodules = !!cloneSubmodules;
config.cloneSubmodulesFilter = cloneSubmodulesFilter;
config.ignorePrAuthor = !!ignorePrAuthor;

// Try to fetch information about repository
try {
Expand Down Expand Up @@ -475,7 +478,12 @@ const platform: Platform = {
},

getPrList(): Promise<Pr[]> {
return GiteaPrCache.getPrs(giteaHttp, config.repository, botUserName);
return GiteaPrCache.getPrs(
giteaHttp,
config.repository,
config.ignorePrAuthor,
botUserName,
);
},

async getPr(number: number): Promise<Pr | null> {
Expand All @@ -491,7 +499,13 @@ const platform: Platform = {

// Add pull request to cache for further lookups / queries
if (pr) {
await GiteaPrCache.setPr(giteaHttp, config.repository, botUserName, pr);
await GiteaPrCache.setPr(
giteaHttp,
config.repository,
config.ignorePrAuthor,
botUserName,
pr,
);
}
}

Expand Down Expand Up @@ -593,7 +607,13 @@ const platform: Platform = {
throw new Error('Can not parse newly created Pull Request');
}

await GiteaPrCache.setPr(giteaHttp, config.repository, botUserName, pr);
await GiteaPrCache.setPr(
giteaHttp,
config.repository,
config.ignorePrAuthor,
botUserName,
pr,
);
return pr;
} catch (err) {
// When the user manually deletes a branch from Renovate, the PR remains but is no longer linked to any branch. In
Expand Down Expand Up @@ -687,7 +707,13 @@ const platform: Platform = {
);
const pr = toRenovatePR(gpr, botUserName);
if (pr) {
await GiteaPrCache.setPr(giteaHttp, config.repository, botUserName, pr);
await GiteaPrCache.setPr(
giteaHttp,
config.repository,
config.ignorePrAuthor,
botUserName,
pr,
);
}
},

Expand Down
Loading

0 comments on commit c7737be

Please sign in to comment.