Skip to content

Commit 12f311a

Browse files
authored
refactor: github_api/projects (#536)
* refactor github_api/projects * ran lint
1 parent 47bb1b4 commit 12f311a

File tree

3 files changed

+120
-14
lines changed

3 files changed

+120
-14
lines changed

__tests__/unit/github/api.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,78 @@ describe('getMembershipForUserInOrg', () => {
385385
}
386386
})
387387
})
388+
389+
describe('projectListColumns', () => {
390+
test('return correct data if no error', async () => {
391+
const projectColumns = [
392+
{ id: '1' },
393+
{ id: '2' }
394+
]
395+
396+
const res = await GithubAPI.projectListColumns(Helper.mockContext({ projectColumns }))
397+
expect(res).toEqual(['1', '2'])
398+
})
399+
400+
test('that error are re-thrown', async () => {
401+
const context = Helper.mockContext()
402+
context.octokit.projects.listColumns = jest.fn().mockRejectedValue({ status: 402 })
403+
404+
try {
405+
await GithubAPI.projectListColumns(context)
406+
// Fail test if above expression doesn't throw anything.
407+
expect(true).toBe(false)
408+
} catch (e) {
409+
expect(e.status).toBe(402)
410+
}
411+
})
412+
})
413+
414+
describe('projectListForRepo', () => {
415+
test('return correct data if no error', async () => {
416+
const repoProjects = [
417+
{ name: 'Project One', id: 1 },
418+
{ name: 'Project Two', id: 2 }
419+
]
420+
421+
const res = await GithubAPI.projectListForRepo(Helper.mockContext({ repoProjects }))
422+
expect(res).toEqual(repoProjects)
423+
})
424+
425+
test('that error are re-thrown', async () => {
426+
const context = Helper.mockContext()
427+
context.octokit.projects.listForRepo = jest.fn().mockRejectedValue({ status: 402 })
428+
429+
try {
430+
await GithubAPI.projectListForRepo(context)
431+
// Fail test if above expression doesn't throw anything.
432+
expect(true).toBe(false)
433+
} catch (e) {
434+
expect(e.status).toBe(402)
435+
}
436+
})
437+
})
438+
439+
describe('projectListCards', () => {
440+
test('return correct data if no error', async () => {
441+
const projectCards = [
442+
{ content_url: 'testRepo/issues/1' },
443+
{ content_url: 'testRepo/issues/2' }
444+
]
445+
446+
const res = await GithubAPI.projectListCards(Helper.mockContext({ projectCards }))
447+
expect(res).toEqual({ data: projectCards })
448+
})
449+
450+
test('that error are re-thrown', async () => {
451+
const context = Helper.mockContext()
452+
context.octokit.projects.listCards = jest.fn().mockRejectedValue({ status: 402 })
453+
454+
try {
455+
await GithubAPI.projectListCards(context)
456+
// Fail test if above expression doesn't throw anything.
457+
expect(true).toBe(false)
458+
} catch (e) {
459+
expect(e.status).toBe(402)
460+
}
461+
})
462+
})

lib/github/api.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,46 @@ class GithubAPI {
272272
}
273273
}
274274
}
275+
276+
static async projectListColumns (context, callParam) {
277+
const callFn = 'projects.listColumns'
278+
279+
debugLog(context, callFn)
280+
281+
try {
282+
const res = await context.octokit.projects.listColumns(callParam)
283+
284+
return res.data.map(project => project.id)
285+
} catch (err) {
286+
return checkCommonError(err, context, callFn)
287+
}
288+
}
289+
290+
static async projectListForRepo (context, callParam) {
291+
const callFn = 'projects.listForRepo'
292+
293+
debugLog(context, callFn)
294+
295+
try {
296+
const res = await context.octokit.projects.listForRepo(callParam)
297+
298+
return res.data.map(project => ({ id: project.id, name: project.name }))
299+
} catch (err) {
300+
return checkCommonError(err, context, callFn)
301+
}
302+
}
303+
304+
static async projectListCards (context, callParam) {
305+
const callFn = 'projects.listCards'
306+
307+
debugLog(context, callFn)
308+
309+
try {
310+
return await context.octokit.projects.listCards(callParam)
311+
} catch (err) {
312+
return checkCommonError(err, context, callFn)
313+
}
314+
}
275315
}
276316

277317
module.exports = GithubAPI

lib/validators/project.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Project extends Validator {
3131
const description = validationSettings.message ||
3232
`Must be in the "${projectName}" project.`
3333

34-
const projects = await getProjects(context)
34+
const projects = await this.githubAPI.projectListForRepo(context, context.repo())
3535
const validatorContext = { name: 'project' }
3636
if (!projectName) {
3737
return consolidateResult([constructError('project', projects, validationSettings, MATCH_NOT_FOUND_ERROR)], validatorContext)
@@ -40,7 +40,7 @@ class Project extends Validator {
4040
const regex = new RegExp(projectName, 'i')
4141

4242
const projIds = projects.filter(project => regex.test(project.name)).map(project => project.id)
43-
const projectCards = await getProjectCards(context, projIds)
43+
const projectCards = await getProjectCards(context, projIds, this)
4444
const idsFromCards = extractIssueIdsFromCards(pr, projectCards)
4545
let isMergeable = idsFromCards.includes(String(pr.number))
4646

@@ -72,23 +72,14 @@ const extractIssueIdsFromCards = (pr, cards) => {
7272
return ids
7373
}
7474

75-
const getProjects = async (context) => {
76-
const res = await context.octokit.projects.listForRepo(
77-
context.repo()
78-
)
79-
80-
return res.data.map(project => ({ id: project.id, name: project.name }))
81-
}
82-
83-
const getProjectCards = async (context, projIds) => {
75+
const getProjectCards = async (context, projIds, validatorObj) => {
8476
let cards = []
8577

8678
// get all the project columns
8779
for (let i = 0; i < projIds.length; i++) {
88-
let res = await context.octokit.projects.listColumns({ project_id: projIds[i] })
89-
const columnIds = res.data.map(project => project.id)
80+
const columnIds = await validatorObj.githubAPI.projectListColumns(context, { project_id: projIds[i] })
9081

91-
res = await Promise.all(columnIds.map(id => context.octokit.projects.listCards({ column_id: id })))
82+
const res = await Promise.all(columnIds.map(id => validatorObj.githubAPI.projectListCards(context, { column_id: id })))
9283
res.forEach(card => {
9384
cards = cards.concat(card.data)
9485
})

0 commit comments

Comments
 (0)