Skip to content

Commit 9ad4c99

Browse files
authored
Merge pull request #48 from screwdriver-cd/parse
fix(757): add parseAnnotations function
2 parents 93e1068 + 2e2661a commit 9ad4c99

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
const Joi = require('joi');
55
const dataSchema = require('screwdriver-data-schema');
66
const executorSchema = dataSchema.plugins.executor;
7+
const ANNOTATIONS = [
8+
'screwdriver.cd/cpu',
9+
'screwdriver.cd/ram',
10+
'screwdriver.cd/disk',
11+
'screwdriver.cd/timeout',
12+
'screwdriver.cd/executor',
13+
'screwdriver.cd/buildPeriodically',
14+
'screwdriver.cd/repoManifest'
15+
];
16+
const annotationRe = /screwdriver.cd\/(\w+)/;
717

818
/**
919
* Validate the config using the schema
@@ -121,6 +131,26 @@ class Executor {
121131
stats() {
122132
return {};
123133
}
134+
135+
/**
136+
* Parsed annotations object
137+
* @method parseAnnotations
138+
* @return {Object} object Object contains parsed annotations
139+
*/
140+
parseAnnotations(annotations) {
141+
const parsedAnnotations = {};
142+
143+
Object.keys(annotations).forEach((key) => {
144+
const parsedKey = key.replace(/^beta./, '');
145+
146+
if (ANNOTATIONS.includes(parsedKey)) {
147+
// First group will be the part after slash, e.g. cpu, ram, disk
148+
parsedAnnotations[parsedKey.match(annotationRe)[1]] = annotations[key];
149+
}
150+
});
151+
152+
return parsedAnnotations;
153+
}
124154
}
125155

126156
module.exports = Executor;

test/index.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ describe('index test', () => {
134134
})
135135
));
136136

137+
it('parseAnnotations returns a parsed object', () => {
138+
const parsed = instance.parseAnnotations({
139+
'beta.screwdriver.cd/cpu': 'HIGH',
140+
'beta.screwdriver.cd/ram': 'LOW',
141+
'screwdriver.cd/disk': 'HIGH',
142+
'invald.screwdriver.cd': 'invalid'
143+
});
144+
145+
assert.deepEqual(parsed, {
146+
cpu: 'HIGH',
147+
ram: 'LOW',
148+
disk: 'HIGH'
149+
});
150+
});
151+
137152
it('can be extended', () => {
138153
class Foo extends Executor {
139154
_stop(config) {

0 commit comments

Comments
 (0)