Skip to content
32 changes: 26 additions & 6 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const Overrides = require('./overrides')
const ignorableFields = []
const previewHeaders = { accept: 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }
const overrides = {
'contexts': {
'action': 'reset',
'type': 'array'
},
contexts: {
action: 'reset',
type: 'array'
}
}

// GitHub API requires these fields to be present in updateBranchProtection calls
// See: https://docs.github.com/rest/branches/branch-protection#update-branch-protection
const requiredBranchProtectionDefaults = {
required_status_checks: null,
enforce_admins: null,
restrictions: null
}

module.exports = class Branches extends ErrorStash {
Expand Down Expand Up @@ -73,7 +81,7 @@ module.exports = class Branches extends ErrorStash {
resArray.push(new NopCommand(this.constructor.name, this.repo, null, results))
}

Object.assign(params, branch.protection, { headers: previewHeaders })
Object.assign(params, requiredBranchProtectionDefaults, this.reformatAndReturnBranchProtection(structuredClone(result.data)), branch.protection, { headers: previewHeaders })

if (this.nop) {
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.repos.updateBranchProtection.endpoint(params), 'Add Branch Protection'))
Expand All @@ -83,7 +91,7 @@ module.exports = class Branches extends ErrorStash {
return this.github.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
}).catch((e) => {
if (e.status === 404) {
Object.assign(params, Overrides.removeOverrides(overrides, branch.protection, {}), { headers: previewHeaders })
Object.assign(params, requiredBranchProtectionDefaults, Overrides.removeOverrides(overrides, branch.protection, {}), { headers: previewHeaders })
if (this.nop) {
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.repos.updateBranchProtection.endpoint(params), 'Add Branch Protection'))
return Promise.resolve(resArray)
Expand Down Expand Up @@ -123,6 +131,18 @@ module.exports = class Branches extends ErrorStash {
protection.required_linear_history = protection.required_linear_history && protection.required_linear_history.enabled
protection.enforce_admins = protection.enforce_admins && protection.enforce_admins.enabled
protection.required_signatures = protection.required_signatures && protection.required_signatures.enabled
if (protection.restrictions) {
delete protection.restrictions.url
protection.restrictions.users = Array.isArray(protection.restrictions.users)
? protection.restrictions.users.map(user => user.login || user)
: []
protection.restrictions.teams = Array.isArray(protection.restrictions.teams)
? protection.restrictions.teams.map(team => team.slug || team)
: []
protection.restrictions.apps = Array.isArray(protection.restrictions.apps)
? protection.restrictions.apps.map(app => app.slug || app)
: []
}
if (protection.required_pull_request_reviews && !protection.required_pull_request_reviews.bypass_pull_request_allowances) {
protection.required_pull_request_reviews.bypass_pull_request_allowances = { apps: [], teams: [], users: [] }
}
Expand Down
3 changes: 3 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Settings {
const settings = new Settings(nop, context, context.repo(), config, ref)

try {
// Apply org-level settings (e.g., rulesets) first, matching syncAll behavior
await settings.updateOrg()

for (const repo of repos) {
settings.repo = repo
await settings.loadConfigs(repo)
Expand Down
Loading