-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f373bb
commit c510552
Showing
8 changed files
with
209 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const { durationToString, isNil } = require('../utils'); | ||
|
||
const noParse = (value) => String(value ?? '-'); | ||
|
||
const toFixed = (decimals) => (value) => (isNil(value) ? '-' : value.toFixed(decimals)); | ||
|
||
const STATS = { | ||
totalReviews: { | ||
id: 'totalReviews', | ||
sortOrder: 'DESC', | ||
parser: noParse, | ||
}, | ||
timeToReview: { | ||
id: 'timeToReview', | ||
sortOrder: 'ASC', | ||
parser: durationToString, | ||
}, | ||
totalComments: { | ||
id: 'totalComments', | ||
sortOrder: 'DESC', | ||
parser: noParse, | ||
}, | ||
commentsPerReview: { | ||
id: 'commentsPerReview', | ||
sortOrder: 'DESC', | ||
parser: toFixed(2), | ||
}, | ||
openedPullRequests: { | ||
id: 'openedPullRequests', | ||
sortOrder: 'DESC', | ||
parser: noParse, | ||
}, | ||
}; | ||
|
||
const VALID_STATS = Object.keys(STATS); | ||
|
||
const DEFAULT_STATS = ['totalReviews', 'timeToReview', 'totalComments']; | ||
|
||
module.exports = { | ||
STATS, | ||
VALID_STATS, | ||
DEFAULT_STATS, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
src/interactors/buildMarkdown/__tests__/getMarkdownContent.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const { t } = require('../../../i18n'); | ||
const { table } = require('../../../../tests/mocks'); | ||
const getMarkdownContent = require('../getMarkdownContent'); | ||
|
||
const HEADERS = [ | ||
'', | ||
t('table.columns.username'), | ||
t('table.columns.totalReviews'), | ||
t('table.columns.timeToReview'), | ||
t('table.columns.totalComments'), | ||
t('table.columns.commentsPerReview'), | ||
t('table.columns.openedPullRequests'), | ||
]; | ||
|
||
const AVATAR1_SM = '<a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/user1" width="20"></a>'; | ||
const AVATAR1_LG = '<a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/user1" width="32"></a>'; | ||
|
||
const ROW1_SIMPLE = [ | ||
AVATAR1_LG, | ||
'user1<br/>🥇', | ||
'**4**<br/>▀▀▀▀▀▀▀▀', | ||
'[34m](https://app.flowwer.dev/charts/review-time/1)<br/>▀▀', | ||
'1<br/>▀▀', | ||
'0.25<br/>', | ||
'7<br/>▀▀', | ||
]; | ||
|
||
const ROW1_NO_CHARTS = [ | ||
AVATAR1_SM, | ||
'user1', | ||
'**4**', | ||
'[34m](https://app.flowwer.dev/charts/review-time/1)', | ||
'1', | ||
'0.25', | ||
'7', | ||
]; | ||
|
||
describe('Interactors | .buildMarkdown | .getMarkdownContent', () => { | ||
it('returns the default case data', () => { | ||
const response = getMarkdownContent({ table }); | ||
expect(response.length).toEqual(table.rows.length + 1); | ||
expect(response[0]).toEqual(HEADERS); | ||
expect(response[1]).toEqual(ROW1_SIMPLE); | ||
}); | ||
|
||
it('sets a small avatar size when there a no charts', () => { | ||
const tableCopy = { ...table }; | ||
tableCopy.rows[0].user.emoji = null; | ||
|
||
const response = getMarkdownContent({ table: tableCopy }); | ||
expect(response[1][0]).toEqual(AVATAR1_SM); | ||
expect(response[1][1]).toEqual('user1'); | ||
}); | ||
|
||
it('does not add a character chart when stats have no chart value', () => { | ||
const tableCopy = { ...table }; | ||
tableCopy.rows[0].user.emoji = null; | ||
tableCopy.rows[0].stats = tableCopy.rows[0].stats | ||
.map((stat) => ({ ...stat, chartValue: null })); | ||
|
||
const response = getMarkdownContent({ table: tableCopy }); | ||
expect(response[1]).toEqual(ROW1_NO_CHARTS); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const { table } = require('../../../../tests/mocks'); | ||
const buildTable = require('../index'); | ||
|
||
const EXPECTED_RESPONSE = `| | User | Total reviews | Time to review | Total comments | Comments per review | Opened PRs | | ||
| ----------------------------------------------------------------------------------------------------------- | ------------ | ------------------ | ------------------------------------------------------------------ | ------------------ | ------------------- | ------------------- | | ||
| <a href="https://github.com/user1"><img src="https://avatars.githubusercontent.com/u/user1" width="32"></a> | user1<br/>🥇 | **4**<br/>▀▀▀▀▀▀▀▀ | [34m](https://app.flowwer.dev/charts/review-time/1)<br/>▀▀ | 1<br/>▀▀ | 0.25<br/> | 7<br/>▀▀ | | ||
| <a href="https://github.com/user2"><img src="https://avatars.githubusercontent.com/u/user2" width="32"></a> | user2<br/>🥈 | 1<br/>▀▀ | [2h 21m](https://app.flowwer.dev/charts/review-time/2)<br/>▀▀▀▀▀▀▀ | **5**<br/>▀▀▀▀▀▀▀▀ | **5**<br/>▀▀▀▀▀▀▀▀ | 3<br/>▀ | | ||
| <a href="https://github.com/user3"><img src="https://avatars.githubusercontent.com/u/user3" width="32"></a> | user3<br/>🥉 | 0<br/> | [**17m**](https://app.flowwer.dev/charts/review-time/3)<br/>▀ | 0<br/> | 1<br/>▀▀ | **30**<br/>▀▀▀▀▀▀▀▀ |`; | ||
|
||
describe('Interactors | .buildTable', () => { | ||
it('builds a formatted markdown table', () => { | ||
const response = buildTable({ table }); | ||
expect(response).toEqual(EXPECTED_RESPONSE); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const { isNil } = require('../../utils'); | ||
|
||
const EMOJIS_MAP = { | ||
medal1: String.fromCodePoint(0x1F947), /* 🥇 */ | ||
medal2: String.fromCodePoint(0x1F948), /* 🥈 */ | ||
medal3: String.fromCodePoint(0x1F949), /* 🥉 */ | ||
}; | ||
|
||
const AVATAR_SIZE = { | ||
SMALL: 20, | ||
LARGE: 32, | ||
}; | ||
|
||
const NEW_LINE = '<br/>'; | ||
|
||
const CHART_CHARACTER = '▀'; | ||
|
||
const CHART_MAX_LENGTH = 10; | ||
|
||
const generateChart = (percentage = 0) => { | ||
const length = Math.round(percentage * CHART_MAX_LENGTH); | ||
const chart = Array(length).fill(CHART_CHARACTER).join(''); | ||
return `${NEW_LINE}${chart}`; | ||
}; | ||
|
||
const buildLink = (href, content) => `<a href="${href}">${content}</a>`; | ||
|
||
const buildImage = (src, width) => `<img src="${src}" width="${width}">`; | ||
|
||
const markdownBold = (value) => `**${value}**`; | ||
|
||
const markdownLink = (text, link) => `[${text}](${link})`; | ||
|
||
const buildHeader = ({ text }) => (text); | ||
|
||
const buildHeaders = ({ headers }) => [ | ||
'', // Empty header for the avatar | ||
...headers.map(buildHeader), | ||
]; | ||
|
||
const buildAvatar = ({ image, link, avatarSize }) => buildLink(link, buildImage(image, avatarSize)); | ||
|
||
const buildUsername = ({ text, emoji }) => (emoji ? `${text}${NEW_LINE}${EMOJIS_MAP[emoji]}` : text); | ||
|
||
const buildStat = ({ | ||
text, | ||
link, | ||
chartValue, | ||
bold, | ||
}) => { | ||
const bolded = bold ? markdownBold(text) : text; | ||
const linked = link ? markdownLink(bolded, link) : bolded; | ||
const chart = isNil(chartValue) ? '' : generateChart(chartValue); | ||
return `${linked}${chart}`; | ||
}; | ||
|
||
const buildRows = ({ table, avatarSize }) => table.rows.map((row) => [ | ||
buildAvatar({ ...row.user, avatarSize }), | ||
buildUsername(row.user), | ||
...row.stats.map((stat) => buildStat(stat)), | ||
]); | ||
|
||
module.exports = ({ | ||
table, | ||
}) => { | ||
const firstUser = table.rows[0].user; | ||
const avatarSize = firstUser.emoji ? AVATAR_SIZE.LARGE : AVATAR_SIZE.SMALL; | ||
|
||
const headers = buildHeaders(table); | ||
const rows = buildRows({ table, avatarSize }); | ||
|
||
return [ | ||
headers, | ||
...rows, | ||
]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const toMarkdownTable = require('markdown-table'); | ||
const getMarkdownContent = require('./getMarkdownContent'); | ||
|
||
module.exports = ({ table }) => { | ||
const content = getMarkdownContent({ table }); | ||
return toMarkdownTable(content); | ||
}; |