Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(platform/gitea): autodiscover repos by topic #26676

Merged
merged 18 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/usage/self-hosted-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ If set to any string, Renovate will use this as the `user-agent` it sends with H

The order method for autodiscover server side repository search.

> If multiple `autodiscoverTopics` are used resulting order will be per topic not global.

Allowed values:

- `asc`
Expand All @@ -63,6 +65,8 @@ Default value: `asc`.

The sort method for autodiscover server side repository search.

> If multiple `autodiscoverTopics` are used resulting order will be per topic not global.

Allowed values:

- `alpha`
Expand Down
2 changes: 1 addition & 1 deletion lib/config/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ const options: RenovateOptions[] = [
subType: 'string',
default: null,
globalOnly: true,
supportedPlatforms: ['github', 'gitlab'],
supportedPlatforms: ['gitea', 'github', 'gitlab'],
},
{
name: 'prCommitsPerRunLimit',
Expand Down
39 changes: 39 additions & 0 deletions lib/modules/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ describe('modules/platform/gitea/index', () => {
partial<Repo>({ full_name: 'e/f', mirror: true }),
];

const mockTopicRepos: Repo[] = [partial<Repo>({ full_name: 'a/b' })];

const mockPRs: MockPr[] = [
partial<MockPr>({
number: 1,
Expand Down Expand Up @@ -351,6 +353,43 @@ describe('modules/platform/gitea/index', () => {
expect(repos).toEqual(['a/b', 'c/d']);
});

it('should return an filtered array of repos', async () => {
const scope = httpMock.scope('https://gitea.com/api/v1');

scope
.get('/repos/search')
.query({
uid: 1,
archived: false,
q: 'renovate',
topic: true,
})
.reply(200, {
ok: true,
data: mockTopicRepos,
});

scope
.get('/repos/search')
.query({
uid: 1,
archived: false,
q: 'renovatebot',
topic: true,
})
.reply(200, {
ok: true,
data: mockTopicRepos,
});

await initFakePlatform(scope);

const repos = await gitea.getRepos({
topics: ['renovate', 'renovatebot'],
});
expect(repos).toEqual(['a/b']);
});

it('Sorts repos', async () => {
process.env.RENOVATE_X_AUTODISCOVER_REPO_SORT = 'updated';
process.env.RENOVATE_X_AUTODISCOVER_REPO_ORDER = 'desc';
Expand Down
40 changes: 28 additions & 12 deletions lib/modules/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import {
} from '../../../constants/error-messages';
import { logger } from '../../../logger';
import type { BranchStatus } from '../../../types';
import { deduplicateArray } from '../../../util/array';
import { parseJson } from '../../../util/common';
import * as git from '../../../util/git';
import { setBaseUrl } from '../../../util/http/gitea';
import { map } from '../../../util/promises';
import { sanitize } from '../../../util/sanitize';
import { ensureTrailingSlash } from '../../../util/url';
import { getPrBodyStruct, hashBody } from '../pr-body';
import type {
AutodiscoverConfig,
BranchStatusConfig,
CreatePRConfig,
EnsureCommentConfig,
Expand Down Expand Up @@ -154,6 +157,24 @@ async function lookupLabelByName(name: string): Promise<number | null> {
return labelList.find((l) => l.name === name)?.id ?? null;
}

async function fetchRepositories(topic?: string): Promise<string[]> {
const repos = await helper.searchRepos({
uid: botUserID,
archived: false,
...(topic && {
topic: true,
q: topic,
lafriks marked this conversation as resolved.
Show resolved Hide resolved
}),
...(process.env.RENOVATE_X_AUTODISCOVER_REPO_SORT && {
sort: process.env.RENOVATE_X_AUTODISCOVER_REPO_SORT as RepoSortMethod,
}),
...(process.env.RENOVATE_X_AUTODISCOVER_REPO_ORDER && {
order: process.env.RENOVATE_X_AUTODISCOVER_REPO_ORDER as SortMethod,
lafriks marked this conversation as resolved.
Show resolved Hide resolved
}),
});
return repos.filter((r) => !r.mirror).map((r) => r.full_name);
}

const platform: Platform = {
async initPlatform({
endpoint,
Expand Down Expand Up @@ -295,20 +316,15 @@ const platform: Platform = {
};
},

async getRepos(): Promise<string[]> {
async getRepos(config?: AutodiscoverConfig): Promise<string[]> {
logger.debug('Auto-discovering Gitea repositories');
try {
const repos = await helper.searchRepos({
uid: botUserID,
archived: false,
...(process.env.RENOVATE_X_AUTODISCOVER_REPO_SORT && {
sort: process.env.RENOVATE_X_AUTODISCOVER_REPO_SORT as RepoSortMethod,
}),
...(process.env.RENOVATE_X_AUTODISCOVER_REPO_ORDER && {
order: process.env.RENOVATE_X_AUTODISCOVER_REPO_ORDER as SortMethod,
}),
});
return repos.filter((r) => !r.mirror).map((r) => r.full_name);
if (!config?.topics) {
return await fetchRepositories();
}

const repos = await map(config.topics, fetchRepositories);
return deduplicateArray(repos.flat());
} catch (err) {
logger.error({ err }, 'Gitea getRepos() error');
throw err;
Expand Down
2 changes: 2 additions & 0 deletions lib/modules/platform/gitea/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export type SortMethod = 'asc' | 'desc';
export interface RepoSearchParams {
uid?: number;
archived?: boolean;
topic?: boolean;
q?: string;

/**
* Repo sort type, defaults to `alpha`.
Expand Down