Skip to content

Commit

Permalink
feat: Require app slug in config
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrady committed Jul 23, 2022
1 parent f6a4970 commit afcdab0
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 23 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ In order to use the bot, the config file should be provided. Config file should

---

### registered_app_slug
**mandatory**

The 'slugged' name you gave to [the GitHub App you registered](https://docs.github.com/en/developers/apps/building-github-apps/creating-a-github-app) this code under. For example, if you named your app "PR Approver", the slugged version would be `pr-approver`.
```
registered_app_slug: pr-approver
```
This is used by the app to identify actions it has already taken on PRs.

---

### from_owner
**mandatory**

Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = (app: Probot) => {
}

if (requiredLabelsSatisfied && ownerSatisfied) {
const reviews = await getAutoapprovalReviews(context)
const reviews = await getAutoapprovalReviews(context, config.registered_app_slug)

if (reviews.length > 0) {
context.log('PR has already reviews')
Expand Down Expand Up @@ -132,11 +132,12 @@ async function enableAutoMerge (context: Context, method: string) {
})
}

async function getAutoapprovalReviews (context: Context): Promise<any> {
async function getAutoapprovalReviews (context: Context, appSlug: string): Promise<any> {
const pr = context.pullRequest()
const reviews = await context.octokit.pulls.listReviews(pr)

const autoapprovalReviews = (reviews.data).filter((item: any) => item.user.login === 'autoapproval[bot]')
const fullAppSlug = `${appSlug}[bot]`
const autoapprovalReviews = (reviews.data).filter((item: any) => item.user.login === fullAppSlug)

return autoapprovalReviews
}
3 changes: 1 addition & 2 deletions test/fixtures/pull_request_reviews.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": 80,
"node_id": "MDE3OlB1bGxSZXF1ZXN0UmV2aWV3ODA=",
"user": {
"login": "autoapproval[bot]",
"login": "dummy[bot]",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
Expand Down Expand Up @@ -37,4 +37,3 @@
}
}
]

151 changes: 133 additions & 18 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ describe('Autoapproval bot', () => {

test('PR has missing blacklisted_labels -> will be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - merge\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- merge
apply_labels: []
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand All @@ -56,7 +63,16 @@ describe('Autoapproval bot', () => {

test('PR has blacklisted labels -> will NOT be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - merge\nblacklisted_labels:\n - wip\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- merge
apply_labels: []
blacklisted_labels:
- wip
`

nock('https://api.github.com')
.get('/repos/dabrady/autoapproval/contents/.github%2Fautoapproval.yml')
Expand All @@ -71,7 +87,15 @@ describe('Autoapproval bot', () => {

test('PR has no required labels -> will NOT be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - ready\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- ready
apply_labels: []
blacklisted_labels: []
`

nock('https://api.github.com')
.get('/repos/dabrady/autoapproval/contents/.github%2Fautoapproval.yml')
Expand All @@ -86,7 +110,16 @@ describe('Autoapproval bot', () => {

test('PR has not all required labels -> will NOT be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - ready\n - ready2\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- ready
- ready2
apply_labels: []
blacklisted_labels: []
`

nock('https://api.github.com')
.get('/repos/dabrady/autoapproval/contents/.github%2Fautoapproval.yml')
Expand All @@ -101,7 +134,15 @@ describe('Autoapproval bot', () => {

test('PR has no expected owner -> will NOT be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - blabla\nrequired_labels:\n - merge\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- blabla
required_labels:
- merge
apply_labels: []
blacklisted_labels: []
`

nock('https://api.github.com')
.get('/repos/dabrady/autoapproval/contents/.github%2Fautoapproval.yml')
Expand All @@ -116,7 +157,15 @@ describe('Autoapproval bot', () => {

test('PR has required labels and expected owner -> will be approved', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - merge\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- merge
apply_labels: []
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand All @@ -142,7 +191,16 @@ describe('Autoapproval bot', () => {

test('PR has multiple required labels and expected owner -> will be approved', async () => {
const payload = require('./fixtures/pull_request_opened_multiple_labels.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - merge\n - merge2\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- merge
- merge2
apply_labels: []
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand All @@ -168,7 +226,17 @@ describe('Autoapproval bot', () => {

test('PR has one of multiple required labels and expected owner -> will be approved', async () => {
const payload = require('./fixtures/pull_request_opened_multiple_labels.json')
const config = 'from_owner:\n - dabrady\nrequired_labels:\n - merge\n - merge2\nrequired_labels_mode: one_of\nblacklisted_labels: []\napply_labels: []'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels:
- merge
- merge2
required_labels_mode: one_of
apply_labels: []
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand All @@ -194,8 +262,15 @@ describe('Autoapproval bot', () => {

test('PR approved and label is applied', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels:\n - done'

const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels:
- done
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand Down Expand Up @@ -227,8 +302,16 @@ describe('Autoapproval bot', () => {

test('PR approved and auto merge is enabled', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels: []\nauto_merge_labels:\n - merge'

const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels: []
blacklisted_labels: []
auto_merge_labels:
- merge
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand Down Expand Up @@ -261,8 +344,16 @@ describe('Autoapproval bot', () => {

test('PR approved and auto merge squash is enabled', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels: []\nauto_squash_merge_labels:\n - merge'

const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels: []
blacklisted_labels: []
auto_squash_merge_labels:
- merge
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand Down Expand Up @@ -295,8 +386,16 @@ describe('Autoapproval bot', () => {

test('PR approved and auto merge rebase is enabled', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels: []\nauto_rebase_merge_labels:\n - merge'

const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels: []
blacklisted_labels: []
auto_rebase_merge_labels:
- merge
`
const reviews = require('./fixtures/pull_request_reviews_empty.json')

nock('https://api.github.com')
Expand Down Expand Up @@ -329,7 +428,15 @@ describe('Autoapproval bot', () => {

test('PR is already approved -> will NOT be approved again', async () => {
const payload = require('./fixtures/pull_request.opened.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels:\n - merge'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels:
- merge
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews.json')

nock('https://api.github.com')
Expand All @@ -349,7 +456,15 @@ describe('Autoapproval bot', () => {

test('Autoapproval review was dismissed -> approve PR again', async () => {
const payload = require('./fixtures/pull_request_review.dismissed.json')
const config = 'from_owner:\n - dabrady\nrequired_labels: []\nblacklisted_labels: []\napply_labels:\n - merge'
const config = `
registered_app_slug: 'dummy'
from_owner:
- dabrady
required_labels: []
apply_labels:
- merge
blacklisted_labels: []
`
const reviews = require('./fixtures/pull_request_reviews.json')

nock('https://api.github.com')
Expand Down

0 comments on commit afcdab0

Please sign in to comment.