Skip to content

Commit

Permalink
feat(democrat): Add comments on merge and pr opening (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
deuzu authored Apr 11, 2021
1 parent 65925e8 commit 5a67101
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 8 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ jobs:
- run: npm run format-check
- run: npm run lint
- run: npm run test
- run: npm run build
- run: npm run package
action:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npm run all
- run: npm run build
- run: npm run package
- uses: ./
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
dryRun: true
- uses: ./
with:
githubToken: ${{ secrets.GITHUB_TOKEN }}
voters: deuzu
dryRun: true
prMinimumReviewScore: 1
prMaturity: 24
prVotingTimeHours: 24
prMarkAsMegeableLabel: ready
prTargetBranch: main
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ name: 'democracy-enforcer'

on:
schedule:
- cron: '*/30 * * * *'
- cron: '*/30 * * * *' # checks pull requests to merge every 30 minutes
pull_request:
types:
- opened # for a voting opening message on a pull request

jobs:
enforce-democracy:
Expand Down
6 changes: 6 additions & 0 deletions __tests__/democrat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Democrat', () => {
],
}))
const pullMergeMock = jest.fn()
const pullMergeCommentMock = jest.fn()

const octokitMock: any = mocked(getOctokit, true)
octokitMock.mockImplementation(() => ({
Expand All @@ -33,6 +34,9 @@ describe('Democrat', () => {
listReviews: pullListReviewsMock,
merge: pullMergeMock,
},
issues: {
createComment: pullMergeCommentMock,
},
}))

beforeEach(() => {
Expand Down Expand Up @@ -60,6 +64,7 @@ describe('Democrat', () => {
expect(pullListMock).toHaveBeenCalledTimes(1)
expect(pullGetMock).toHaveBeenCalledTimes(1)
expect(pullListReviewsMock).toHaveBeenCalledTimes(1)
expect(pullMergeCommentMock).toHaveBeenCalledTimes(1)
expect(pullMergeMock).toHaveBeenCalledTimes(1)
})

Expand All @@ -84,6 +89,7 @@ describe('Democrat', () => {
expect(pullListMock).toHaveBeenCalledTimes(1)
expect(pullGetMock).toHaveBeenCalledTimes(1)
expect(pullListReviewsMock).toHaveBeenCalledTimes(1)
expect(pullMergeCommentMock).toHaveBeenCalledTimes(0)
expect(pullMergeMock).toHaveBeenCalledTimes(0)
})
})
49 changes: 48 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions src/democrat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ export default class Democrat {
this.octokit = github.getOctokit(democratParameters.token)
}

async votingOpening(pullNumber: number): Promise<void> {
const { owner, repo, voters, dryRun } = this.democratParameters
const { targetBranch, markAsMergeableLabel, votingTimeHours, minimumReviewScore } = this.pullRequestParameters
const votersLink = voters.map((voter) => `[${voter}](https://github.com/${voter})`)
const body = `
## The Github Democrat is now taking care of this pull request. Voting is open!
To be eligible for merge, the pull request must:
- be mergeable (no conflicts)
- target the branch \`${targetBranch}\`
- have a \`${markAsMergeableLabel}\` label
- have been unmodified for \`${votingTimeHours}h\`
- have a review score of \`${minimumReviewScore}\` or more (approves +1 & request changes -1)
Allowed voters are: ${voters.length > 0 ? votersLink.join(', ') : ':open_hands: everyone :open_hands:'}
`

if (dryRun) {
this.logger(
'info',
`Dry-run enabled. The following comment will not be created one the pull request #${pullNumber}: ${body}`
)

return
}

this.octokit.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body,
})
}

async enforceDemocracy(): Promise<void> {
const { owner, repo } = this.democratParameters
this.logger('info', `Implementing democracy on ${owner}/${repo}, resistance is futile.`)
Expand Down Expand Up @@ -199,6 +233,7 @@ export default class Democrat {
return new Promise((resolve) => resolve(undefined))
}

await this.commentMergePull(pull)
await this.octokit.pulls.merge({
owner,
repo,
Expand All @@ -208,4 +243,16 @@ export default class Democrat {

return this.logger('info', `Pull Request #${pull.number} merged.`)
}

private async commentMergePull(pull: PullCandidate): Promise<void> {
const { owner, repo } = this.democratParameters
const body = `Democracy has spoken, the pull request will be merged.`

this.octokit.issues.createComment({
owner,
repo,
issue_number: pull.number,
body,
})
}
}
13 changes: 12 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ dotenv.config()
async function run(): Promise<void> {
try {
const context = github.context
const {
eventName,
payload: { action, number },
} = context
const [owner, repo] = (context.payload.repository?.full_name || process.env.GITHUB_REPOSITORY || '/').split('/')
const voters = core
.getInput('voter')
.getInput('voters')
.split(',')
.map((voter: string): string => voter.trim())
.filter((voter: string): boolean => !!voter)
Expand All @@ -37,6 +41,13 @@ async function run(): Promise<void> {
}

const democrat = new Democrat(democratParameters, pullRequestParameters)

if ('pull_request' === eventName && 'opened' === action) {
await democrat.votingOpening(number)

return
}

await democrat.enforceDemocracy()
} catch (error) {
core.setFailed(error.message)
Expand Down

0 comments on commit 5a67101

Please sign in to comment.