Skip to content

Commit

Permalink
fix(gitea): check for disabled issues (#26134)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Dec 5, 2023
1 parent 283fbeb commit a2672ef
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/modules/platform/gitea/gitea-helper.spec.ts
Expand Up @@ -87,6 +87,7 @@ describe('modules/platform/gitea/gitea-helper', () => {
push: true,
admin: false,
},
has_issues: true,
};

const otherMockRepo: Repo = {
Expand Down
29 changes: 29 additions & 0 deletions lib/modules/platform/gitea/index.spec.ts
Expand Up @@ -1327,6 +1327,13 @@ describe('modules/platform/gitea/index', () => {
});
});

describe('getIssueList', () => {
it('should return empty for disabled issues', async () => {
await initFakeRepo({ has_issues: false });
expect(await gitea.getIssueList()).toBeEmptyArray();
});
});

describe('getIssue', () => {
it('should return the issue', async () => {
const mockIssue = mockIssues.find((i) => i.number === 1)!;
Expand All @@ -1338,6 +1345,11 @@ describe('modules/platform/gitea/index', () => {
mockIssue.number,
);
});

it('should return null for disabled issues', async () => {
await initFakeRepo({ has_issues: false });
expect(await gitea.getIssue!(1)).toBeNull();
});
});

describe('findIssue', () => {
Expand Down Expand Up @@ -1657,6 +1669,18 @@ describe('modules/platform/gitea/index', () => {

expect(logger.warn).toHaveBeenCalledTimes(1);
});

it('should return null for disabled issues', async () => {
await initFakeRepo({ has_issues: false });
expect(
await gitea.ensureIssue({
title: 'new-title',
body: 'new-body',
shouldReOpen: false,
once: false,
}),
).toBeNull();
});
});

describe('ensureIssueClosing', () => {
Expand All @@ -1672,6 +1696,11 @@ describe('modules/platform/gitea/index', () => {
mockIssue.number,
);
});

it('should return for disabled issues', async () => {
await initFakeRepo({ has_issues: false });
await expect(gitea.ensureIssueClosing('new-title')).toResolve();
});
});

describe('deleteLabel', () => {
Expand Down
20 changes: 20 additions & 0 deletions lib/modules/platform/gitea/index.ts
Expand Up @@ -64,6 +64,7 @@ interface GiteaRepoConfig {
labelList: Promise<Label[]> | null;
defaultBranch: string;
cloneSubmodules: boolean;
hasIssuesEnabled: boolean;
}

export const id = 'gitea';
Expand Down Expand Up @@ -334,6 +335,7 @@ const platform: Platform = {
config.prList = null;
config.issueList = null;
config.labelList = null;
config.hasIssuesEnabled = repo.has_issues;

return {
defaultBranch: config.defaultBranch,
Expand Down Expand Up @@ -655,6 +657,9 @@ const platform: Platform = {
},

getIssueList(): Promise<Issue[]> {
if (config.hasIssuesEnabled === false) {
return Promise.resolve([]);
}
if (config.issueList === null) {
config.issueList = helper
.searchIssues(config.repository, { state: 'all' }, { memCache: false })
Expand All @@ -669,6 +674,9 @@ const platform: Platform = {
},

async getIssue(number: number, memCache = true): Promise<Issue | null> {
if (config.hasIssuesEnabled === false) {
return null;
}
try {
const body = (
await helper.getIssue(config.repository, number, { memCache })
Expand Down Expand Up @@ -707,6 +715,12 @@ const platform: Platform = {
once,
}: EnsureIssueConfig): Promise<'updated' | 'created' | null> {
logger.debug(`ensureIssue(${title})`);
if (config.hasIssuesEnabled === false) {
logger.info(
'Cannot ensure issue because issues are disabled in this repository',
);
return null;
}
try {
const body = smartLinks(content);

Expand Down Expand Up @@ -819,6 +833,12 @@ const platform: Platform = {

async ensureIssueClosing(title: string): Promise<void> {
logger.debug(`ensureIssueClosing(${title})`);
if (config.hasIssuesEnabled === false) {
logger.info(
'Cannot ensure issue because issues are disabled in this repository',
);
return;
}
const issueList = await platform.getIssueList();
for (const issue of issueList) {
if (issue.state === 'open' && issue.title === title) {
Expand Down
7 changes: 7 additions & 0 deletions lib/modules/platform/gitea/schema.spec.ts
@@ -0,0 +1,7 @@
import { ContentsListResponseSchema } from './schema';

describe('modules/platform/gitea/schema', () => {
it('ContentsResponseSchema', () => {
expect(ContentsListResponseSchema.parse([])).toBeEmptyArray();
});
});
1 change: 1 addition & 0 deletions lib/modules/platform/gitea/types.ts
Expand Up @@ -64,6 +64,7 @@ export interface Repo {
allow_squash_merge: boolean;
archived: boolean;
clone_url?: string;
has_issues: boolean;
ssh_url?: string;
default_branch: string;
empty: boolean;
Expand Down

0 comments on commit a2672ef

Please sign in to comment.