diff --git a/index.js b/index.js index eaa4e882f..e2bdc9952 100644 --- a/index.js +++ b/index.js @@ -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)) } diff --git a/lib/plugins/rulesets.js b/lib/plugins/rulesets.js new file mode 100644 index 000000000..5cbcdf95d --- /dev/null +++ b/lib/plugins/rulesets.js @@ -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 }) + } +} diff --git a/lib/settings.js b/lib/settings.js index 54e2087f7..0e2b97acd 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -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) { @@ -49,5 +50,6 @@ Settings.PLUGINS = { teams: Teams, milestones: Milestones, branches: Branches, - environments: Environments + environments: Environments, + rulesets: Rulesets } diff --git a/test/integration/features/rulesets.feature b/test/integration/features/rulesets.feature index 6b0a9b712..dde0e6840 100644 --- a/test/integration/features/rulesets.feature +++ b/test/integration/features/rulesets.feature @@ -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 diff --git a/test/integration/features/step_definitions/rulesets-steps.mjs b/test/integration/features/step_definitions/rulesets-steps.mjs index 7b3915525..56e164e00 100644 --- a/test/integration/features/step_definitions/rulesets-steps.mjs +++ b/test/integration/features/step_definitions/rulesets-steps.mjs @@ -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( @@ -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`, @@ -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) }) diff --git a/test/unit/lib/plugins/rulesets.test.js b/test/unit/lib/plugins/rulesets.test.js new file mode 100644 index 000000000..a7b3733c3 --- /dev/null +++ b/test/unit/lib/plugins/rulesets.test.js @@ -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 }) + }) +})