From 9d8664ad09bfbba8674dae51a0fcea6e3b81aedb Mon Sep 17 00:00:00 2001 From: proton <25139420+proton-ab@users.noreply.github.com> Date: Tue, 21 Apr 2020 09:53:52 +0200 Subject: [PATCH] feat(gitea): support organization labels (#6000) --- lib/platform/gitea/gitea-helper.spec.ts | 14 ++++++++++++ lib/platform/gitea/gitea-helper.ts | 10 +++++++++ lib/platform/gitea/index.spec.ts | 30 ++++++++++++++++++++----- lib/platform/gitea/index.ts | 22 ++++++++++++++++-- 4 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/platform/gitea/gitea-helper.spec.ts b/lib/platform/gitea/gitea-helper.spec.ts index 59d02905a2..aba869fc93 100644 --- a/lib/platform/gitea/gitea-helper.spec.ts +++ b/lib/platform/gitea/gitea-helper.spec.ts @@ -622,6 +622,20 @@ describe('platform/gitea/gitea-helper', () => { }); }); + describe('getOrgLabels', () => { + it('should call /api/v1/orgs/[org]/labels endpoint', async () => { + mockAPI<ght.Label[]>( + { + urlPattern: `/api/v1/orgs/${mockRepo.owner.username}/labels`, + }, + [mockLabel, otherMockLabel] + ); + + const res = await helper.getOrgLabels(mockRepo.owner.username); + expect(res).toEqual([mockLabel, otherMockLabel]); + }); + }); + describe('unassignLabel', () => { it('should call /api/v1/repos/[repo]/issues/[issue]/labels/[label] endpoint', async () => { mockAPI({ diff --git a/lib/platform/gitea/gitea-helper.ts b/lib/platform/gitea/gitea-helper.ts index 1b7b6566e1..5e56eff112 100644 --- a/lib/platform/gitea/gitea-helper.ts +++ b/lib/platform/gitea/gitea-helper.ts @@ -399,6 +399,16 @@ export async function getRepoLabels( return res.body; } +export async function getOrgLabels( + orgName: string, + options?: GiteaGotOptions +): Promise<Label[]> { + const url = `orgs/${orgName}/labels`; + const res: GotResponse<Label[]> = await api.get(url, options); + + return res.body; +} + export async function unassignLabel( repoPath: string, issue: number, diff --git a/lib/platform/gitea/index.spec.ts b/lib/platform/gitea/index.spec.ts index 3351c8b044..fdedcd0e12 100644 --- a/lib/platform/gitea/index.spec.ts +++ b/lib/platform/gitea/index.spec.ts @@ -135,11 +135,26 @@ describe('platform/gitea', () => { { id: 3, body: '### some-topic\n\nsome-content' }, ]; - const mockLabels: ght.Label[] = [ + const mockRepoLabels: ght.Label[] = [ { id: 1, name: 'some-label', description: 'its a me', color: '#000000' }, { id: 2, name: 'other-label', description: 'labelario', color: '#ffffff' }, ]; + const mockOrgLabels: ght.Label[] = [ + { + id: 3, + name: 'some-org-label', + description: 'its a org me', + color: '#0000aa', + }, + { + id: 4, + name: 'other-org-label', + description: 'org labelario', + color: '#ffffaa', + }, + ]; + const gsmInitRepo = jest.fn(); const gsmCleanRepo = jest.fn(); const gsmSetBaseBranch = jest.fn(); @@ -789,7 +804,10 @@ describe('platform/gitea', () => { it('should resolve and apply optional labels to pull request', async () => { helper.createPR.mockResolvedValueOnce(mockNewPR); - helper.getRepoLabels.mockResolvedValueOnce(mockLabels); + helper.getRepoLabels.mockResolvedValueOnce(mockRepoLabels); + helper.getOrgLabels.mockResolvedValueOnce(mockOrgLabels); + + const mockLabels = mockRepoLabels.concat(mockOrgLabels); await initFakeRepo(); await gitea.createPr({ @@ -1157,8 +1175,9 @@ index 0000000..2173594 describe('deleteLabel', () => { it('should delete a label which exists', async () => { - const mockLabel = mockLabels[0]; - helper.getRepoLabels.mockResolvedValueOnce(mockLabels); + const mockLabel = mockRepoLabels[0]; + helper.getRepoLabels.mockResolvedValueOnce(mockRepoLabels); + helper.getOrgLabels.mockRejectedValueOnce(new Error()); await initFakeRepo(); await gitea.deleteLabel(42, mockLabel.name); @@ -1171,7 +1190,8 @@ index 0000000..2173594 }); it('should gracefully fail with warning if label is missing', async () => { - helper.getRepoLabels.mockResolvedValueOnce(mockLabels); + helper.getRepoLabels.mockResolvedValueOnce(mockRepoLabels); + helper.getOrgLabels.mockResolvedValueOnce([]); await initFakeRepo(); await gitea.deleteLabel(42, 'missing'); diff --git a/lib/platform/gitea/index.ts b/lib/platform/gitea/index.ts index c2c2351963..2a4bdc6e9d 100644 --- a/lib/platform/gitea/index.ts +++ b/lib/platform/gitea/index.ts @@ -166,14 +166,32 @@ async function retrieveDefaultConfig( function getLabelList(): Promise<helper.Label[]> { if (config.labelList === null) { - config.labelList = helper + const repoLabels = helper .getRepoLabels(config.repository, { useCache: false, }) .then((labels) => { - logger.debug(`Retrieved ${labels.length} Labels`); + logger.debug(`Retrieved ${labels.length} repo labels`); return labels; }); + + const orgLabels = helper + .getOrgLabels(config.repository.split('/')[0], { + useCache: false, + }) + .then((labels) => { + logger.debug(`Retrieved ${labels.length} org labels`); + return labels; + }) + .catch((err) => { + // Will fail if owner of repo is not org or Gitea version < 1.12 + logger.debug(`Unable to fetch organization labels`); + return []; + }); + + config.labelList = Promise.all([repoLabels, orgLabels]).then((labels) => { + return [].concat(...labels); + }); } return config.labelList; -- GitLab