Skip to content

Commit

Permalink
feat(gitea): support organization labels (#6000)
Browse files Browse the repository at this point in the history
  • Loading branch information
proton-ab committed Apr 21, 2020
1 parent 2cd653c commit 9d8664a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
14 changes: 14 additions & 0 deletions lib/platform/gitea/gitea-helper.spec.ts
Expand Up @@ -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({
Expand Down
10 changes: 10 additions & 0 deletions lib/platform/gitea/gitea-helper.ts
Expand Up @@ -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,
Expand Down
30 changes: 25 additions & 5 deletions lib/platform/gitea/index.spec.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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);

Expand All @@ -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');

Expand Down
22 changes: 20 additions & 2 deletions lib/platform/gitea/index.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 9d8664a

Please sign in to comment.