Repository: probot/settings
Pull request #261 (2020-06-20 19:28:55)

added trigger for syncing based on the repository.created event
depends on #260. please don't merge before that one


Patch:
diff --git a/index.js b/index.js
index 3dfcacb..1b42ee9 100644
--- a/index.js
+++ b/index.js
@@ -42,4 +42,8 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
 
     return syncSettings(context)
   })
+
+  robot.on('repository.created', async context => {
+    return syncSettings(context)
+  })
 }


Test patch:
diff --git a/test/integration/common.js b/test/integration/common.js
index 0612a34..f1813a5 100644
--- a/test/integration/common.js
+++ b/test/integration/common.js
@@ -1,7 +1,20 @@
 const { createProbot } = require('probot')
 const nock = require('nock')
+const any = require('@travi/any')
 const debugNock = require('debug')('nock')
-const settingsBot = require('../../')
+const settingsBot = require('../../index')
+const settings = require('../../lib/settings')
+
+nock.disableNetConnect()
+
+const repository = {
+  default_branch: 'master',
+  name: 'botland',
+  owner: {
+    name: 'bkeepers-inc',
+    email: null
+  }
+}
 
 function loadInstance () {
   const probot = createProbot({ id: 1, cert: 'test', githubToken: 'test' })
@@ -20,16 +33,44 @@ function teardownNock (githubScope) {
   nock.cleanAll()
 }
 
+function buildPushEvent () {
+  return {
+    name: 'push',
+    payload: {
+      ref: 'refs/heads/master',
+      repository,
+      commits: [{ modified: [settings.FILE_NAME], added: [] }]
+    }
+  }
+}
+
+function buildRepositoryEditedEvent () {
+  return {
+    name: 'repository.edited',
+    payload: {
+      changes: { default_branch: { from: any.word() } },
+      repository
+    }
+  }
+}
+
+function buildRepositoryCreatedEvent () {
+  return {
+    name: 'repository.created',
+    payload: { repository }
+  }
+}
+
+function buildTriggerEvent () {
+  return any.fromList([buildPushEvent(), buildRepositoryCreatedEvent(), buildRepositoryEditedEvent()])
+}
+
 module.exports = {
   loadInstance,
   initializeNock,
   teardownNock,
-  repository: {
-    default_branch: 'master',
-    name: 'botland',
-    owner: {
-      name: 'bkeepers-inc',
-      email: null
-    }
-  }
+  buildTriggerEvent,
+  buildRepositoryCreatedEvent,
+  buildRepositoryEditedEvent,
+  repository
 }
diff --git a/test/integration/collaborators.test.js b/test/integration/plugins/collaborators.test.js
similarity index 73%
rename from test/integration/collaborators.test.js
rename to test/integration/plugins/collaborators.test.js
index fb76869..4522c90 100644
--- a/test/integration/collaborators.test.js
+++ b/test/integration/plugins/collaborators.test.js
@@ -1,10 +1,10 @@
 const path = require('path')
 const fs = require('fs')
 const { CREATED, NO_CONTENT, OK } = require('http-status-codes')
-const settings = require('../../lib/settings')
-const { initializeNock, loadInstance, repository, teardownNock } = require('./common')
+const settings = require('../../../lib/settings')
+const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
 
-describe('collaborators', function () {
+describe('collaborators plugin', function () {
   let probot, githubScope
 
   beforeEach(() => {
@@ -17,7 +17,7 @@ describe('collaborators', function () {
   })
 
   it('syncs collaborators', async () => {
-    const pathToConfig = path.resolve(__dirname, '..', 'fixtures', 'collaborators-config.yml')
+    const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'collaborators-config.yml')
     const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
     const encodedConfig = configFile.toString('base64')
     githubScope
@@ -42,13 +42,6 @@ describe('collaborators', function () {
       .delete(`/repos/${repository.owner.name}/${repository.name}/collaborators/travi`)
       .reply(NO_CONTENT)
 
-    await probot.receive({
-      name: 'push',
-      payload: {
-        ref: 'refs/heads/master',
-        repository,
-        commits: [{ modified: [settings.FILE_NAME], added: [] }]
-      }
-    })
+    await probot.receive(buildTriggerEvent())
   })
 })
diff --git a/test/integration/milestones.test.js b/test/integration/plugins/milestones.test.js
similarity index 81%
rename from test/integration/milestones.test.js
rename to test/integration/plugins/milestones.test.js
index 528d386..b954123 100644
--- a/test/integration/milestones.test.js
+++ b/test/integration/plugins/milestones.test.js
@@ -1,10 +1,10 @@
 const path = require('path')
 const fs = require('fs')
 const { CREATED, NO_CONTENT, OK } = require('http-status-codes')
-const settings = require('../../lib/settings')
-const { initializeNock, loadInstance, repository, teardownNock } = require('./common')
+const settings = require('../../../lib/settings')
+const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
 
-describe('milestones', function () {
+describe('milestones plugin', function () {
   let probot, githubScope
 
   beforeEach(() => {
@@ -17,7 +17,7 @@ describe('milestones', function () {
   })
 
   it('syncs milestones', async () => {
-    const pathToConfig = path.resolve(__dirname, '..', 'fixtures', 'milestones-config.yml')
+    const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'milestones-config.yml')
     const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
     const encodedConfig = configFile.toString('base64')
     githubScope
@@ -69,13 +69,6 @@ describe('milestones', function () {
       .delete(`/repos/${repository.owner.name}/${repository.name}/milestones/8`)
       .reply(NO_CONTENT)
 
-    await probot.receive({
-      name: 'push',
-      payload: {
-        ref: 'refs/heads/master',
-        repository,
-        commits: [{ modified: [settings.FILE_NAME], added: [] }]
-      }
-    })
+    await probot.receive(buildTriggerEvent())
   })
 })
diff --git a/test/integration/repository.test.js b/test/integration/plugins/repository.test.js
similarity index 67%
rename from test/integration/repository.test.js
rename to test/integration/plugins/repository.test.js
index 7a3fe1e..b7390a0 100644
--- a/test/integration/repository.test.js
+++ b/test/integration/plugins/repository.test.js
@@ -1,10 +1,10 @@
 const path = require('path')
 const fs = require('fs')
 const yaml = require('js-yaml')
-const settings = require('../../lib/settings')
-const { initializeNock, loadInstance, repository, teardownNock } = require('./common')
+const settings = require('../../../lib/settings')
+const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
 
-describe('repository', function () {
+describe('repository plugin', function () {
   let probot, githubScope
 
   beforeEach(() => {
@@ -17,7 +17,7 @@ describe('repository', function () {
   })
 
   it('syncs repo settings', async () => {
-    const pathToConfig = path.resolve(__dirname, '..', 'fixtures', 'repository-config.yml')
+    const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'repository-config.yml')
     const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
     const config = yaml.safeLoad(configFile, 'utf8')
     const encodedConfig = configFile.toString('base64')
@@ -32,13 +32,6 @@ describe('repository', function () {
       .matchHeader('accept', ['application/vnd.github.baptiste-preview+json'])
       .reply(200)
 
-    await probot.receive({
-      name: 'push',
-      payload: {
-        ref: 'refs/heads/master',
-        repository,
-        commits: [{ modified: [settings.FILE_NAME], added: [] }]
-      }
-    })
+    await probot.receive(buildTriggerEvent())
   })
 })
diff --git a/test/integration/teams.test.js b/test/integration/plugins/teams.test.js
similarity index 80%
rename from test/integration/teams.test.js
rename to test/integration/plugins/teams.test.js
index 90e78f5..c7b9ca7 100644
--- a/test/integration/teams.test.js
+++ b/test/integration/plugins/teams.test.js
@@ -2,10 +2,10 @@ const path = require('path')
 const fs = require('fs')
 const { CREATED, NO_CONTENT, OK } = require('http-status-codes')
 const any = require('@travi/any')
-const settings = require('../../lib/settings')
-const { initializeNock, loadInstance, repository, teardownNock } = require('./common')
+const settings = require('../../../lib/settings')
+const { buildTriggerEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
 
-describe('teams', function () {
+describe('teams plugin', function () {
   let probot, githubScope
 
   beforeEach(() => {
@@ -18,7 +18,7 @@ describe('teams', function () {
   })
 
   it('syncs teams', async () => {
-    const pathToConfig = path.resolve(__dirname, '..', 'fixtures', 'teams-config.yml')
+    const pathToConfig = path.resolve(__dirname, '..', '..', 'fixtures', 'teams-config.yml')
     const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
     const encodedConfig = configFile.toString('base64')
     const probotTeamId = any.integer()
@@ -55,13 +55,6 @@ describe('teams', function () {
       .delete(`/teams/${formationTeamId}/repos/${repository.owner.name}/${repository.name}`)
       .reply(NO_CONTENT)
 
-    await probot.receive({
-      name: 'push',
-      payload: {
-        ref: 'refs/heads/master',
-        repository,
-        commits: [{ modified: [settings.FILE_NAME], added: [] }]
-      }
-    })
+    await probot.receive(buildTriggerEvent())
   })
 })
diff --git a/test/integration/push.test.js b/test/integration/triggers/push.test.js
similarity index 77%
rename from test/integration/push.test.js
rename to test/integration/triggers/push.test.js
index d13cbd9..746fde7 100644
--- a/test/integration/push.test.js
+++ b/test/integration/triggers/push.test.js
@@ -1,10 +1,7 @@
-const nock = require('nock')
-const settings = require('../../lib/settings')
-const { initializeNock, loadInstance, repository, teardownNock } = require('./common')
+const settings = require('../../../lib/settings')
+const { initializeNock, loadInstance, repository, teardownNock } = require('../common')
 
-nock.disableNetConnect()
-
-describe('push', function () {
+describe('push trigger', function () {
   let probot, githubScope
 
   beforeEach(() => {
diff --git a/test/integration/triggers/repository-created.test.js b/test/integration/triggers/repository-created.test.js
new file mode 100644
index 0000000..4245c00
--- /dev/null
+++ b/test/integration/triggers/repository-created.test.js
@@ -0,0 +1,33 @@
+const { NOT_FOUND } = require('http-status-codes')
+const settings = require('../../../lib/settings')
+const { buildRepositoryCreatedEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
+
+describe('repository.created trigger', function () {
+  let probot, githubScope
+
+  beforeEach(() => {
+    githubScope = initializeNock()
+    probot = loadInstance()
+  })
+
+  afterEach(() => {
+    teardownNock(githubScope)
+  })
+
+  it('does not apply configuration when the repository does not have a settings.yml', async () => {
+    githubScope
+      .get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_NAME}`)
+      .reply(NOT_FOUND, {
+        message: 'Not Found',
+        documentation_url: 'https://developer.github.com/v3/repos/contents/#get-contents'
+      })
+    githubScope
+      .get(`/repos/${repository.owner.name}/.github/contents/${settings.FILE_NAME}`)
+      .reply(NOT_FOUND, {
+        message: 'Not Found',
+        documentation_url: 'https://developer.github.com/v3/repos/contents/#get-contents'
+      })
+
+    await probot.receive(buildRepositoryCreatedEvent())
+  })
+})
diff --git a/test/integration/triggers/repository-edited.test.js b/test/integration/triggers/repository-edited.test.js
new file mode 100644
index 0000000..d5d6c6f
--- /dev/null
+++ b/test/integration/triggers/repository-edited.test.js
@@ -0,0 +1,43 @@
+const { NOT_FOUND } = require('http-status-codes')
+const any = require('@travi/any')
+const settings = require('../../../lib/settings')
+const { buildRepositoryEditedEvent, initializeNock, loadInstance, repository, teardownNock } = require('../common')
+
+describe('repository.edited trigger', function () {
+  let probot, githubScope
+
+  beforeEach(() => {
+    githubScope = initializeNock()
+    probot = loadInstance()
+  })
+
+  afterEach(() => {
+    teardownNock(githubScope)
+  })
+
+  it('does not apply configuration when the default branch was not changed', async () => {
+    await probot.receive({
+      name: 'repository.edited',
+      payload: {
+        changes: any.simpleObject()
+      }
+    })
+  })
+
+  it('does not apply configuration when the repository does not have a settings.yml', async () => {
+    githubScope
+      .get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_NAME}`)
+      .reply(NOT_FOUND, {
+        message: 'Not Found',
+        documentation_url: 'https://developer.github.com/v3/repos/contents/#get-contents'
+      })
+    githubScope
+      .get(`/repos/${repository.owner.name}/.github/contents/${settings.FILE_NAME}`)
+      .reply(NOT_FOUND, {
+        message: 'Not Found',
+        documentation_url: 'https://developer.github.com/v3/repos/contents/#get-contents'
+      })
+
+    await probot.receive(buildRepositoryEditedEvent())
+  })
+})
diff --git a/test/unit/index.test.js b/test/unit/index.test.js
index 13bd7d7..06a471f 100644
--- a/test/unit/index.test.js
+++ b/test/unit/index.test.js
@@ -54,7 +54,7 @@ describe('plugin', () => {
   describe('default branch changed', () => {
     beforeEach(() => {
       event = {
-        name: 'repository',
+        name: 'repository.edited',
         payload: require('../fixtures/events/repository.edited.json')
       }
     })
@@ -64,4 +64,14 @@ describe('plugin', () => {
       expect(sync).toHaveBeenCalled()
     })
   })
+
+  describe('repository created', async () => {
+    event = {
+      name: 'repository.created',
+      payload: {}
+    }
+
+    await app.receive(event)
+    expect(sync).toHaveBeenCalled()
+  })
 })