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

fix(3235): Fetch parentEvent meta when create Event #3256

Merged
merged 4 commits into from
Dec 20, 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
16 changes: 14 additions & 2 deletions plugins/events/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,27 @@ module.exports = () => ({

payload.sha = sha;

// If there is parentEvent, pass workflowGraph and sha to payload
// If there is parentEvent, pass workflowGraph, meta and sha to payload
// Skip PR, for PR builds, we should always start from latest commit
if (payload.parentEventId) {
const parentEvent = await eventFactory.get(parentEventId);
let mergedParameters = payload.meta.parameters || {};

payload.baseBranch = parentEvent.baseBranch || null;

// Merge parameters if they exist in the parent event and not in the payload
if (!payload.meta.parameters && parentEvent.meta && parentEvent.meta.parameters) {
payload.meta.parameters = parentEvent.meta.parameters;
mergedParameters = parentEvent.meta.parameters;
}
delete payload.meta.parameters;

// Copy meta from parent event if payload.meta is empty except for the parameters
if (Object.keys(payload.meta).length === 0) {
payload.meta = { ...parentEvent.meta };
}

if (Object.keys(mergedParameters).length > 0) {
payload.meta.parameters = mergedParameters;
}

if (!prNum) {
Expand Down
73 changes: 73 additions & 0 deletions test/plugins/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,79 @@ describe('event plugin test', () => {
});
});

it('returns 201 when it creates an event with parent event which has meta', () => {
eventConfig.parentEventId = parentEventId;
eventConfig.workflowGraph = getEventMock(testEvent).workflowGraph;
eventConfig.sha = getEventMock(testEvent).sha;
eventConfig.baseBranch = 'master';
testEvent.configPipelineSha = 'configPipelineSha';
testEvent.meta = meta;
eventConfig.configPipelineSha = 'configPipelineSha';
eventConfig.meta = meta;
options.payload.parentEventId = parentEventId;
eventFactoryMock.get.withArgs(parentEventId).resolves(getEventMock(testEvent));

return server.inject(options).then(reply => {
expectedLocation = {
host: reply.request.headers.host,
port: reply.request.headers.port,
protocol: reply.request.server.info.protocol,
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);
delete testEvent.configPipelineSha;
});
});

it('returns 201 when it creates an event with custom parameters and parent event which has meta', () => {
eventConfig.parentEventId = parentEventId;
eventConfig.workflowGraph = getEventMock(testEvent).workflowGraph;
eventConfig.sha = getEventMock(testEvent).sha;
eventConfig.baseBranch = 'master';
testEvent.configPipelineSha = 'configPipelineSha';
testEvent.meta = {
parameters: {
user: { value: 'adong' }
},
foo: 'bar',
one: 1
};
eventConfig.configPipelineSha = 'configPipelineSha';
eventConfig.meta = {
parameters: {
user: { value: 'klu' }
},
foo: 'bar',
one: 1
};
options.payload.parentEventId = parentEventId;
options.payload.meta = {
parameters: {
user: { value: 'klu' }
}
};
eventFactoryMock.get.withArgs(parentEventId).resolves(getEventMock(testEvent));

return server.inject(options).then(reply => {
expectedLocation = {
host: reply.request.headers.host,
port: reply.request.headers.port,
protocol: reply.request.server.info.protocol,
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);
delete testEvent.configPipelineSha;
});
});

it('returns 201 when it successfully creates a PR event', () => {
eventConfig.startFrom = 'PR-1:main';
eventConfig.prNum = '1';
Expand Down