Skip to content

Commit

Permalink
Allow Mustache templates in on-update and on-create
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Dec 4, 2022
1 parent 7fd297c commit 08dc66c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 10 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Dictates what should happen if there is no comment on this PR yet. For almost al
- `create` - create a new comment
- `nothing` - do not create a new comment

Also accepts a Mustache template that evaluates to one of the above values.

**Required**: false

**Default**: `create`
Expand All @@ -86,6 +88,8 @@ Dictates what should happen if a comment was already created on this PR, but mor
- `edit` - edit the old comment
- `nothing` - leave the old comment unchanged

Also accepts a Mustache template that evaluates to one of the above values.

**Required**: true

**Default**: `recreate`
Expand Down Expand Up @@ -124,7 +128,7 @@ The text to be included in the PR comment.

##### Templates

Comment snippet bodies (as well as `comment.header` and `comment.footer`) are [Mustache](https://mustache.github.io/mustache.5.html) templates.
Comment snippet bodies and ids (as well as `comment.on-create`, `comment.on-update`, `comment.header`, and `comment.footer`) are [Mustache](https://mustache.github.io/mustache.5.html) templates.

Variables for the template can be provided via the `template-variables` input which should be a string containing a valid JSON.

Expand Down
28 changes: 22 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,38 @@ function validateCommentConfig(configObject, templateVariables) {
const allowedOnCreateValues = ['create', 'nothing'];
if (configObject.comment['on-create'] === undefined || configObject.comment['on-create'] === null) {
configMap.set('onCreate', allowedOnCreateValues[0]);
} else if (allowedOnCreateValues.includes(configObject.comment['on-create'])) {
configMap.set('onCreate', configObject.comment['on-create']);
} else if (typeof configObject.comment['on-create'] === 'string') {
const onCreate = Mustache.render(configObject.comment['on-create'], templateVariables);

if (allowedOnCreateValues.includes(onCreate)) {
configMap.set('onCreate', onCreate);
} else {
throw Error(
`found unexpected value '${onCreate}' under key '.comment.on-create' (should be one of: ${allowedOnCreateValues.join(', ')})`,
);
}
} else {
throw Error(
`found unexpected value '${configObject.comment['on-create']}' under key '.comment.on-create' (should be one of: ${allowedOnCreateValues.join(', ')})`,
`found unexpected value type '${typeof configObject.comment['on-create']}' under key '.comment.on-create' (should be a string)`,
);
}

const allowedOnUpdateValues = ['recreate', 'edit', 'nothing'];
if (configObject.comment['on-update'] === undefined || configObject.comment['on-update'] === null) {
configMap.set('onUpdate', allowedOnUpdateValues[0]);
} else if (allowedOnUpdateValues.includes(configObject.comment['on-update'])) {
configMap.set('onUpdate', configObject.comment['on-update']);
} else if (typeof configObject.comment['on-update'] === 'string') {
const onUpdate = Mustache.render(configObject.comment['on-update'], templateVariables);

if (allowedOnUpdateValues.includes(onUpdate)) {
configMap.set('onUpdate', onUpdate);
} else {
throw Error(
`found unexpected value '${onUpdate}' under key '.comment.on-update' (should be one of: ${allowedOnUpdateValues.join(', ')})`,
);
}
} else {
throw Error(
`found unexpected value '${configObject.comment['on-update']}' under key '.comment.on-update' (should be one of: ${allowedOnUpdateValues.join(', ')})`,
`found unexpected value type '${typeof configObject.comment['on-update']}' under key '.comment.on-update' (should be a string)`,
);
}

Expand Down
96 changes: 93 additions & 3 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,61 @@ describe('validateCommentConfig', () => {
expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value type 'number' under key '\.comment\.footer' \(should be a string\)/);
});

test('on-create must be one of known values', () => {
const input = {
comment: {
'on-create': 'sup',
},
};

expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value 'sup' under key '\.comment\.on-create' \(should be one of: create, nothing\)/);
});

test('on-create must be one a string', () => {
const input = {
comment: {
'on-create': [],
},
};

expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value type 'object' under key '\.comment\.on-create' \(should be a string\)/);
});

test('on-create can be a mustache template', () => {
const input = {
comment: {
'on-create': '{{ onCreate }}',
header: 'hi',
footer: 'bye',
snippets: [snippet2Object],
},
};

const templateVariables = { onCreate: 'nothing' };

const output = new Map([
['onCreate', 'nothing'],
['onUpdate', 'recreate'],
['header', 'hi'],
['footer', 'bye'],
['snippets', [snippet2Map]],
]);

expect(config.validateCommentConfig(input, templateVariables)).toEqual(output);
});

test('on-create must be one of known values after rendering the mustache template', () => {
const input = {
comment: {
'on-create': '{{ onCreate }}',
},
};

const templateVariables = { onCreate: '1234' };

expect(() => config.validateCommentConfig(input, templateVariables)).toThrow(/found unexpected value '1234' under key '\.comment\.on-create' \(should be one of: create, nothing\)/);
});

test('on-update must be one of known values', () => {
const input = {
comment: {
Expand All @@ -216,14 +271,49 @@ describe('validateCommentConfig', () => {
expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value 'whatever' under key '\.comment\.on-update' \(should be one of: recreate, edit, nothing\)/);
});

test('on-create must be one of known values', () => {
test('on-update must be one a string', () => {
const input = {
comment: {
'on-create': 'sup',
'on-update': 123,
},
};

expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value 'sup' under key '\.comment\.on-create' \(should be one of: create, nothing\)/);
expect(() => config.validateCommentConfig(input)).toThrow(/found unexpected value type 'number' under key '\.comment\.on-update' \(should be a string\)/);
});

test('on-update can be a mustache template', () => {
const input = {
comment: {
'on-update': '{{ onUpdate }}',
header: 'hi',
footer: 'bye',
snippets: [snippet2Object],
},
};

const templateVariables = { onUpdate: 'nothing' };

const output = new Map([
['onCreate', 'create'],
['onUpdate', 'nothing'],
['header', 'hi'],
['footer', 'bye'],
['snippets', [snippet2Map]],
]);

expect(config.validateCommentConfig(input, templateVariables)).toEqual(output);
});

test('on-update must be one of known values after rendering the mustache template', () => {
const input = {
comment: {
'on-update': '{{ onUpdate }}',
},
};

const templateVariables = { onUpdate: 'cat' };

expect(() => config.validateCommentConfig(input, templateVariables)).toThrow(/found unexpected value 'cat' under key '\.comment\.on-update' \(should be one of: recreate, edit, nothing\)/);
});

test('glob-options is optional', () => {
Expand Down

0 comments on commit 08dc66c

Please sign in to comment.