Skip to content

Commit

Permalink
feat: Streamline codebase
Browse files Browse the repository at this point in the history
It is mainly a code cleanup to facilitate maintenance. Some features that did not fit well have been removed.

BREAKING CHANGE: - Renamed environment variable `COMMIT_CONFIG` to `CONFIG`

- Switched to stock `angular` commit-analyzer config (https://github.com/semantic-release/commit-analyzer#configuration)

    To restore the original behaviour, set `CONFIG`to:

    ```
    {
      preset: 'angular',
      releaseRules: [
        { type: 'chore', release: 'patch' }
      ],
      parserOpts: {
        noteKeywords: ['BREAKING CHANGES']
      }
    }
    ```

- Dropped assignment of reviewers

- Removed its own rather useless commit status
  • Loading branch information
scherermichael authored Jun 6, 2020
1 parent b3e28f9 commit 9f39507
Show file tree
Hide file tree
Showing 35 changed files with 643 additions and 948 deletions.
75 changes: 43 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ Squashes and merges your pull requests with a Conventional Commit message.

## Getting started

This GitHub application uses the description of a pull request to create a merge commit with a custom message. Based on such a commit message, it is possible to automatically determine the correct [SemVer](https://semver.org) version of the next release.
This GitHub application uses the title and description of a pull request to create a merge commit. Based on the commit message, it is possible to automatically determine the correct [SemVer](https://semver.org) version of the next release.

It can set the status of the pull request to `error` if there is not enough information to build the commit message, if wished.

The commit message follows the pattern defined by [Conventional Commits](https://conventionalcommits.org) as you can see in the following sample:
The generated commit message follows the pattern defined by [Conventional Commits](https://conventionalcommits.org) as you can see in the following sample:

```
feat: Introducing a cool feature.
The new feature is awesome!
BREAKING CHANGES: You must update the configuration.
BREAKING CHANGE: You must update the configuration.
#1234
```
Expand Down Expand Up @@ -53,7 +51,7 @@ For the sample commit message given above, the complete pull request description
```
# Internal Information
This text will be excluded from the commit message.
This text will be excluded from the commit message because of the unknown heading.
# Details
Expand All @@ -74,7 +72,7 @@ This text will be excluded from the commit message, too.

The level of the headings does not matter. You can increase it if you prefer a smaller text size.

### Merging
### Manual Merging

Unfortunately, you cannot use the `Merge` button with this bot. To create the custom merge commit, create a new comment with the merge command:

Expand All @@ -84,17 +82,29 @@ Unfortunately, you cannot use the `Merge` button with this bot. To create the cu

All commits are squashed and merged using a conventional commit message. If it is not possible to create such a message, the standard message is used.

### Assigning reviewers
You can define a [branch protection](https://help.github.com/en/github/administering-a-repository/configuring-protected-branches) to allow only this app to merge to the master branch. This disables the `Merge` button for all members.

You can define rules to automatically request reviews from users or teams based on the topics and labels set for the pull request.
### Automatic merging

The rules are provided via environment variables `REVIEW_USERS_RULES` and `REVIEW_TEAMS_RULES`.
By setting the environment variable `AUTOMERGE_BRANCHES`, you can specify branches that will be automatically merged once all checks have been successfully completed.

Example:
### Preventing accidental merges

```bash
REVIEW_TEAMS_RULES = "documentation,release/major=+docs-team documentation,release/minor=-docs-team documentation=-docs-team"
```
As long as you are still working on a pull request, you can prefix its title with `WIP:`. This prevents the bot from performing a manual or automatic merge.

### Labels

The bot creates some labels to inform users about the state of the pull request:

- The type of release that will be triggered, e.g. `release/major`. The label consists of a prefix and the type of release.

- The keyword used to specify the kind of changes, e.g. `release/feat`. The label also consists of the prefix and the keyword itself.

- Pull requests that must not be merged since they are still being worked on are labeled with `work-in-progress`.

- If the branch of a pull request is included in the list of branches to be merged automatically, the label `automatic-merge` is displayed.

The text of each label can be changed by setting the appropriate [environment variables](#environment-variables).

## Deployment

Expand All @@ -106,28 +116,29 @@ You can use [serverless](https://serverless.com) to deploy the application. The

### Environment variables

- `COMMIT_CONFIG`: Config for semantic release analyzer
The bot works out of the box with sensible default settings which can be changed by environment variables.

Config used by default:
- `AUTOMERGE_BRANCHES`: Comma-separated list of branch names that will be merged automatically if all checks are ok

- `AUTOMERGE_LABEL` (defaults to `automatic-merge`): Label created for branches that will be merged automatically by the bot

- `CONFIG`: Configuration for [semantic release analyzer](https://github.com/semantic-release/commit-analyzer#configuration)

Defaults to:

```
{
"preset": "angular",
"releaseRules": [
{ "type": "chore", "release": "patch" }
],
"parserOpts": {
"noteKeywords": ["BREAKING CHANGES"]
}
"preset": "angular"
}
```
- `LABEL_PREFIX`: Prefix for all created labels
- `LABEL_SUFFIX_MAJOR`: Suffix of label for major release
- `LABEL_SUFFIX_MINOR`: Suffix of label for minor release
- `LABEL_SUFFIX_PATCH`: Suffix of label for patch release
- `REVIEW_USERS_RULES`: Rule for requesting reviews from users (see [Assigning reviewers](#assigning-reviewers))
- `REVIEW_TEAMS_RULES`: Rule for requesting reviews from teams (see [Assigning reviewers](#assigning-reviewers))
- `AUTOMERGE_BRANCHES`: Comma-separated list of branch names that will be merged automatically if all checks are ok
- `AUTOMERGE_LABEL`: Label for branches that will be merged automatically
- `WIP_LABEL`: Label for unfinished branches that must not be merged
- `LABEL_PREFIX` (defaults to `release/`): Prefix for all created release labels
- `LABEL_SUFFIX_MAJOR` (defaults to `major`): Suffix of label for major release
- `LABEL_SUFFIX_MINOR` (defaults to `minor`): Suffix of label for minor release
- `LABEL_SUFFIX_PATCH` (defaults to `patch`): Suffix of label for patch release
automatically
- `WIP_LABEL` (defaults to `work-in-progress`): Label created for unfinished branches that must not be merged
7 changes: 3 additions & 4 deletions config/config.dev.json.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"APP_ID": 1234,
"GITHUB_CLIENT_ID": "example-github-client-id",
"GITHUB_CLIENT_SECRET": "example-github-client-secret",
"REVIEW_TEAMS_RULES": "my-topic,release/feat=+my-team my-topic,release/fix=+my-team my-topic=-my-team",
"APP_ID": 0000,
"GITHUB_CLIENT_ID": "client-id-by-github",
"GITHUB_CLIENT_SECRET": "client-secret-by-github",
"AUTOMERGE_BRANCHES": "branch1,branch2",
"WEBHOOK_SECRET": "example-webhook-secret",
"memorySize": 256
Expand Down
19 changes: 6 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
'use strict'

const handleCheck = require('./lib/handleCheck')
const handleCommitStatus = require('./lib/handleCommitStatus')
const handleComment = require('./lib/handleComment')
const handlePullRequestChange = require('./lib/handlePullRequestChange')
const status = require('./lib/handlers/status')
const comment = require('./lib/handlers/comment')
const pullRequestChange = require('./lib/handlers/pullRequestChange')

const probotPlugin = (robot) => {
robot.on([
'pull_request.edited',
'pull_request.labeled',
'pull_request.unlabeled',
'pull_request.opened',
'pull_request.reopened'
], handlePullRequestChange)
], pullRequestChange)
robot.on([
'issue_comment.created'
], handleComment)
robot.on([
'check_run.completed',
'check_suite.completed'
], handleCheck)
], comment)
robot.on([
'status'
], handleCommitStatus)
], status)
}

module.exports = probotPlugin
28 changes: 0 additions & 28 deletions lib/areAllChecksSuccessful.js

This file was deleted.

16 changes: 12 additions & 4 deletions lib/extractMessage.js → lib/buildCommitMessage.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict'

const debug = require('debug')('app:extractMessage')
const debug = require('debug')('app:buildCommitMessage')

const extractMessage = function (title, description) {
const config = require('./config')

const buildCommitMessage = function (title, description) {
debug('Start')

// Get keyword for breaking changes from config
const noteKeyword = (config.parserOpts && config.parserOpts.noteKeywords)
// noteKeywords can be string or array
? Array.isArray(config.parserOpts.noteKeywords) ? config.parserOpts.noteKeywords[0] : config.parserOpts.noteKeywords
: 'BREAKING CHANGE'

const message = {
title: title.trim(),
body: '',
Expand All @@ -18,7 +26,7 @@ const extractMessage = function (title, description) {
details = details ? details[1].trim() : ''
if (breaking) {
// Add BREAKING CHANGE only if there is more text
breaking = breaking[1].trim() === '' ? '' : `BREAKING CHANGES: ${breaking[1].trim()}`
breaking = breaking[1].trim() === '' ? '' : `${noteKeyword}: ${breaking[1].trim()}`
} else {
breaking = ''
}
Expand All @@ -33,4 +41,4 @@ const extractMessage = function (title, description) {
return message
}

module.exports = extractMessage
module.exports = buildCommitMessage
69 changes: 69 additions & 0 deletions lib/buildResultMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const debug = require('debug')('app:buildResultMessage')

const getMergeComment = require('./merge/getMergeComment')
const getReleaseType = require('./getReleaseType')

const getSuccessMessage = async function ({ commitMessage }) {
debug('composeSuccessMessage')

const releaseType = await getReleaseType(commitMessage.full)
const successComment = releaseType ? [
'🎉 Pull Request Merged 🎉',
'',
'This pull request has been merged with the following commit message:',
'',
'``````',
commitMessage.full,
'``````',
'',
`A **${releaseType}** release is triggered.`
] : [
'🎉 Pull Request Merged 🎉',
'',
'No type of pull request detected. Therefore, no release is triggered.'
]

return successComment.join('\n')
}

const getErrorMessage = async function ({ context, exception }) {
debug('composeErrorMessage')

let errorMessage
try {
errorMessage = JSON.parse(exception.message).message
} catch (exJson) {
errorMessage = exception.message
}

const comment = await getMergeComment(context)
let deleteNote
if (comment) {
deleteNote = `Delete the [/merge comment](${comment.html_url}) to stop.`
}

return [
'⚠️ Merging Pull Request Failed ⚠️',
'',
errorMessage,
'',
`Merge will be **automatically retried** on any change. ${deleteNote}`
].join('\n')
}

const buildResultMessage = async function ({ context, exception = null, commitMessage = null }) {
debug('Start')

let message = ''
if (exception) {
message = await getErrorMessage({ context, exception })
} else {
message = await getSuccessMessage({ context, commitMessage })
}

return message
}

module.exports = buildResultMessage
7 changes: 7 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

const getenv = require('getenv')

const config = JSON.parse(getenv('CONFIG', '{ "preset": "angular" }'))

module.exports = config
16 changes: 1 addition & 15 deletions lib/getReleaseType.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,8 @@
const debug = require('debug')('app:getReleaseType')

const commitAnalyzer = require('@semantic-release/commit-analyzer')
const getenv = require('getenv')

let config = {
preset: 'angular',
releaseRules: [
{ type: 'chore', release: 'patch' }
],
parserOpts: {
noteKeywords: ['BREAKING CHANGES']
}
}

const configEnv = getenv('COMMIT_CONFIG', '')
if (configEnv !== '') {
config = JSON.parse(configEnv)
}
const config = require('./config')

const getReleaseType = async function (message = '') {
debug('Start')
Expand Down
42 changes: 0 additions & 42 deletions lib/handleCheck.js

This file was deleted.

Loading

0 comments on commit 9f39507

Please sign in to comment.