Skip to content

Commit

Permalink
feat(project): add error message when project not found
Browse files Browse the repository at this point in the history
  • Loading branch information
Singebob authored and Jeremy Chauvin committed Oct 15, 2020
1 parent 8e449ac commit 379f30f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 47 deletions.
3 changes: 3 additions & 0 deletions src/projects/utils/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const registerIssueToKanban = async (context) => {
content_id: context.payload.issue.id
})
} catch (error) {
if(error.message == "Project not found") {
await addCommentToIssue("Project not found")
}
throw error;
}
}
Expand Down
89 changes: 42 additions & 47 deletions src/projects/utils/kanban.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,56 @@
const getProjectKanban = (context) => {
return new Promise((resolve, reject) => {
const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
context.github.projects.listForRepo({owner, repo, state: 'open'})
.then( res => {
const kanban = res.data.filter(project => project.name === 'kanban automatic')
kanban.length >= 1 ? resolve(kanban[0]) : reject('nothing kanban')
})
.catch(err => {
reject(err)
})
})
const getProjectKanban = async (context) => {
const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
const projects = await context.github.projects.listForRepo({ owner, repo, state: 'open' })
const kanban = projects.data.filter(project => project.name === 'kanban automatic')
if(kanban.length >= 1)
return kanban[0]
throw Error("Project not found")
}

const getColumn = (context, project, name) => {
return new Promise((resolve, reject) => {
context.github.projects.listColumns({project_id: project.id})
.then(resColumns => {
const columns = resColumns.data.filter(column => column.name === name)
columns.length >= 1 ? resolve(columns[0]) : reject('no columns')
})
.catch(err => reject(err))
})
return new Promise((resolve, reject) => {
context.github.projects.listColumns({ project_id: project.id })
.then(resColumns => {
const columns = resColumns.data.filter(column => column.name === name)
columns.length >= 1 ? resolve(columns[0]) : reject('no columns')
})
.catch(err => reject(err))
})
}

const getCard = (context, project, columnName, issueNumber) => {
return new Promise((resolve, reject) => {
getColumn(context, project, columnName)
.then(column => {
context.github.projects.listCards({column_id: column.id, archived_state: 'not_archived'})
.then(cards => {
const card = cards.data.find(card => {
const issueNumberCard = card.content_url.slice(card.content_url.lastIndexOf('/')+ 1, card.content_url.length)
return issueNumberCard == issueNumber
})
if(card === undefined){
reject('card not found')
}
context.github.projects.getCard({card_id: card.id})
.then(resCard => {
resolve(resCard.data)
}).catch(err => reject(err))
return new Promise((resolve, reject) => {
getColumn(context, project, columnName)
.then(column => {
context.github.projects.listCards({ column_id: column.id, archived_state: 'not_archived' })
.then(cards => {
const card = cards.data.find(card => {
const issueNumberCard = card.content_url.slice(card.content_url.lastIndexOf('/') + 1, card.content_url.length)
return issueNumberCard == issueNumber
})
})
})
if (card === undefined) {
reject('card not found')
}
context.github.projects.getCard({ card_id: card.id })
.then(resCard => {
resolve(resCard.data)
}).catch(err => reject(err))
})
})
})
}

const moveCard = async (context, fromColumn, targetColumn, issueNumber) => {
const project = await getProjectKanban(context)
const column = await getColumn(context, project, targetColumn)
const card = await getCard(context, project, fromColumn, issueNumber)
await context.github.projects.moveCard({position: 'top', column_id: column.id, card_id: card.id})
const project = await getProjectKanban(context)
const column = await getColumn(context, project, targetColumn)
const card = await getCard(context, project, fromColumn, issueNumber)
await context.github.projects.moveCard({ position: 'top', column_id: column.id, card_id: card.id })
}

module.exports = {
getProjectKanban,
getColumn,
getCard,
moveCard,
getProjectKanban,
getColumn,
getCard,
moveCard,
}
51 changes: 51 additions & 0 deletions test/utils/kanban.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const kanbanUtils = require('../../src/projects/utils/kanban')

describe('kanban utils', () => {
beforeEach(() => {
jest.clearAllMocks()
})

describe('getProjectKanban', () =>{
test('when all is fine', async () => {
const context = {
payload: {
repository: {
name: 'botProjet',
owner: {
login: 'Singebob',
}
}
},
github: {
projects: {
listForRepo: jest.fn(() => {return {data: [{name: 'kanban automatic'}]}})
}
}
}
const result = await kanbanUtils.getProjectKanban(context)
expect(result).toEqual({name: 'kanban automatic'})
})
test('when kanban project not found', async () => {
const context = {
payload: {
repository: {
name: 'botProjet',
owner: {
login: 'Singebob',
}
}
},
github: {
projects: {
listForRepo: jest.fn(() => {return {data: [{name: 'kanban'}]}})
}
}
}
try {
await kanbanUtils.getProjectKanban(context)
} catch (error) {
expect(error.message).toEqual('Project not found')
}
})
})
})

0 comments on commit 379f30f

Please sign in to comment.