From e2716b1d3fa5d8c04f2ccdd76c0fce6d8eccbc0e Mon Sep 17 00:00:00 2001 From: yakanechi <138551445+yakanechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 13:24:54 +0900 Subject: [PATCH 1/2] fix --- plugins/events/create.js | 3 +- test/plugins/events.test.js | 56 ++++++++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/plugins/events/create.js b/plugins/events/create.js index 385f80b26..be9745501 100644 --- a/plugins/events/create.js +++ b/plugins/events/create.js @@ -136,7 +136,7 @@ module.exports = () => ({ let permissions; try { - permissions = await user.getPermissions(scmUri); + permissions = await user.getPermissions(scmUri, pipeline.scmContext, pipeline.scmRepo); } catch (err) { if (err.statusCode === 403 && pipeline.scmRepo && pipeline.scmRepo.private) { throw boom.notFound(); @@ -155,6 +155,7 @@ module.exports = () => ({ prNum, scmContext: pipeline.scmContext, scmUri: pipeline.scmUri, + scmRepo: pipeline.scmRepo, token }; diff --git a/test/plugins/events.test.js b/test/plugins/events.test.js index dabef352b..bc6932edf 100644 --- a/test/plugins/events.test.js +++ b/test/plugins/events.test.js @@ -291,29 +291,19 @@ describe('event plugin test', () => { let expectedLocation; let scmConfig; let userMock; + let pipelineMock; let meta; const username = 'myself'; const parentBuildId = 12345; const pipelineId = 123; const scmContext = 'github:github.com'; + const scmRepo = { + branch: 'branch', + url: 'https://github.com/org/repo/tree/branch', + name: 'org/repo' + }; const scmUri = 'github.com:12345:branchName'; const checkoutUrl = 'git@github.com:screwdriver-cd/data-model.git#master'; - const pipelineMock = { - id: pipelineId, - checkoutUrl, - scmContext: 'github:github.com', - update: sinon.stub().resolves(), - admins: { foo: true, bar: true }, - admin: Promise.resolve({ - username: 'foo', - unsealToken: sinon.stub().resolves('token') - }), - scmUri, - chainPR: false, - annotations: { - 'screwdriver.cd/restrictPR': 'none' - } - }; const parentBuilds = { 123: { eventId: 8888, jobs: { main: 12345 } } }; const prInfo = { sha: testBuild.sha, @@ -330,10 +320,28 @@ describe('event plugin test', () => { unsealToken: sinon.stub().resolves('iamtoken'), getFullDisplayName: sinon.stub().returns('Memys Elfandi') }; + pipelineMock = { + id: pipelineId, + checkoutUrl, + scmContext: 'github:github.com', + scmRepo, + update: sinon.stub().resolves(), + admins: { foo: true, bar: true }, + admin: Promise.resolve({ + username: 'foo', + unsealToken: sinon.stub().resolves('token') + }), + scmUri, + chainPR: false, + annotations: { + 'screwdriver.cd/restrictPR': 'none' + } + }; scmConfig = { prNum: null, scmContext: 'github:github.com', scmUri, + scmRepo, token: 'iamtoken' }; meta = { @@ -409,6 +417,7 @@ describe('event plugin test', () => { }; assert.calledWith(buildFactoryMock.get, 1234); assert.calledWith(jobFactoryMock.get, 222); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -440,6 +449,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledWith(eventFactoryMock.scm.getCommitSha, scmConfig); @@ -471,6 +481,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledWith(eventFactoryMock.scm.getCommitSha, scmConfig); @@ -487,6 +498,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledWith(eventFactoryMock.scm.getCommitSha, scmConfig); @@ -505,6 +517,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledWith(eventFactoryMock.scm.getCommitSha, scmConfig); @@ -527,6 +540,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -567,6 +581,7 @@ describe('event plugin test', () => { }; assert.calledWith(buildFactoryMock.get, 1234); assert.calledWith(jobFactoryMock.get, 222); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -609,6 +624,7 @@ describe('event plugin test', () => { }; assert.calledWith(buildFactoryMock.get, 1234); assert.calledWith(jobFactoryMock.get, 222); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -645,6 +661,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -681,6 +698,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.notCalled(eventFactoryMock.scm.getPrInfo); @@ -700,6 +718,7 @@ describe('event plugin test', () => { return server.inject(options).then(reply => { assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); @@ -720,6 +739,7 @@ describe('event plugin test', () => { return server.inject(options).then(reply => { assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); @@ -740,6 +760,7 @@ describe('event plugin test', () => { return server.inject(options).then(reply => { assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); @@ -875,6 +896,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledOnce(eventFactoryMock.scm.getCommitSha); @@ -898,6 +920,7 @@ describe('event plugin test', () => { pathname: `${options.url}/12345` }; assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledWith(eventFactoryMock.scm.getCommitSha, scmConfig); @@ -919,6 +942,7 @@ describe('event plugin test', () => { return server.inject(options).then(reply => { assert.equal(reply.statusCode, 201); + assert.calledWith(userMock.getPermissions, scmUri, scmContext, scmRepo); assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); From 131a5077a5b24857a3367b2d5afc6a5c48a69d27 Mon Sep 17 00:00:00 2001 From: yakanechi <138551445+yakanechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:24:07 +0900 Subject: [PATCH 2/2] fix test --- test/plugins/events.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/plugins/events.test.js b/test/plugins/events.test.js index bc6932edf..dcc4d3e6a 100644 --- a/test/plugins/events.test.js +++ b/test/plugins/events.test.js @@ -722,6 +722,7 @@ describe('event plugin test', () => { assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); }); @@ -743,6 +744,10 @@ describe('event plugin test', () => { assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { + ...scmConfig, + prNum: Number(options.payload.prNum) + }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); }); @@ -764,6 +769,7 @@ describe('event plugin test', () => { assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); }); @@ -901,6 +907,7 @@ describe('event plugin test', () => { assert.strictEqual(reply.headers.location, urlLib.format(expectedLocation)); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); }); @@ -946,6 +953,7 @@ describe('event plugin test', () => { assert.calledWith(eventFactoryMock.create, eventConfig); assert.calledOnce(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); }); @@ -1030,6 +1038,7 @@ describe('event plugin test', () => { assert.notCalled(eventFactoryMock.create); assert.notCalled(eventFactoryMock.scm.getCommitSha); assert.calledOnce(eventFactoryMock.scm.getPrInfo); + assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: '1' }); assert.calledOnce(eventFactoryMock.scm.getChangedFiles); }); });