Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message template to render #106

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Another hacky way (no code) to find the MS Teams UPN is the following: open MS T

Ignore Pull Requests with that label(s), eg: `no-reminder` or `no-reminder,ignore me` (optional).

### message-template

The message template to render (optional). Default: `Hey {mention}, the PR "{title}" is waiting for your review: {url}`.


## Example usage

```yaml
Expand All @@ -55,6 +60,7 @@ jobs:
channel: '' # Optional, eg: #general
github-provider-map: '' # Optional, eg: DavideViolante:UEABCDEFG,foobar:UAABCDEFG
ignore-label: '' # Optional, eg: no-reminder,ignore me
message-template: '' # Optional, eg: Hey {mention}, the PR "{title}" is waiting for your review: {url}'
```

## Bug or feedback?
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ inputs:
description: 'Ignore Pull Requests with that label'
required: false
default: ''
message-template:
description: 'Message template to render'
required: false
default: 'Hey {mention}, the PR "{title}" is waiting for your review: {url}'
runs:
using: 'node16'
main: 'dist/index.js'
27 changes: 23 additions & 4 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,55 @@
return map;
}

/**

Check failure on line 87 in functions.js

View workflow job for this annotation

GitHub Actions / build

Missing JSDoc @return for function
* Format the message to print
* @param {String} mention Username to mention as the reviewer
* @param {String} title PR title
* @param {String} url PR URL
* @param {String} messageTemplate Message template to render
*/
function formatMessage(mention, title, url, messageTemplate) {
return messageTemplate
.replace('{mention}', mention)

Check failure on line 96 in functions.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 4
.replace('{title}', title)

Check failure on line 97 in functions.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 4
.replace('{url}', url);

Check failure on line 98 in functions.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 4
}

/**

Check failure on line 101 in functions.js

View workflow job for this annotation

GitHub Actions / build

Missing JSDoc for parameter 'messageTemplate'
* Create a pretty message to print
* @param {Array} pr2user Array of Object with these properties { url, title, login }
* @param {Object} github2provider Object containing usernames as properties and IDs as values
* @param {String} provider Service to use: slack or msteams
* @return {String} Pretty message to print
*/
function prettyMessage(pr2user, github2provider, provider) {
function prettyMessage(pr2user, github2provider, provider, messageTemplate) {
if (!messageTemplate) {
messageTemplate = 'Hey {mention}, the PR "{title}" is waiting for your review: {url}';
}

let message = '';
for (const obj of pr2user) {
switch (provider) {
case 'slack': {
const mention = github2provider[obj.login] ?
`<@${github2provider[obj.login]}>` :
`@${obj.login}`;
message += `Hey ${mention}, the PR "${obj.title}" is waiting for your review: ${obj.url}\n`;
message += messageTemplate.for

Check failure on line 120 in functions.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon
Copy link
Owner

@DavideViolante DavideViolante Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this messageTemplate.for? If messageTemplate is a string, .for throws a error

message += formatMessage(mention, title, url, messageTemplate) + "\n";

Check failure on line 121 in functions.js

View workflow job for this annotation

GitHub Actions / build

'title' is not defined

Check failure on line 121 in functions.js

View workflow job for this annotation

GitHub Actions / build

'url' is not defined

Check failure on line 121 in functions.js

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
break;
}
case 'rocket': {
const mention = github2provider[obj.login] ?
`<@${github2provider[obj.login]}>` :
`@${obj.login}`;
message += `Hey ${mention}, the PR "${obj.title}" is waiting for your review: ${obj.url}\n`;
message += formatMessage(mention, title, url, messageTemplate) + "\n";

Check failure on line 128 in functions.js

View workflow job for this annotation

GitHub Actions / build

'title' is not defined
break;
}
case 'msteams': {
const mention = github2provider[obj.login] ?
`<at>${obj.login}</at>` :
`@${obj.login}`;
message += `Hey ${mention}, the PR "${obj.title}" is waiting for your review: [${obj.url}](${obj.url}) \n`;
message += formatMessage(mention, title, url, messageTemplate) + "\n";
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ async function main() {
const channel = core.getInput('channel');
const github2providerString = core.getInput('github-provider-map');
const ignoreLabel = core.getInput('ignore-label');
const messageTemplate = core.getInput('message-template');
core.info('Getting open pull requests...');
const pullRequests = await getPullRequests();
const totalReviewers = await getPullRequestsReviewersCount(pullRequests.data);
Expand All @@ -71,7 +72,7 @@ async function main() {
return core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`);
}
const github2provider = stringToObject(github2providerString);
const messageText = prettyMessage(pr2user, github2provider, provider);
const messageText = prettyMessage(pr2user, github2provider, provider, messageTemplate);
let messageObject;
switch (provider) {
case 'slack':
Expand Down
Loading