Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(3146): Get repository data from scmRepo without GitHub access from UI start #3155

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion plugins/events/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -155,6 +155,7 @@ module.exports = () => ({
prNum,
scmContext: pipeline.scmContext,
scmUri: pipeline.scmUri,
scmRepo: pipeline.scmRepo,
token
};

Expand Down
65 changes: 49 additions & 16 deletions test/plugins/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]: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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -700,9 +718,11 @@ 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);
assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum });
assert.calledOnce(eventFactoryMock.scm.getChangedFiles);
});
});
Expand All @@ -720,9 +740,14 @@ 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);
assert.calledWith(eventFactoryMock.scm.getPrInfo, {
...scmConfig,
prNum: Number(options.payload.prNum)
});
assert.calledOnce(eventFactoryMock.scm.getChangedFiles);
});
});
Expand All @@ -740,9 +765,11 @@ 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);
assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum });
assert.calledOnce(eventFactoryMock.scm.getChangedFiles);
});
});
Expand Down Expand Up @@ -875,10 +902,12 @@ 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);
assert.calledOnce(eventFactoryMock.scm.getPrInfo);
assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum });
assert.calledOnce(eventFactoryMock.scm.getChangedFiles);
});
});
Expand All @@ -898,6 +927,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);
Expand All @@ -919,9 +949,11 @@ 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);
assert.calledWith(eventFactoryMock.scm.getPrInfo, { ...scmConfig, prNum: eventConfig.prNum });
assert.calledOnce(eventFactoryMock.scm.getChangedFiles);
});
});
Expand Down Expand Up @@ -1006,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);
});
});
Expand Down