Skip to content

Commit

Permalink
feat(platform): getIssue (#10453)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Jun 16, 2021
1 parent 7109030 commit 4a99883
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 21 deletions.
15 changes: 15 additions & 0 deletions lib/platform/gitea/__snapshots__/gitea-helper.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ Array [
]
`;

exports[`platform/gitea/gitea-helper getIssue should call /api/v1/repos/[repo]/issues/[issue] endpoint 1`] = `
Array [
Object {
"headers": Object {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "gitea.renovatebot.com",
"user-agent": "https://github.com/renovatebot/renovate",
},
"method": "GET",
"url": "https://gitea.renovatebot.com/api/v1/repos/some/repo/issues/7",
},
]
`;

exports[`platform/gitea/gitea-helper getOrgLabels should call /api/v1/orgs/[org]/labels endpoint 1`] = `
Array [
Object {
Expand Down
13 changes: 13 additions & 0 deletions lib/platform/gitea/gitea-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,19 @@ describe(getName(), () => {
});
});

describe('getIssue', () => {
it('should call /api/v1/repos/[repo]/issues/[issue] endpoint', async () => {
httpMock
.scope(baseUrl)
.get(`/repos/${mockRepo.full_name}/issues/${mockIssue.number}`)
.reply(200, mockIssue);

const res = await ght.getIssue(mockRepo.full_name, mockIssue.number);
expect(res).toEqual(mockIssue);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});

describe('getRepoLabels', () => {
it('should call /api/v1/repos/[repo]/labels endpoint', async () => {
httpMock
Expand Down
10 changes: 10 additions & 0 deletions lib/platform/gitea/gitea-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ export async function searchIssues(
return res.body;
}

export async function getIssue(
repoPath: string,
idx: number,
options?: GiteaHttpOptions
): Promise<Issue> {
const url = `repos/${repoPath}/issues/${idx}`;
const res = await giteaHttp.getJson<Issue>(url, options);
return res.body;
}

export async function getRepoLabels(
repoPath: string,
options?: GiteaHttpOptions
Expand Down
14 changes: 14 additions & 0 deletions lib/platform/gitea/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,24 @@ describe(getName(), () => {
});
});

describe('getIssue', () => {
it('should return the issue', async () => {
const mockIssue = mockIssues.find((i) => i.number === 1);
helper.getIssue.mockResolvedValueOnce(mockIssue);
await initFakeRepo();

expect(await gitea.getIssue(mockIssue.number)).toHaveProperty(
'number',
mockIssue.number
);
});
});

describe('findIssue', () => {
it('should return existing open issue', async () => {
const mockIssue = mockIssues.find((i) => i.title === 'open-issue');
helper.searchIssues.mockResolvedValueOnce(mockIssues);
helper.getIssue.mockResolvedValueOnce(mockIssue);
await initFakeRepo();

expect(await gitea.findIssue(mockIssue.title)).toHaveProperty(
Expand Down
25 changes: 22 additions & 3 deletions lib/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,34 @@ const platform: Platform = {
return config.issueList;
},

async getIssue(number: number, useCache = true): Promise<Issue> {
try {
const body = (
await helper.getIssue(config.repository, number, {
useCache,
})
).body;
return {
number,
body,
};
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, number }, 'Error getting issue');
return null;
}
},

async findIssue(title: string): Promise<Issue> {
const issueList = await platform.getIssueList();
const issue = issueList.find(
(i) => i.state === 'open' && i.title === title
);

if (issue) {
logger.debug(`Found Issue #${issue.number}`);
if (!issue) {
return null;
}
return issue ?? null;
logger.debug(`Found Issue #${issue.number}`);
return platform.getIssue(issue.number);
},

async ensureIssue({
Expand Down Expand Up @@ -836,6 +854,7 @@ export const {
getBranchPr,
getBranchStatus,
getBranchStatusCheck,
getIssue,
getRawFile,
getJsonFile,
getIssueList,
Expand Down
31 changes: 22 additions & 9 deletions lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,27 @@ export async function getIssueList(): Promise<Issue[]> {
return config.issueList;
}

export async function getIssue(
number: number,
useCache = true
): Promise<Issue | null> {
try {
const issueBody = (
await githubApi.getJson<{ body: string }>(
`repos/${config.parentRepo || config.repository}/issues/${number}`,
{ useCache }
)
).body.body;
return {
number,
body: issueBody,
};
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, number }, 'Error getting issue');
return null;
}
}

export async function findIssue(title: string): Promise<Issue | null> {
logger.debug(`findIssue(${title})`);
const [issue] = (await getIssueList()).filter(
Expand All @@ -1114,15 +1135,7 @@ export async function findIssue(title: string): Promise<Issue | null> {
return null;
}
logger.debug(`Found issue ${issue.number}`);
const issueBody = (
await githubApi.getJson<{ body: string }>(
`repos/${config.parentRepo || config.repository}/issues/${issue.number}`
)
).body.body;
return {
number: issue.number,
body: issueBody,
};
return getIssue(issue.number);
}

async function closeIssue(issueNumber: number): Promise<void> {
Expand Down
31 changes: 22 additions & 9 deletions lib/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,23 +753,36 @@ export async function getIssueList(): Promise<GitlabIssue[]> {
return config.issueList;
}

export async function findIssue(title: string): Promise<Issue | null> {
logger.debug(`findIssue(${title})`);
export async function getIssue(
number: number,
useCache = true
): Promise<Issue | null> {
try {
const issueList = await getIssueList();
const issue = issueList.find((i) => i.title === title);
if (!issue) {
return null;
}
const issueBody = (
await gitlabApi.getJson<{ description: string }>(
`projects/${config.repository}/issues/${issue.iid}`
`projects/${config.repository}/issues/${number}`,
{ useCache }
)
).body.description;
return {
number: issue.iid,
number,
body: issueBody,
};
} catch (err) /* istanbul ignore next */ {
logger.debug({ err, number }, 'Error getting issue');
return null;
}
}

export async function findIssue(title: string): Promise<Issue | null> {
logger.debug(`findIssue(${title})`);
try {
const issueList = await getIssueList();
const issue = issueList.find((i) => i.title === title);
if (!issue) {
return null;
}
return getIssue(issue.iid);
} catch (err) /* istanbul ignore next */ {
logger.warn('Error finding issue');
return null;
Expand Down
1 change: 1 addition & 0 deletions lib/platform/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export type EnsureIssueResult = 'updated' | 'created';
export interface Platform {
findIssue(title: string): Promise<Issue | null>;
getIssueList(): Promise<Issue[]>;
getIssue?(number: number, useCache?: boolean): Promise<Issue>;
getVulnerabilityAlerts(): Promise<VulnerabilityAlert[]>;
getRawFile(fileName: string, repo?: string): Promise<string | null>;
getJsonFile(fileName: string, repo?: string): Promise<any | null>;
Expand Down

0 comments on commit 4a99883

Please sign in to comment.