Skip to content

Commit

Permalink
Changelog & build
Browse files Browse the repository at this point in the history
  • Loading branch information
angelikatyborska committed Dec 4, 2022
1 parent 08dc66c commit 2f4e14e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add a new config option `.comment.on-create`.
- Add support for Mustache templates in the config option `.comment.on-update`.

## 1.3.1 (2022-11-25)

- Bump Node version to 16.
Expand Down
47 changes: 37 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ function newCommentWouldHaveContent(snippetIds) {
}

function shouldPostNewComment(previousComment, snippetIds, commentConfig) {
return newCommentWouldHaveContent(snippetIds) && (
!previousComment || (
!!previousComment
&& commentConfig.get('onUpdate') === 'recreate'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds)
)
);
const isNotEmpty = newCommentWouldHaveContent(snippetIds);
const isCreating = !previousComment && commentConfig.get('onCreate') === 'create';
const isUpdating = !!previousComment
&& commentConfig.get('onUpdate') === 'recreate'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds);

return isNotEmpty && (isCreating || isUpdating);
}

function shouldDeletePreviousComment(previousComment, snippetIds, commentConfig) {
Expand Down Expand Up @@ -111,14 +111,41 @@ 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 (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 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

0 comments on commit 2f4e14e

Please sign in to comment.