Repository: probot/settings
Pull request #163 (2019-11-02 17:26:39)

Detect and update config if default branch was changed

travi: could you explain the use case where this is useful?

we would also want corresponding unit tests to be included if this is a change we end up moving forward with 
mvegter: @travi if the default branches changes than the content of the .github folder could be different. Thus I would expect probot to reload the settings file.
travi: seems like this should be a really rare use case, but it seems like this is a reasonable way to handle it when it does happen.

with tests in place, i think we can get this merged

Patch:
diff --git a/index.js b/index.js
index b30cb4e..3dfcacb 100644
--- a/index.js
+++ b/index.js
@@ -1,6 +1,11 @@
 const mergeArrayByName = require('./lib/mergeArrayByName')
 
 module.exports = (robot, _, Settings = require('./lib/settings')) => {
+  async function syncSettings (context, repo = context.repo()) {
+    const config = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
+    return Settings.sync(context.github, repo, config)
+  }
+
   robot.on('push', async context => {
     const { payload } = context
     const { repository } = payload
@@ -21,7 +26,20 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
       return
     }
 
-    const config = await context.config('settings.yml', {}, { arrayMerge: mergeArrayByName })
-    return Settings.sync(context.github, context.repo(), config)
+    return syncSettings(context)
+  })
+
+  robot.on('repository.edited', async context => {
+    const { payload } = context
+    const { changes, repository } = payload
+
+    if (!Object.prototype.hasOwnProperty.call(changes, 'default_branch')) {
+      robot.log.debug('Repository configuration was edited but the default branch was not affected, returning...')
+      return
+    }
+
+    robot.log.debug(`Default branch changed from '${changes.default_branch.from}' to '${repository.default_branch}'`)
+
+    return syncSettings(context)
   })
 }


Test patch:
diff --git a/test/fixtures/events/repository.edited.json b/test/fixtures/events/repository.edited.json
new file mode 100644
index 0000000..0abaf10
--- /dev/null
+++ b/test/fixtures/events/repository.edited.json
@@ -0,0 +1,140 @@
+{
+  "action": "edited",
+  "changes": {
+    "default_branch": {
+      "from": "mvegter-patch-2"
+    }
+  },
+  "repository": {
+    "id": 220051293,
+    "node_id": "MDEwOlJlcG9zaXRvcnkyMjAwNTEyOTM=",
+    "name": "repo-a",
+    "full_name": "Martijn-Workspace/repo-a",
+    "private": false,
+    "owner": {
+      "login": "Martijn-Workspace",
+      "id": 57455045,
+      "node_id": "MDEyOk9yZ2FuaXphdGlvbjU3NDU1MDQ1",
+      "avatar_url": "https://avatars0.githubusercontent.com/u/57455045?v=4",
+      "gravatar_id": "",
+      "url": "https://api.github.com/users/Martijn-Workspace",
+      "html_url": "https://github.com/Martijn-Workspace",
+      "followers_url": "https://api.github.com/users/Martijn-Workspace/followers",
+      "following_url": "https://api.github.com/users/Martijn-Workspace/following{/other_user}",
+      "gists_url": "https://api.github.com/users/Martijn-Workspace/gists{/gist_id}",
+      "starred_url": "https://api.github.com/users/Martijn-Workspace/starred{/owner}{/repo}",
+      "subscriptions_url": "https://api.github.com/users/Martijn-Workspace/subscriptions",
+      "organizations_url": "https://api.github.com/users/Martijn-Workspace/orgs",
+      "repos_url": "https://api.github.com/users/Martijn-Workspace/repos",
+      "events_url": "https://api.github.com/users/Martijn-Workspace/events{/privacy}",
+      "received_events_url": "https://api.github.com/users/Martijn-Workspace/received_events",
+      "type": "Organization",
+      "site_admin": false
+    },
+    "html_url": "https://github.com/Martijn-Workspace/repo-a",
+    "description": "description of repo",
+    "fork": false,
+    "url": "https://api.github.com/repos/Martijn-Workspace/repo-a",
+    "forks_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/forks",
+    "keys_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/keys{/key_id}",
+    "collaborators_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/collaborators{/collaborator}",
+    "teams_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/teams",
+    "hooks_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/hooks",
+    "issue_events_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/issues/events{/number}",
+    "events_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/events",
+    "assignees_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/assignees{/user}",
+    "branches_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/branches{/branch}",
+    "tags_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/tags",
+    "blobs_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/git/blobs{/sha}",
+    "git_tags_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/git/tags{/sha}",
+    "git_refs_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/git/refs{/sha}",
+    "trees_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/git/trees{/sha}",
+    "statuses_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/statuses/{sha}",
+    "languages_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/languages",
+    "stargazers_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/stargazers",
+    "contributors_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/contributors",
+    "subscribers_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/subscribers",
+    "subscription_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/subscription",
+    "commits_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/commits{/sha}",
+    "git_commits_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/git/commits{/sha}",
+    "comments_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/comments{/number}",
+    "issue_comment_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/issues/comments{/number}",
+    "contents_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/contents/{+path}",
+    "compare_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/compare/{base}...{head}",
+    "merges_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/merges",
+    "archive_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/{archive_format}{/ref}",
+    "downloads_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/downloads",
+    "issues_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/issues{/number}",
+    "pulls_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/pulls{/number}",
+    "milestones_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/milestones{/number}",
+    "notifications_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/notifications{?since,all,participating}",
+    "labels_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/labels{/name}",
+    "releases_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/releases{/id}",
+    "deployments_url": "https://api.github.com/repos/Martijn-Workspace/repo-a/deployments",
+    "created_at": "2019-11-06T17:19:47Z",
+    "updated_at": "2019-12-01T16:02:54Z",
+    "pushed_at": "2019-11-24T11:18:20Z",
+    "git_url": "git://github.com/Martijn-Workspace/repo-a.git",
+    "ssh_url": "git@github.com:Martijn-Workspace/repo-a.git",
+    "clone_url": "https://github.com/Martijn-Workspace/repo-a.git",
+    "svn_url": "https://github.com/Martijn-Workspace/repo-a",
+    "homepage": "https://example.github.io/",
+    "size": 11,
+    "stargazers_count": 0,
+    "watchers_count": 0,
+    "language": null,
+    "has_issues": false,
+    "has_projects": true,
+    "has_downloads": true,
+    "has_wiki": true,
+    "has_pages": false,
+    "forks_count": 0,
+    "mirror_url": null,
+    "archived": false,
+    "disabled": false,
+    "open_issues_count": 0,
+    "license": null,
+    "forks": 0,
+    "open_issues": 0,
+    "watchers": 0,
+    "default_branch": "master"
+  },
+  "organization": {
+    "login": "Martijn-Workspace",
+    "id": 57455045,
+    "node_id": "MDEyOk9yZ2FuaXphdGlvbjU3NDU1MDQ1",
+    "url": "https://api.github.com/orgs/Martijn-Workspace",
+    "repos_url": "https://api.github.com/orgs/Martijn-Workspace/repos",
+    "events_url": "https://api.github.com/orgs/Martijn-Workspace/events",
+    "hooks_url": "https://api.github.com/orgs/Martijn-Workspace/hooks",
+    "issues_url": "https://api.github.com/orgs/Martijn-Workspace/issues",
+    "members_url": "https://api.github.com/orgs/Martijn-Workspace/members{/member}",
+    "public_members_url": "https://api.github.com/orgs/Martijn-Workspace/public_members{/member}",
+    "avatar_url": "https://avatars0.githubusercontent.com/u/57455045?v=4",
+    "description": null
+  },
+  "sender": {
+    "login": "probot-settings-label[bot]",
+    "id": 58135831,
+    "node_id": "MDM6Qm90NTgxMzU4MzE=",
+    "avatar_url": "https://avatars3.githubusercontent.com/u/25134477?v=4",
+    "gravatar_id": "",
+    "url": "https://api.github.com/users/probot-settings-label%5Bbot%5D",
+    "html_url": "https://github.com/apps/probot-settings-label",
+    "followers_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/followers",
+    "following_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/following{/other_user}",
+    "gists_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/gists{/gist_id}",
+    "starred_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/starred{/owner}{/repo}",
+    "subscriptions_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/subscriptions",
+    "organizations_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/orgs",
+    "repos_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/repos",
+    "events_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/events{/privacy}",
+    "received_events_url": "https://api.github.com/users/probot-settings-label%5Bbot%5D/received_events",
+    "type": "Bot",
+    "site_admin": false
+  },
+  "installation": {
+    "id": 5368170,
+    "node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uNTM2ODE3MA=="
+  }
+}
diff --git a/test/index.test.js b/test/index.test.js
index 2e3e1b2..8853d80 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -45,9 +45,23 @@ describe('plugin', () => {
       event.payload = require('./fixtures/events/push.readme.json')
     })
 
-    it('does not sync settings', () => {
-      app.receive(event)
+    it('does not sync settings', async () => {
+      await app.receive(event)
       expect(sync).not.toHaveBeenCalled()
     })
   })
+
+  describe('default branch changed', () => {
+    beforeEach(() => {
+      event = {
+        name: 'repository',
+        payload: require('./fixtures/events/repository.edited.json')
+      }
+    })
+
+    it('does sync settings', async () => {
+      await app.receive(event)
+      expect(sync).toHaveBeenCalled()
+    })
+  })
 })