Skip to content

Commit

Permalink
fix: search and replace of special annotations (#735)
Browse files Browse the repository at this point in the history
* Do not replace multiple @ when using author annotation

* Add changeset

* Format changeset following convention

---------

Co-authored-by: Shine Lee <aungshine@gmail.com>
  • Loading branch information
mesaugat and shine2lay committed Mar 2, 2024
1 parent a026f67 commit c6d0d68
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,22 @@ describe('searchAndReplaceSpecialAnnotations', () => {
}
expect(searchAndReplaceSpecialAnnotations('this is @author', payload)).toBe('this is creator')
})

test('@@author is replaced by @payload.user.login', () => {
const payload = {
user: {
login: 'creator'
}
}
expect(searchAndReplaceSpecialAnnotations('this is @@author', payload)).toBe('this is @creator')
})

test('replaces annotation anywhere in the text', () => {
const payload = {
user: {
login: 'creator'
}
}
expect(searchAndReplaceSpecialAnnotations('this is something@author speaking', payload)).toBe('this is somethingcreator speaking')
})
})
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CHANGELOG
=====================================
| Feb 27, 2024: fix: search and replace of special annotations `#735 <https://github.com/mergeability/mergeable/pull/735>`_
| May 12, 2023: fix: Loading teams for team option of author filter/validator `#713 <https://github.com/mergeability/mergeable/pull/713>`_
| May 11, 2023: fix: Send correct payload for changing labels `#715 <https://github.com/mergeability/mergeable/pull/715>`_
| April 25, 2023: feat: Add author validator `#710 <https://github.com/mergeability/mergeable/pull/710>`_
Expand Down
4 changes: 2 additions & 2 deletions lib/actions/lib/searchAndReplaceSpecialAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const searchAndReplaceSpecialAnnotations = (template, payload) => {
let newTemplate = template

for (const annotation of Object.keys(SPECIAL_ANNOTATION)) {
const specialAnnotationRegex = new RegExp(`([^\\\\])${annotation}`)
const specialAnnotationRegex = new RegExp(`(?<!\\\\)${annotation}`)
const annotationAtStartRegex = new RegExp(`^${annotation}`)
const escapeAnnotationRegex = new RegExp(`(\\\\){1}${annotation}`)

newTemplate = newTemplate.replace(specialAnnotationRegex, ` ${SPECIAL_ANNOTATION[annotation](payload)}`)
newTemplate = newTemplate.replace(specialAnnotationRegex, `${SPECIAL_ANNOTATION[annotation](payload)}`)
newTemplate = newTemplate.replace(escapeAnnotationRegex, annotation)
newTemplate = newTemplate.replace(annotationAtStartRegex, SPECIAL_ANNOTATION[annotation](payload))
}
Expand Down

0 comments on commit c6d0d68

Please sign in to comment.