Skip to content

Commit 3a2f7e8

Browse files
authored
feat: Exclude branches configuration (#40)
* 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
1 parent 33a0d82 commit 3a2f7e8

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ LABEL "com.github.actions.color"="red"
77

88
LABEL "repository"="https://github.com/SvanBoxel/delete-merged-branch"
99
LABEL "homepage"="https://github.com/SvanBoxel"
10-
LABEL "maintainer"="[email protected]"
10+
LABEL "maintainer"="[email protected]"
11+
1112

1213
COPY ./lib /delete-merged-branch-action
1314
COPY ./package.json /delete-merged-branch-action/package.json
1415
COPY ./entrypoint.sh /delete-merged-branch-action/entrypoint.sh
1516

16-
ENTRYPOINT ["/delete-merged-branch-action/entrypoint.sh"]
17+
ENV PATH=$PATH:/app/node_modules/.bin
18+
19+
WORKDIR /app
20+
COPY . .
21+
RUN npm install --production
22+
ENTRYPOINT ["probot", "receive"]
23+
CMD ["/app/index.js", "-p", "../workflow/event.json"]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ npm start
3333
## How it works
3434
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.
3535

36+
## Configuration
37+
The optional app configuration YAML file should be saved as `.github/delete-merged-branch-config.yml`. At the moment it supports the following options:
38+
39+
- `exclude` _(array)_ - list of branches that should not be automatically deleted after a merge.
40+
41+
Example `.github/delete-merged-branch-config.yml`:
42+
43+
```
44+
exclude:
45+
- development
46+
- qa
47+
```
48+
3649
## Release process
3750
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.
3851

entrypoint.sh

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/action.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

lib/delete-merged-branch.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
module.exports = async (context) => {
2+
const config = await context.config('delete-merged-branch-config.yml', { exclude: [] })
23
const headRepoId = context.payload.pull_request.head.repo.id
34
const baseRepoId = context.payload.pull_request.base.repo.id
45

56
const owner = context.payload.repository.owner.login
67
const repo = context.payload.repository.name
7-
const ref = `heads/${context.payload.pull_request.head.ref}`
8+
const branchName = context.payload.pull_request.head.ref
9+
const ref = `heads/${branchName}`
810

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

16+
if (config.exclude.includes(branchName)) {
17+
context.log.info(`Branch ${branchName} excluded. Keeping ${context.payload.pull_request.head.label}`)
18+
return
19+
}
20+
1421
if (!context.payload.pull_request.merged) {
1522
context.log.info(`PR was closed but not merged. Keeping ${owner}/${repo}/${ref}`)
1623
return

test/lib/delete-merged-branch.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe('deleteMergedBranch function', () => {
1111
beforeEach(() => {
1212
deleteReference = jest.fn().mockReturnValue(Promise.resolve())
1313
context = {
14+
config: jest.fn((_, defaults) => defaults),
1415
log: {
1516
info: jest.fn(),
1617
warn: jest.fn()
@@ -47,6 +48,22 @@ describe('deleteMergedBranch function', () => {
4748
})
4849
})
4950

51+
describe('branch is excluded in config', () => {
52+
beforeEach(async () => {
53+
context.config = jest.fn().mockReturnValue({ exclude: [context.payload.pull_request.head.ref] })
54+
context.payload.pull_request.head.label = 'foo:bar'
55+
await deleteMergedBranch(context)
56+
})
57+
58+
it('should log it didn\'t delete the branch', () => {
59+
expect(context.log.info).toBeCalledWith(`Branch ${context.payload.pull_request.head.ref} excluded. Keeping ${context.payload.pull_request.head.label}`)
60+
})
61+
62+
it('should NOT call the deleteReference method', () => {
63+
expect(context.github.gitdata.deleteReference).not.toHaveBeenCalled()
64+
})
65+
})
66+
5067
describe('branch is merged', async () => {
5168
beforeEach(async () => {
5269
context.payload.pull_request.merged = true

0 commit comments

Comments
 (0)