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(3128): Allow user to define new jobs which are not part of the pipeline template [2] #165

Merged
merged 1 commit into from
Jun 27, 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
118 changes: 62 additions & 56 deletions lib/phase/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Hoek = require('@hapi/hoek');
const clone = require('clone');
const TEMPLATE_NAME_REGEX_WITH_NAMESPACE =
require('screwdriver-data-schema').config.regex.FULL_TEMPLATE_NAME_WITH_NAMESPACE;
const ALLOWED_JOB_FIELDS_WITH_PIPELINE_TEMPLATE = ['settings', 'requires', 'image', 'environment'];

/**
* Fetch pipeline template
Expand Down Expand Up @@ -70,74 +71,79 @@ function handlePipelineTemplateMergeForJobs(parsedDoc, newPipeline, pipelineTemp
// Check if job names are valid
if (parsedDoc.jobs) {
const pipelineTemplateJobNames = Object.keys(newPipeline.jobs);
const invalidJobNames = [];

Object.keys(parsedDoc.jobs).forEach(jobName => {
if (!pipelineTemplateJobNames.includes(jobName)) {
invalidJobNames.push(jobName);
}
});

if (invalidJobNames.length > 0) {
throw new Error(
`User template has job name(s) that do not exist in pipeline template ${parsedDoc.template}: ${invalidJobNames}`
);
}

// Flatten jobs
const fieldsToFlatten = ['image', 'requires'];

// Flatten jobs
Object.keys(parsedDoc.jobs).forEach(jobName => {
const newJob = clone(pipelineTemplate.config.jobs[jobName]);
const oldJob = clone(parsedDoc.jobs[jobName]);

// Replace
fieldsToFlatten.forEach(key => {
if (oldJob[key]) {
newJob[key] = oldJob[key];
// Case: Job name exists in pipeline template
// Make sure customized jobs that exist in pipeline template
// only have certain fields set and flatten those fields
if (pipelineTemplateJobNames.includes(jobName)) {
const userJobFields = Object.keys(parsedDoc.jobs[jobName]);
const hasExtraField = userJobFields.some(
field => !ALLOWED_JOB_FIELDS_WITH_PIPELINE_TEMPLATE.includes(field)
);

if (hasExtraField) {
throw new Error(
`Job "${jobName}" has unsupported fields in user yaml. Can only set ${ALLOWED_JOB_FIELDS_WITH_PIPELINE_TEMPLATE}.`
);
}
});

// Merge job environment
if (oldJob.environment || newJob.environment) {
newJob.environment = {
...newJob.environment,
...oldJob.environment
};
}

// Replace settings
const yamlSettingsConfig = oldJob.settings || {};
const templateSettingsConfig = newJob.settings || {};
const newSlackSettings = yamlSettingsConfig.slack || {};
const oldSlackSettings = templateSettingsConfig.slack || {};
const newEmailSettings = yamlSettingsConfig.email || {};
const oldEmailSettings = templateSettingsConfig.email || {};

newJob.settings = {
...templateSettingsConfig,
...yamlSettingsConfig
};
if (!isEmpty(oldSlackSettings)) {
newJob.settings = {
...newJob.settings,
slack: {
...oldSlackSettings,
...newSlackSettings
// Replace fields
fieldsToFlatten.forEach(key => {
if (oldJob[key]) {
newJob[key] = oldJob[key];
}
};
}
if (!isEmpty(oldEmailSettings)) {
});

// Merge job environment
if (oldJob.environment || newJob.environment) {
newJob.environment = {
...newJob.environment,
...oldJob.environment
};
}

// Merge settings
const yamlSettingsConfig = oldJob.settings || {};
const templateSettingsConfig = newJob.settings || {};
const newSlackSettings = yamlSettingsConfig.slack || {};
const oldSlackSettings = templateSettingsConfig.slack || {};
const newEmailSettings = yamlSettingsConfig.email || {};
const oldEmailSettings = templateSettingsConfig.email || {};

newJob.settings = {
...newJob.settings,
email: {
...oldEmailSettings,
...newEmailSettings
}
...templateSettingsConfig,
...yamlSettingsConfig
};
}
if (!isEmpty(oldSlackSettings)) {
Copy link
Contributor

@pritamstyz4ever pritamstyz4ever Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems redundant, considering we are already setting the oldSlackSettings to {} based on when templateSettingsConfig.slack is empty.

newJob.settings = {
...newJob.settings,
slack: {
...oldSlackSettings,
...newSlackSettings
}
};
}
if (!isEmpty(oldEmailSettings)) {
newJob.settings = {
...newJob.settings,
email: {
...oldEmailSettings,
...newEmailSettings
}
};
}

newPipeline.jobs[jobName] = newJob;
newPipeline.jobs[jobName] = newJob;
} else {
// Case: Job name does not exist in pipeline template
newPipeline.jobs[jobName] = oldJob;
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/pipeline-template-invalid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ jobs:
- install: npm install
requires:
- ~pr
- ~commit
- ~commit
142 changes: 142 additions & 0 deletions test/data/pipeline-template-with-customized-job-result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"annotations": {
"bar": "template setting",
"screwdriver.cd/restrictPR": "fork"
},
"childPipelines": {
"scmUrls": [
"git@github.com:org/templateSetting.git",
"https://github.com:org/templateSetting2.git"
],
"startAll": true
},
"parameters": {
"arr": [
"a",
"b"
],
"common": {
"description": "template description",
"value": "template setting"
},
"foo": "template setting",
"override": "template setting"
},
"jobs": {
"main": [{
"annotations": {},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
],
"event": [
"$SD_SOURCE_DIR/templateSetting/event"
],
"job": [
"/temp/templateSetting/job/main"
]
},
"image": "node:25",
"commands": [
{
"name": "init",
"command": "npm install"
},
{
"name": "test",
"command": "npm test"
}
],
"environment": {
"BAR": "baz",
"FOO": "foo",
"SD_PIPELINE_TEMPLATE_FULLNAME": "foo/bar",
"SD_PIPELINE_TEMPLATE_NAME": "bar",
"SD_PIPELINE_TEMPLATE_NAMESPACE": "foo",
"SD_PIPELINE_TEMPLATE_VERSION": "1.0.0"
},
"secrets": [
"NPM_TOKEN"
],
"settings": {
"slack": [
"room_a"
]
},
"requires": []
}],
"job1": [{
"annotations": {},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
],
"event": [
"$SD_SOURCE_DIR/templateSetting/event"
],
"job": []
},
"image": "node:10",
"commands": [
{
"name": "init",
"command": "npm install"
},
{
"name": "test",
"command": "npm test"
}
],
"environment": {
"OTHER": "foo",
"BAR": "bar",
"FOO": "foo",
"SD_PIPELINE_TEMPLATE_FULLNAME": "foo/bar",
"SD_PIPELINE_TEMPLATE_NAME": "bar",
"SD_PIPELINE_TEMPLATE_NAMESPACE": "foo",
"SD_PIPELINE_TEMPLATE_VERSION": "1.0.0"
},
"secrets": [
"NPM_TOKEN"
],
"settings": {
"slack": [
"room_b"
]
},
"requires": [
"main"
]
}]
},
"workflowGraph": {
"nodes": [
{ "name": "~pr" },
{ "name": "~commit" },
{ "name": "main" },
{ "name": "job1" }
],
"edges": [
{ "src": "main", "dest": "job1", "join": true }
]
},
"subscribe": {
"scmUrls": [
{
"git@github.com:foo/templateSetting.git#master": [
"~commit",
"~tags",
"~release"
]
},
{
"git@github.com:foo/commonSetting.git#master": [
"~commit",
"~tags",
"~release"
]
}
]
},
"templateVersionId": 111
}
17 changes: 17 additions & 0 deletions test/data/pipeline-template-with-customized-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
template: foo/bar@1.0.0
jobs:
main:
image: node:25
settings:
slack: [room_a]
environment:
BAR: baz
requires: []
job1:
image: node:10
settings:
slack: [room_b]
environment:
OTHER: foo
requires: [main]

46 changes: 37 additions & 9 deletions test/data/pipeline-template-with-new-customized-job-result.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
],
"job": []
},
"image": "node:10",
"image": "node:18",
"commands": [
{
"name": "init",
Expand All @@ -88,7 +88,6 @@
}
],
"environment": {
"OTHER": "foo",
"BAR": "bar",
"FOO": "foo",
"SD_PIPELINE_TEMPLATE_FULLNAME": "foo/bar",
Expand All @@ -99,25 +98,54 @@
"secrets": [
"NPM_TOKEN"
],
"settings": {
"slack": [
"room_b"
]
"settings": {},
"requires": [
"~main"
]
}],
"job2": [{
"annotations": {},
"cache": {
"pipeline": [
"~/templateSetting/pipeline"
],
"event": [
"$SD_SOURCE_DIR/templateSetting/event"
],
"job": []
},
"image": "node:10",
"commands": [
{
"name": "echo",
"command": "echo new step"
}
],
"environment": {
"OTHER": "foo",
"SD_PIPELINE_TEMPLATE_FULLNAME": "foo/bar",
"SD_PIPELINE_TEMPLATE_NAME": "bar",
"SD_PIPELINE_TEMPLATE_NAMESPACE": "foo",
"SD_PIPELINE_TEMPLATE_VERSION": "1.0.0"
},
"settings": {},
"requires": [
"main"
]
],
"secrets": []
}]
},
"workflowGraph": {
"nodes": [
{ "name": "~pr" },
{ "name": "~commit" },
{ "name": "main" },
{ "name": "job1" }
{ "name": "job1" },
{ "name": "job2" }
],
"edges": [
{ "src": "main", "dest": "job1", "join": true }
{ "src": "main", "dest": "job1" },
{ "src": "main", "dest": "job2", "join": true }
]
},
"subscribe": {
Expand Down
Loading