Skip to content

Commit

Permalink
Add header and footer (#1050)
Browse files Browse the repository at this point in the history
* Add header and footer

* Add tests for header and footer
  • Loading branch information
mkurz committed Feb 1, 2022
1 parent db60e03 commit a8445e8
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -109,6 +109,8 @@ You can configure Release Drafter using the following key in your `.github/relea
| Key | Required | Description |
| -------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `template` | Required | The template for the body of the draft release. Use [template variables](#template-variables) to insert values. |
| `header` | Optional | Will be prepended to `template`. Use [template variables](#template-variables) to insert values. |
| `footer` | Optional | Will be appended to `template`. Use [template variables](#template-variables) to insert values. |
| `category-template` | Optional | The template to use for each category. Use [category template variables](#category-template-variables) to insert values. Default: `"## $TITLE"`. |
| `name-template` | Optional | The template for the name of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
| `tag-template` | Optional | The template for the tag of the draft release. For example: `"v$NEXT_PATCH_VERSION"`. |
Expand All @@ -134,7 +136,7 @@ Release Drafter also supports [Probot Config](https://github.com/probot/probot-c

## Template Variables

You can use any of the following variables in your `template`:
You can use any of the following variables in your `template`, `header` and `footer`:

| Variable | Description |
| --------------- | --------------------------------------------------------------------------------------------------------------------- |
Expand All @@ -154,7 +156,7 @@ You can use any of the following variables in `category-template`:

## Next Version Variables

You can use any of the following variables in your `template`, `name-template` and `tag-template`:
You can use any of the following variables in your `template`, `header`, `footer`, `name-template` and `tag-template`:

| Variable | Description |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
Expand Down
7 changes: 6 additions & 1 deletion dist/index.js
Expand Up @@ -129311,7 +129311,8 @@ const generateReleaseInfo = ({
}) => {
const { owner, repo } = context.repo()

let body = config.template
let body =
(config['header'] || '') + config.template + (config['footer'] || '')

body = template(
body,
Expand Down Expand Up @@ -129575,8 +129576,12 @@ const schema = (context) => {
.allow('')
.default(DEFAULT_CONFIG['category-template']),

header: Joi.string(),

template: Joi.string().required(),

footer: Joi.string(),

_extends: Joi.string(),
})
.rename('branches', 'references', {
Expand Down
3 changes: 2 additions & 1 deletion lib/releases.js
Expand Up @@ -273,7 +273,8 @@ const generateReleaseInfo = ({
}) => {
const { owner, repo } = context.repo()

let body = config.template
let body =
(config['header'] || '') + config.template + (config['footer'] || '')

body = template(
body,
Expand Down
4 changes: 4 additions & 0 deletions lib/schema.js
Expand Up @@ -147,8 +147,12 @@ const schema = (context) => {
.allow('')
.default(DEFAULT_CONFIG['category-template']),

header: Joi.string(),

template: Joi.string().required(),

footer: Joi.string(),

_extends: Joi.string(),
})
.rename('branches', 'references', {
Expand Down
6 changes: 6 additions & 0 deletions schema.json
Expand Up @@ -286,9 +286,15 @@
}
]
},
"header": {
"type": "string"
},
"template": {
"type": "string"
},
"footer": {
"type": "string"
},
"_extends": {
"type": "string"
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/config/config-with-footer-template.yml
@@ -0,0 +1,5 @@
template: |
This is the template in the middle
footer: |
This is at bottom
@@ -0,0 +1,3 @@
template: This is the template in the middle
header: This is at top
footer: This is at bottom
@@ -0,0 +1,8 @@
template: |
This is the template in the middle
header: |
This is at top
footer: |
This is at bottom
5 changes: 5 additions & 0 deletions test/fixtures/config/config-with-header-template.yml
@@ -0,0 +1,5 @@
template: |
This is the template in the middle
header: |
This is at top
172 changes: 172 additions & 0 deletions test/index.test.js
Expand Up @@ -1426,6 +1426,178 @@ describe('release-drafter', () => {
})
})

describe('with header and footer config', () => {
it('only header', async () => {
getConfigMock('config-with-header-template.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": "This is at top
This is the template in the middle
",
"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('only footer', async () => {
getConfigMock('config-with-footer-template.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": "This is the template in the middle
This is at bottom
",
"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('header and footer', async () => {
getConfigMock('config-with-header-and-footer-template.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": "This is at top
This is the template in the middle
This is at bottom
",
"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('header and footer without line break and without space', async () => {
getConfigMock(
'config-with-header-and-footer-no-nl-no-space-template.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": "This is at topThis is the template in the middleThis is at bottom",
"draft": true,
"name": "",
"prerelease": false,
"tag_name": "",
"target_commitish": "",
}
`)
return true
}
)
.reply(200, releasePayload)

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

expect.assertions(1)
})
})

describe('merging strategies', () => {
describe('merge commit', () => {
it('sets $CHANGES based on all commits', async () => {
Expand Down
13 changes: 13 additions & 0 deletions test/schema.test.js
Expand Up @@ -17,6 +17,9 @@ const validConfigs = [
[{ template, replacers: [{ search: '123', replace: '' }] }],
[{ template, replacers: [{ search: '/123/gi', replace: '' }] }],
[{ template, replacers: [{ search: '/123/gi', replace: '123' }] }],
[{ template, header: 'I am on top' }],
[{ template, footer: 'I am on bottm' }],
[{ template, header: 'I am on top', footer: 'I am on bottm' }],
]

const invalidConfigs = [
Expand All @@ -26,6 +29,16 @@ const invalidConfigs = [
[{ template: { '👶': 'a' } }, 'must be a string'],
[{ template: null }, 'must be a string'],
[{ template: '' }, 'is not allowed to be empty'],
[{ header: true }, 'must be a string'],
[{ header: 1 }, 'must be a string'],
[{ header: ['👶'] }, 'must be a string'],
[{ header: { '👶': 'a' } }, 'must be a string'],
[{ header: null }, 'must be a string'],
[{ footer: true }, 'must be a string'],
[{ footer: 1 }, 'must be a string'],
[{ footer: ['👶'] }, 'must be a string'],
[{ footer: { '👶': 'a' } }, 'must be a string'],
[{ footer: null }, 'must be a string'],
[{ 'category-template': ['## $TITLE'] }, 'must be a string'],
[{ 'category-template': null }, 'must be a string'],
[{ 'change-template': ['* $TITLE (#$NUMBER) @$AUTHOR'] }, 'must be a string'],
Expand Down

0 comments on commit a8445e8

Please sign in to comment.