rewriteWorkflowForSingleOpenCiJob function
Implementation
RuntimeWorkflowRewriteResult rewriteWorkflowForSingleOpenCiJob(
String workflowContent,
String? jobKey,
List<String> needs,
Map<String, dynamic>? matrix,
) {
if (jobKey == null || jobKey.isEmpty) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'missing-job-key',
);
}
final hasMatrix = matrix != null && matrix.isNotEmpty;
if (needs.isEmpty && !hasMatrix) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'no-needs-and-no-matrix',
);
}
try {
final doc = loadYaml(workflowContent);
if (doc is! YamlMap) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'parse-error',
);
}
final jobs = doc['jobs'];
if (jobs is! YamlMap) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'jobs-not-map',
);
}
final job = jobs[jobKey];
if (job is! YamlMap) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'job-not-found',
);
}
final editor = YamlEditor(workflowContent);
var rewritten = false;
if (hasMatrix) {
final strategy = job['strategy'];
if (strategy is YamlMap) {
editor.update(
['jobs', jobKey, 'strategy', 'matrix'],
{
'include': [matrix],
},
);
} else {
editor.update(
['jobs', jobKey, 'strategy'],
{
'matrix': {
'include': [matrix],
},
},
);
}
rewritten = true;
}
if (needs.isNotEmpty) {
if (job.containsKey('needs')) {
editor.remove(['jobs', jobKey, 'needs']);
rewritten = true;
}
}
return RuntimeWorkflowRewriteResult(
content: editor.toString(),
rewritten: rewritten,
);
} catch (e) {
return RuntimeWorkflowRewriteResult(
content: workflowContent,
rewritten: false,
reason: 'exception: $e',
);
}
}