Skip to content

Commit

Permalink
feat: Exclude branches configuration (#40)
Browse files Browse the repository at this point in the history
* Added exclude branch option

* Refactored action

* Different workdir

* removed app dir from env

* Reintroduce app dir

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Update Dockerfile

* Delete entrypoint.sh

* Update README.md

* Update README.md

* Delete action.js

* Update Dockerfile
  • Loading branch information
SvanBoxel authored Nov 16, 2018
1 parent 33a0d82 commit 3a2f7e8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ LABEL "com.github.actions.color"="red"

LABEL "repository"="https://github.com/SvanBoxel/delete-merged-branch"
LABEL "homepage"="https://github.com/SvanBoxel"
LABEL "maintainer"="[email protected]"
LABEL "maintainer"="[email protected]"


COPY ./lib /delete-merged-branch-action
COPY ./package.json /delete-merged-branch-action/package.json
COPY ./entrypoint.sh /delete-merged-branch-action/entrypoint.sh

ENTRYPOINT ["/delete-merged-branch-action/entrypoint.sh"]
ENV PATH=$PATH:/app/node_modules/.bin

WORKDIR /app
COPY . .
RUN npm install --production
ENTRYPOINT ["probot", "receive"]
CMD ["/app/index.js", "-p", "../workflow/event.json"]
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ npm start
## How it works
This GitHub app listens to the `pull_request.closed` webhook. If a pull request is closed and the connected branch is merged, it will delete the branch.

## Configuration
The optional app configuration YAML file should be saved as `.github/delete-merged-branch-config.yml`. At the moment it supports the following options:

- `exclude` _(array)_ - list of branches that should not be automatically deleted after a merge.

Example `.github/delete-merged-branch-config.yml`:

```
exclude:
- development
- qa
```

## Release process
CI (Travis) is in charge of releasing new versions of the GitHub Application to [Now](https://zeit.co/now). On every new commit to master we run [semantic-release](https://github.com/semantic-release/semantic-release) to determine whether the major/minor/patch version should be incremented. If so, we update the version running in production.

Expand Down
4 changes: 0 additions & 4 deletions entrypoint.sh

This file was deleted.

27 changes: 0 additions & 27 deletions lib/action.js

This file was deleted.

9 changes: 8 additions & 1 deletion lib/delete-merged-branch.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
module.exports = async (context) => {
const config = await context.config('delete-merged-branch-config.yml', { exclude: [] })
const headRepoId = context.payload.pull_request.head.repo.id
const baseRepoId = context.payload.pull_request.base.repo.id

const owner = context.payload.repository.owner.login
const repo = context.payload.repository.name
const ref = `heads/${context.payload.pull_request.head.ref}`
const branchName = context.payload.pull_request.head.ref
const ref = `heads/${branchName}`

if (headRepoId !== baseRepoId) {
context.log.info(`Closing PR from fork. Keeping ${context.payload.pull_request.head.label}`)
return
}

if (config.exclude.includes(branchName)) {
context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`)
return
}

if (!context.payload.pull_request.merged) {
context.log.info(`PR was closed but not merged. Keeping ${owner}/${repo}/${ref}`)
return
Expand Down
17 changes: 17 additions & 0 deletions test/lib/delete-merged-branch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('deleteMergedBranch function', () => {
beforeEach(() => {
deleteReference = jest.fn().mockReturnValue(Promise.resolve())
context = {
config: jest.fn((_, defaults) => defaults),
log: {
info: jest.fn(),
warn: jest.fn()
Expand Down Expand Up @@ -47,6 +48,22 @@ describe('deleteMergedBranch function', () => {
})
})

describe('branch is excluded in config', () => {
beforeEach(async () => {
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
context.payload.pull_request.head.label = 'foo:bar'
await deleteMergedBranch(context)
})

it('should log it didn\'t delete the branch', () => {
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
})

it('should NOT call the deleteReference method', () => {
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
})
})

describe('branch is merged', async () => {
beforeEach(async () => {
context.payload.pull_request.merged = true
Expand Down

0 comments on commit 3a2f7e8

Please sign in to comment.