Skip to content
Open
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: 9 additions & 7 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ function getToken(self, job, pipeline) {
* @param {Object} mergedMeta - The current state of merged metadata
* @returns {Object} The updated merged metadata
*/
async function mergeParentEventMeta(parentEventId, mergedMeta) {
async function mergeParentEventMeta(parentEventId, pipelineId, mergedMeta) {
let resultMeta = mergedMeta;

if (parentEventId) {
Expand All @@ -287,7 +287,9 @@ async function mergeParentEventMeta(parentEventId, mergedMeta) {
const eventFactory = EventFactory.getInstance();
const parentEvent = await eventFactory.get(parentEventId);

resultMeta = _.merge(mergedMeta, parentEvent.meta);
if (pipelineId === parentEvent.pipelineId) {
resultMeta = _.merge(mergedMeta, parentEvent.meta);
}
}

return resultMeta;
Expand Down Expand Up @@ -347,9 +349,10 @@ async function mergeParentBuildsMeta(build, pipelineId, isVirtual, mergedMeta) {
if (parentJob.pipelineId !== pipelineId) {
resultMeta = mergeExternalBuildMeta(mergedMeta, parentJob, parentBuild.meta);
delete parentBuild.meta.parameters;
} else {
// only fetch parent build meta when parent build is not external
resultMeta = _.merge(mergedMeta, parentBuild.meta);
}

resultMeta = _.merge(mergedMeta, parentBuild.meta);
}
}

Expand Down Expand Up @@ -1054,15 +1057,14 @@ class BuildModel extends BaseModel {
let mergedMeta = this.meta ? _.merge({}, this.meta) : {};

const event = await this.event;
const job = await this.job;

mergedMeta = await mergeParentEventMeta(event.parentEventId, mergedMeta);
mergedMeta = await mergeParentEventMeta(event.parentEventId, job.pipelineId, mergedMeta);

if (event.meta) {
mergedMeta = _.merge(mergedMeta, event.meta);
}

const job = await this.job;

if (this.parentBuildId) {
mergedMeta = await mergeParentBuildsMeta(this, job.pipelineId, isVirtualJob(job), mergedMeta);
}
Expand Down
13 changes: 7 additions & 6 deletions test/lib/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,7 @@ describe('Build Model', () => {

// Mock parent event with metadata
eventFactoryMock.get.withArgs(444).resolves({
pipelineId: 1234,
meta: {
meta1: 'set by parent event', // Overwritten by parent build
meta2: 'set by parent event', // Overwritten by parent build
Expand All @@ -2078,6 +2079,7 @@ describe('Build Model', () => {
// Mock own event with metadata
eventFactoryMock.get.withArgs(555).resolves({
parentEventId: 444,
pipelineId: 1234,
meta: {
meta1: 'set by own event', // Overwritten by parent build
meta2: 'set by own event', // Overwritten by parent build
Expand Down Expand Up @@ -2110,28 +2112,29 @@ describe('Build Model', () => {
}
},
{
// Those should not be merged to meta, but remains under meta.sd.*
id: 9000,
jobId: 900,
endTime: '2025-01-01T10:00:00.000Z',
meta: {
meta5: 'set by the external parent build 1', // Overwritten by the newest parent external build
parameters: { param2: 'set by external parent build 1' } // This should be deleted in meta.parameters, but remains under meta.sd.2345.
meta5: 'set by the external parent build 1',
parameters: { param2: 'set by external parent build 1' }
}
},
{
id: 9001,
jobId: 901,
endTime: '2025-01-01T11:00:00.000Z',
meta: {
meta5: 'set by the external parent build 2' // Remains
meta5: 'set by the external parent build 2'
}
},
{
id: 9002,
jobId: 902,
endTime: '2025-01-01T10:30:00.000Z',
meta: {
meta5: 'set by the external parent build 3' // Overwritten by the newest parent external build
meta5: 'set by the external parent build 3'
}
}
]);
Expand Down Expand Up @@ -2175,7 +2178,6 @@ describe('Build Model', () => {
},
2346: { externalJob1: { meta5: 'set by the external parent build 3' } }
},
meta5: 'set by the external parent build 2',
build: {
pipelineId: '1234',
eventId: '555',
Expand Down Expand Up @@ -2317,7 +2319,6 @@ describe('Build Model', () => {
tag: { name: 'test-tag' },
pr: { name: 'test-name', merged: false, number: 222 }
},
foo: 'set by the parent build 2',
build: {
pipelineId: '1234',
eventId: '555',
Expand Down