Skip to content

Commit

Permalink
Added milestone property to GitHub issue details service
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWert committed Apr 20, 2022
1 parent 560d267 commit ccd1a4b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
29 changes: 29 additions & 0 deletions services/github/github-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,39 @@ function transformErrors(errors, entity = 'repo') {

const commentsColor = colorScale([1, 3, 10, 25], undefined, true)

const randomColors = colorScale(
[1, 3, 4, 5, 6, 7, 8, 9, 10],
[
'firebrick',
'forestgreen',
'goldenrod',
'darkmagenta',
'darkslateblue',
'indianred',
'darkolivegreen',
'cornflowerblue',
'darkslategray',
'lightseagreen',
],
false
)

function hashCode(s) {
return s.split('').reduce((hash, character) => {
hash = (hash << 5) - hash + character.charCodeAt(0)
return hash & hash
}, 0)
}

function colorForString(s) {
return randomColors(hashCode(s))
}

export {
documentation,
issueStateColor,
commentsColor,
errorMessagesFor,
transformErrors,
colorForString,
}
21 changes: 21 additions & 0 deletions services/github/github-issue-detail.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
errorMessagesFor,
issueStateColor,
commentsColor,
colorForString,
} from './github-helpers.js'

const commonSchemaFields = {
Expand Down Expand Up @@ -140,6 +141,24 @@ const ageUpdateMap = {
}),
}

const milestoneMap = {
schema: Joi.object({
...commonSchemaFields,
milestone: Joi.object({
title: Joi.string().required(),
}).allow(null),
}).required(),
transform: ({ json }) => (json.milestone ? json.milestone.title : '---'),
render: ({ value }) => {
const color = value === '---' ? 'grey' : colorForString(value.milestone)
return {
label: 'milestone',
message: value,
color: color,
}
},
}

const propertyMap = {
state: stateMap,
title: titleMap,
Expand All @@ -148,6 +167,7 @@ const propertyMap = {
comments: commentsMap,
age: ageUpdateMap,
'last-update': ageUpdateMap,
milestone: milestoneMap,
}

export default class GithubIssueDetail extends GithubAuthV3Service {
Expand Down Expand Up @@ -182,6 +202,7 @@ export default class GithubIssueDetail extends GithubAuthV3Service {
'comments',
'age',
'last update',
'milestone',
],
documentation,
},
Expand Down
26 changes: 25 additions & 1 deletion services/github/github-issue-detail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { age } from '../color-formatters.js'
import { formatDate, metric } from '../text-formatters.js'
import { InvalidResponse } from '../index.js'
import GithubIssueDetail from './github-issue-detail.service.js'
import { issueStateColor, commentsColor } from './github-helpers.js'
import {
issueStateColor,
commentsColor,
colorForString,
} from './github-helpers.js'

describe('GithubIssueDetail', function () {
test(GithubIssueDetail.render, () => {
Expand Down Expand Up @@ -90,6 +94,14 @@ describe('GithubIssueDetail', function () {
message: formatDate('2019-04-02T20:09:31Z'),
color: age('2019-04-02T20:09:31Z'),
})
given({
property: 'milestone',
value: 'MS 1',
}).expect({
label: 'milestone',
message: 'MS 1',
color: colorForString('MS 1'),
})
})

test(GithubIssueDetail.prototype.transform, () => {
Expand Down Expand Up @@ -178,6 +190,18 @@ describe('GithubIssueDetail', function () {
value: '2019-04-02T20:09:31Z',
isPR: false,
})
given({
property: 'milestone',
json: { milestone: { title: 'MS 1' } },
}).expect({
value: 'MS 1',
})
given({
property: 'milestone',
json: { milestone: null },
}).expect({
value: '---',
})
})

context('transform()', function () {
Expand Down
14 changes: 14 additions & 0 deletions services/github/github-issue-detail.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,17 @@ t.create('github pull request merge state (pull request not found)')
label: 'issue/pull request',
message: 'issue, pull request or repo not found',
})

t.create('github issue milestone')
.get('/pulls/detail/milestone/badges/shields/4949.json')
.expectBadge({
label: 'milestone',
message: 'badge-maker v3.4',
})

t.create('github issue milestone (without milestone)')
.get('/pulls/detail/milestone/badges/shields/979.json')
.expectBadge({
label: 'milestone',
message: '---',
})

0 comments on commit ccd1a4b

Please sign in to comment.