Skip to content

Commit

Permalink
add support for moving uncategorized to a category with no labels (#1013
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jetersen committed Jan 16, 2022
1 parent d51d5d1 commit 935947e
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
31 changes: 29 additions & 2 deletions dist/index.js
Expand Up @@ -133525,14 +133525,24 @@ const categorizePullRequests = (pullRequests, config) => {
return { ...category, pullRequests: [] }
})

const uncategorizedCategoryIndex = categories.findIndex(
(category) => category.labels.length === 0
)

const filterUncategorizedPullRequests = (pullRequest) => {
const labels = pullRequest.labels.nodes

if (
labels.length === 0 ||
!labels.some((label) => allCategoryLabels.has(label.name))
) {
uncategorizedPullRequests.push(pullRequest)
if (uncategorizedCategoryIndex === -1) {
uncategorizedPullRequests.push(pullRequest)
} else {
categorizedPullRequests[uncategorizedCategoryIndex].pullRequests.push(
pullRequest
)
}
return false
}
return true
Expand Down Expand Up @@ -133769,7 +133779,11 @@ const _ = __nccwpck_require__(90250)
const Joi = __nccwpck_require__(44010)
const { SORT_BY, SORT_DIRECTIONS } = __nccwpck_require__(11940)
const { DEFAULT_CONFIG } = __nccwpck_require__(85869)
const { validateReplacers, validateAutolabeler } = __nccwpck_require__(47282)
const {
validateReplacers,
validateAutolabeler,
validateCategories,
} = __nccwpck_require__(47282)
const merge = __nccwpck_require__(56323)

const schema = (context) => {
Expand Down Expand Up @@ -133918,6 +133932,8 @@ const validateSchema = (context, repoConfig) => {

if (error) throw error

validateCategories({ categories: config.categories })

try {
config.replacers = validateReplacers({
context,
Expand Down Expand Up @@ -134079,9 +134095,20 @@ function validateAutolabeler({ context, autolabeler }) {
.filter(Boolean)
}

function validateCategories({ categories }) {
if (
categories.filter((category) => category.labels.length === 0).length > 1
) {
throw new Error(
'Multiple categories detected with no labels.\nOnly one category with no labels is supported for uncategorized pull requests.'
)
}
}

exports.template = template
exports.validateReplacers = validateReplacers
exports.validateAutolabeler = validateAutolabeler
exports.validateCategories = validateCategories


/***/ }),
Expand Down
12 changes: 11 additions & 1 deletion lib/releases.js
Expand Up @@ -129,14 +129,24 @@ const categorizePullRequests = (pullRequests, config) => {
return { ...category, pullRequests: [] }
})

const uncategorizedCategoryIndex = categories.findIndex(
(category) => category.labels.length === 0
)

const filterUncategorizedPullRequests = (pullRequest) => {
const labels = pullRequest.labels.nodes

if (
labels.length === 0 ||
!labels.some((label) => allCategoryLabels.has(label.name))
) {
uncategorizedPullRequests.push(pullRequest)
if (uncategorizedCategoryIndex === -1) {
uncategorizedPullRequests.push(pullRequest)
} else {
categorizedPullRequests[uncategorizedCategoryIndex].pullRequests.push(
pullRequest
)
}
return false
}
return true
Expand Down
8 changes: 7 additions & 1 deletion lib/schema.js
Expand Up @@ -2,7 +2,11 @@ const _ = require('lodash')
const Joi = require('@hapi/joi')
const { SORT_BY, SORT_DIRECTIONS } = require('./sort-pull-requests')
const { DEFAULT_CONFIG } = require('./default-config')
const { validateReplacers, validateAutolabeler } = require('./template')
const {
validateReplacers,
validateAutolabeler,
validateCategories,
} = require('./template')
const merge = require('deepmerge')

const schema = (context) => {
Expand Down Expand Up @@ -151,6 +155,8 @@ const validateSchema = (context, repoConfig) => {

if (error) throw error

validateCategories({ categories: config.categories })

try {
config.replacers = validateReplacers({
context,
Expand Down
11 changes: 11 additions & 0 deletions lib/template.js
Expand Up @@ -76,6 +76,17 @@ function validateAutolabeler({ context, autolabeler }) {
.filter(Boolean)
}

function validateCategories({ categories }) {
if (
categories.filter((category) => category.labels.length === 0).length > 1
) {
throw new Error(
'Multiple categories detected with no labels.\nOnly one category with no labels is supported for uncategorized pull requests.'
)
}
}

exports.template = template
exports.validateReplacers = validateReplacers
exports.validateAutolabeler = validateAutolabeler
exports.validateCategories = validateCategories
@@ -0,0 +1,13 @@
template: |
# What's Changed
$CHANGES
categories:
- title: 🚀 Features
labels:
- feature
- title: 🐛 Bug Fixes
labels:
- fix
- title: 📝 Other Changes
56 changes: 56 additions & 0 deletions test/index.test.js
Expand Up @@ -916,6 +916,62 @@ describe('release-drafter', () => {
expect.assertions(1)
})

it('categorizes pull requests with other category at the bottom', async () => {
getConfigMock('config-with-categories-with-other-category.yml')

nock('https://api.github.com')
.get('/repos/toolmantim/release-drafter-test-project/releases')
.query(true)
.reply(200, [releasePayload])

nock('https://api.github.com')
.post('/graphql', (body) =>
body.query.includes('query findCommitsWithAssociatedPullRequests')
)
.reply(200, graphqlCommitsMergeCommit)

nock('https://api.github.com')
.post(
'/repos/toolmantim/release-drafter-test-project/releases',
(body) => {
expect(body).toMatchInlineSnapshot(`
Object {
"body": "# What's Changed
## 🚀 Features
* Add big feature (#2) @TimonVS
* 👽 Add alien technology (#1) @TimonVS
## 🐛 Bug Fixes
* Bug fixes (#3) @TimonVS
## 📝 Other Changes
* Add documentation (#5) @TimonVS
* Update dependencies (#4) @TimonVS
",
"draft": true,
"name": "",
"prerelease": false,
"tag_name": "",
"target_commitish": "",
}
`)
return true
}
)
.reply(200, releasePayload)

await probot.receive({
name: 'push',
payload: pushPayload,
})

expect.assertions(1)
})

it('categorizes pull requests with multiple labels', async () => {
getConfigMock('config-with-categories-2.yml')

Expand Down
35 changes: 34 additions & 1 deletion test/schema.test.js
@@ -1,4 +1,4 @@
const { schema } = require('../lib/schema')
const { schema, validateSchema } = require('../lib/schema')
const schemaJson = require('../schema.json')
const { jsonSchema } = require('../bin/generate-schema')

Expand Down Expand Up @@ -73,4 +73,37 @@ describe('schema', () => {
it('current schema matches the generated JSON Schema, update schema with `yarn generate-schema`', () => {
expect(jsonSchema).toMatchObject(schemaJson)
})

describe('validateSchema', () => {
it('Multiple other categories', () => {
expect(() => {
validateSchema(context, {
template,
categories: [
{
title: '📝 Other Changes',
},
{
title: '📝 Yet Other Changes',
},
],
})
}).toThrowErrorMatchingInlineSnapshot(`
"Multiple categories detected with no labels.
Only one category with no labels is supported for uncategorized pull requests."
`)
})

it('Single other categories', () => {
const expected = {
template,
categories: [
{
title: '📝 Other Changes',
},
],
}
expect(validateSchema(context, expected)).toMatchObject(expected)
})
})
})

0 comments on commit 935947e

Please sign in to comment.