Skip to content

Commit 4cca3b0

Browse files
authored
Update the context to handle context.repo for org-level webhooks (#539)
* Update the context to handle context.repo for org-level webhooks * Update README.md for typos and content
1 parent 1540810 commit 4cca3b0

File tree

2 files changed

+60
-52
lines changed

2 files changed

+60
-52
lines changed

README.md

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,20 @@
99
> It is possible to override this behavior and specify a custom repo instead of the `admin` repo.<br>
1010
> This could be done by setting an `env` variable called `ADMIN_REPO`.
1111
12-
1. In `safe-settings` the settings can have 2 types of targets:
12+
1. The **settings** in the **default** branch is applied. If the settings are changed in a non-default branch and a PR is created to merge the changes, it would be run in a `dry-run` mode to evaluate and validate the settings, and checks would pass or fail based on that.
13+
2. In `safe-settings` the settings can have 2 types of targets:
1314
1. `org` - These settings are applied to the `org`. `Org`-targeted settings are defined in `.github/settings.yml` . Currently, only `rulesets` are supported as `org`-targeted settings.
1415
2. `repo` - These settings are applied to `repos`
1516

16-
2. For The `repo`-targeted settings there can be at 3 levels at which the settings could be managed:
17+
3. For The `repo`-targeted settings there can be at 3 levels at which the settings could be managed:
1718
1. Org-level settings are defined in `.github/settings.yml`
1819
> **Note**
1920
> It is possible to override this behavior and specify a different filename for the `settings` yml repo.<br>
2021
> This could be done by setting an `env` variable called `SETTINGS_FILE_PATH`.
2122
2223
2. `Suborg` level settings. A `suborg` is an arbitrary collection of repos belonging to projects, business units, or teams. The `suborg` settings reside in a yaml file for each `suborg` in the `.github/suborgs` folder.
2324
3. `Repo` level settings. They reside in a repo specific yaml in `.github/repos` folder
24-
3. It is recommended to break the settings into org-level, suborg-level, and repo-level units. This will allow different teams to define and manage policies for their specific projects or business units. With `CODEOWNERS`, this will allow different people to be responsible for approving changes in different projects.
25+
4. It is recommended to break the settings into org-level, suborg-level, and repo-level units. This will allow different teams to define and manage policies for their specific projects or business units. With `CODEOWNERS`, this will allow different people to be responsible for approving changes in different projects.
2526

2627
> **Note**
2728
> `Suborg` and `Repo` level settings directory structure cannot be customized.
@@ -150,52 +151,52 @@ If the settings is:
150151
and the settings in GitHub is:
151152
```json
152153
{
153-
"branches": [
154-
{
155-
"name": "master",
156-
"protection": {
157-
url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection",
158-
"required_status_checks": {
159-
url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_status_checks",
160-
"strict": true,
161-
"contexts": [],
162-
contexts_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_status_checks/contexts",
163-
"checks": []
164-
},
165-
"restrictions": {
166-
url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions",
167-
users_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/users",
168-
teams_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/teams",
169-
apps_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/apps",
170-
"users": [],
171-
"teams": [],
172-
"apps": []
173-
},
174-
"required_pull_request_reviews": {
175-
url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_pull_request_reviews",
176-
"dismiss_stale_reviews": true,
177-
"require_code_owner_reviews": true,
178-
"required_approving_review_count": 2,
179-
"dismissal_restrictions": {
180-
url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions",
181-
users_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions/users",
182-
teams_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions/teams",
183-
"users": [],
184-
"teams": []
185-
}
186-
},
187-
"required_signatures": false,
188-
"enforce_admins": false,
189-
"required_linear_history": false,
190-
"allow_force_pushes": {
191-
"enabled": false
192-
},
193-
"allow_deletions": false,
194-
"required_conversation_resolution": false
195-
}
196-
}
197-
]
198-
}
154+
"branches": [
155+
{
156+
"name": "master",
157+
"protection": {
158+
"url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection",
159+
"required_status_checks": {
160+
"url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_status_checks",
161+
"strict": true,
162+
"contexts": [],
163+
"contexts_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_status_checks/contexts",
164+
"checks": []
165+
},
166+
"restrictions": {
167+
"url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions",
168+
"users_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/users",
169+
"teams_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/teams",
170+
"apps_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/restrictions/apps",
171+
"users": [],
172+
"teams": [],
173+
"apps": []
174+
},
175+
"required_pull_request_reviews": {
176+
"url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/required_pull_request_reviews",
177+
"dismiss_stale_reviews": true,
178+
"require_code_owner_reviews": true,
179+
"required_approving_review_count": 2,
180+
"dismissal_restrictions": {
181+
"url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions",
182+
"users_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions/users",
183+
"teams_url": "https://api.github.com/repos/decyjphr-org/test/branches/develop/protection/dismissal_restrictions/teams",
184+
"users": [],
185+
"teams": []
186+
}
187+
},
188+
"required_signatures": false,
189+
"enforce_admins": false,
190+
"required_linear_history": false,
191+
"allow_force_pushes": {
192+
"enabled": false
193+
},
194+
"allow_deletions": false,
195+
"required_conversation_resolution": false
196+
}
197+
}
198+
]
199+
}
199200
```
200201

201202
the results of comparison would be:
@@ -214,6 +215,7 @@ the results of comparison would be:
214215
}
215216
]
216217
},
218+
"deletions": {},
217219
"hasChanges": true
218220
}
219221
```

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
263263
return
264264
}
265265
robot.log.debug('Branch Creation by a Human')
266-
if(payload.repository.default_branch !== payload.ref) {
266+
if (payload.repository.default_branch !== payload.ref) {
267267
robot.log.debug('Not default Branch')
268268
return
269269
}
@@ -291,9 +291,14 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
291291
robot.log.debug('Repository Ruleset edited by Bot')
292292
return
293293
}
294+
294295
robot.log.debug('Repository Repository edited by a Human')
295-
if (payload.repository_ruleset.source_type === 'organization') {
296-
return syncAllSettings(false, context)
296+
if (payload.repository_ruleset.source_type === 'Organization') {
297+
// For org-level events, we need to update the context since context.repo() won't work
298+
const updatedContext = Object.assign({}, context, {
299+
repo: () => { return { repo: env.ADMIN_REPO, owner: payload.organization.login } }
300+
})
301+
return syncAllSettings(false, updatedContext)
297302
} else {
298303
return syncSettings(false, context)
299304
}
@@ -305,6 +310,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
305310
'team.removed_from_repository',
306311
'team.edited'
307312
]
313+
308314
robot.on(member_change_events, async context => {
309315
const { payload } = context
310316
const { sender } = payload

0 commit comments

Comments
 (0)