Skip to content

Commit

Permalink
feat(rulesets): enable adding a new ruleset
Browse files Browse the repository at this point in the history
for #732
  • Loading branch information
travi committed Sep 13, 2024
1 parent 5b9a0e7 commit d7e9ffe
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,5 @@ export default (robot, _, Settings = SettingsApp) => {
return syncSettings(context)
})

robot.on('repository.created', async context => {
return syncSettings(context)
})
robot.on('repository.created', async context => syncSettings(context))
}
13 changes: 13 additions & 0 deletions lib/plugins/rulesets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Diffable from './diffable.js'

export default class Rulesets extends Diffable {
async find () {
await this.github.repos.getRepoRulesets(this.repo)

return []
}

async add (attrs) {
await this.github.repos.createRepoRuleset({ ...this.repo, ...attrs })
}
}
4 changes: 3 additions & 1 deletion lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Teams from './plugins/teams.js'
import Milestones from './plugins/milestones.js'
import Branches from './plugins/branches.js'
import Environments from './plugins/environments.js'
import Rulesets from './plugins/rulesets.js'

export default class Settings {
static sync (github, repo, config) {
Expand Down Expand Up @@ -49,5 +50,6 @@ Settings.PLUGINS = {
teams: Teams,
milestones: Milestones,
branches: Branches,
environments: Environments
environments: Environments,
rulesets: Rulesets
}
1 change: 0 additions & 1 deletion test/integration/features/rulesets.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Feature: Repository Rulesets

@wip
Scenario: Add a ruleset
Given no rulesets are defined for the repository
And a ruleset is defined in the config
Expand Down
7 changes: 5 additions & 2 deletions test/integration/features/step_definitions/rulesets-steps.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { http, HttpResponse } from 'msw'

import { repository } from './common-steps.mjs'
import settings from '../../../../lib/settings.js'
import any from '@travi/any'

Given('no rulesets are defined for the repository', async function () {
this.server.use(
Expand All @@ -17,12 +18,14 @@ Given('no rulesets are defined for the repository', async function () {
})

Given('a ruleset is defined in the config', async function () {
this.ruleset = { name: any.word() }

this.server.use(
http.get(
`https://api.github.com/repos/${repository.owner.name}/${repository.name}/contents/${encodeURIComponent(
settings.FILE_NAME
)}`,
({ request }) => HttpResponse.arrayBuffer(Buffer.from(dump({ rulesets: [{}] })))
({ request }) => HttpResponse.arrayBuffer(Buffer.from(dump({ rulesets: [this.ruleset] })))
),
http.post(
`https://api.github.com/repos/${repository.owner.name}/${repository.name}/rulesets`,
Expand All @@ -36,5 +39,5 @@ Given('a ruleset is defined in the config', async function () {
})

Then('the ruleset is enabled for the repository', async function () {
assert.deepEqual(this.createdRuleset, {})
assert.deepEqual(this.createdRuleset, this.ruleset)
})
36 changes: 36 additions & 0 deletions test/unit/lib/plugins/rulesets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { jest } from '@jest/globals'
import { when } from 'jest-when'
import any from '@travi/any'

import Rulesets from '../../../../lib/plugins/rulesets'

function configure (github, owner, repo, config) {
return new Rulesets(github, { owner, repo }, config)
}

describe('rulesets', () => {
let github
const owner = any.word()
const repo = any.word()

beforeEach(() => {
github = {
repos: {
createRepoRuleset: jest.fn(),
getRepoRulesets: jest.fn()
}
}
})

it('should sync rulesets', async () => {
const newRuleset = { name: any.word() }
const plugin = configure(github, owner, repo, [newRuleset])
when(github.repos.getRepoRulesets)
.calledWith({ owner, repo })
.mockResolvedValue([])

await plugin.sync()

expect(github.repos.createRepoRuleset).toHaveBeenCalledWith({ owner, repo, ...newRuleset })
})
})

0 comments on commit d7e9ffe

Please sign in to comment.