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
Change parse mode #16
Changes from 6 commits
9abd3b9
9499e24
1a235bd
6a1a2ab
60560af
ea7cbbd
ee94413
8e94bfb
8c129ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
require('dotenv').config(); | ||
const { Octokit } = require('@octokit/core'); | ||
const parseGithubUrl = require('parse-github-url'); | ||
const axios = require('axios').default; | ||
const CronJob = require('cron').CronJob; | ||
|
||
|
@@ -9,6 +10,8 @@ const COLUMN_NODE_ID_PR = process.env.COLUMN_NODE_ID_PR; | |
const NOTIFIER_URL = process.env.NOTIFIER_URL; | ||
const MENTION = process.env.MENTION; | ||
const MEETING_MENTION = process.env.MEETING_MENTION; | ||
const PARSE_MODE = 'HTML'; | ||
|
||
/** | ||
* The default cron expression described as: | ||
* At minute 0 past hour 9 and 18 on every day-of-week from Monday through Friday. | ||
|
@@ -35,16 +38,15 @@ const PR_QUERY = require('./queries/pr'); | |
* Sends POST request to telegram bot | ||
* | ||
* @param {string} message - telegram message | ||
* @param {boolean} markdown - telegram message include markdown or not. | ||
* @returns {Promise} - returns a promise to catch error. | ||
*/ | ||
async function notify(message, markdown) { | ||
const botMessage = 'message=' + encodeURIComponent(message) + (markdown ? '&parse_mode=Markdown' : ''); | ||
async function notify(message) { | ||
const messageData = `parse_mode=${PARSE_MODE}&disable_web_page_preview=True&message=${encodeURIComponent(message)}`; | ||
|
||
return axios({ | ||
method: 'POST', | ||
url: NOTIFIER_URL, | ||
data: botMessage, | ||
data: messageData, | ||
}); | ||
} | ||
|
||
|
@@ -83,25 +85,57 @@ function replaceGithubLink(message, markdownLink) { | |
); | ||
} | ||
|
||
/** | ||
* Escape chars in raw string which should not be processed as marked text | ||
* | ||
* List of chars to be transcoded | ||
* https://core.telegram.org/bots/api#html-style | ||
* | ||
* @param {string} message - string to be processed | ||
* @returns {string} | ||
*/ | ||
function escapeChars(message) { | ||
message = message.replace(/</g, '<'); | ||
message = message.replace(/>/g, '>'); | ||
message = message.replace(/&/g, '&'); | ||
|
||
return message; | ||
} | ||
|
||
/** | ||
* Parse github link via jonschlinkert/parse-github-url module | ||
* | ||
* https://github.com/jonschlinkert/parse-github-url | ||
* | ||
* @param {string} url - any github link (to pr or issue for example) | ||
* @returns {string} - HTML marked link to repo | ||
*/ | ||
function createTaskBadge(url) { | ||
const repoInfo = parseGithubUrl(url); | ||
|
||
return `<a href="https://github.com/${repoInfo.repo}"><b>${repoInfo.name}</b></a>`; | ||
} | ||
|
||
/** | ||
* parse the response of GraphQL query for pull request. | ||
* | ||
* @param {object} content - response of GraphQL API. | ||
* @returns {string} - parsed message. | ||
*/ | ||
function pullRequestParser(content) { | ||
let parsedTask = `[${content.title.replace(/[[\]']+/g, '')}](${content.url}) @${content.author.login}`; | ||
|
||
content.reviewRequests.nodes.forEach((node) => { | ||
if (node.requestedReviewer.login) { | ||
parsedTask += `@${node.requestedReviewer.login}`; | ||
} | ||
}); | ||
content.assignees.nodes.forEach((node) => { | ||
if (node.login) { | ||
parsedTask += `@${node.login}`; | ||
} | ||
}); | ||
const parsedTask = `${createTaskBadge(content.url)}: <a href="${content.url}">${escapeChars(content.title)}</a> @${content.author.login}`; | ||
|
||
// content.reviewRequests.nodes.forEach((node) => { | ||
// if (node.requestedReviewer.login) { | ||
// parsedTask += `@${node.requestedReviewer.login}`; | ||
// } | ||
// }); | ||
// | ||
// content.assignees.nodes.forEach((node) => { | ||
// if (node.login) { | ||
// parsedTask += `@${node.login}`; | ||
// } | ||
// }); | ||
Comment on lines
+131
to
+141
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we removed the requested reviewer and assignees There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can discuss it too. i think it is now necessary to duplicate link to pr. u can see repo name and request name and if it is project which u working for u should check it |
||
|
||
return parsedTask; | ||
} | ||
|
@@ -113,7 +147,7 @@ function pullRequestParser(content) { | |
* @returns {string} - parsed message. | ||
*/ | ||
function issuesParser(content) { | ||
let parsedTask = `[${content.title.replace(/[[\]']+/g, '')}](${content.url})`; | ||
let parsedTask = `${createTaskBadge(content.url)}: <a href="${content.url}">${escapeChars(content.title)}</a>`; | ||
|
||
content.assignees.nodes.forEach((node) => { | ||
parsedTask += `@${node.login} `; | ||
|
@@ -180,12 +214,12 @@ async function parseQuery(members, response) { | |
await response.map(async (items) => { | ||
if (items.state === 'NOTE_ONLY') { | ||
for (let i = 0; i < members.length; i++) { | ||
if (items.note.includes(`@${members[i].name}`)) { | ||
if (items.note.includes(`@${members[i].name}`) || items.note.includes(`@${members[i].name.toLowerCase()}`)) { | ||
const parsable = checkForParsableGithubLink(items.note); | ||
|
||
return parsable[0] | ||
? await parseGithubLink(items.note, parsable) | ||
: items.note; | ||
: escapeChars(items.note); | ||
} | ||
} | ||
const parsable = checkForParsableGithubLink(items.note); | ||
|
@@ -209,15 +243,17 @@ async function parseQuery(members, response) { | |
let cardDataWithoutMembers = [ ...parsedCardData ]; | ||
|
||
for (let i = 0; i < members.length; i++) { | ||
cardDataWithoutMembers = cardDataWithoutMembers.map((x) => | ||
x.replace(new RegExp(`@${members[i].name}`, 'g'), '') | ||
); | ||
cardDataWithoutMembers = cardDataWithoutMembers.map((x) => { | ||
const data = x.replace(new RegExp(`@${members[i].name}`, 'g'), ''); | ||
|
||
return data.replace(new RegExp(`@${members[i].name.toLowerCase()}`, 'g'), ''); | ||
}); | ||
} | ||
cardDataWithoutMembers = cardDataWithoutMembers.map((x) => x.replace(/^\s+|\s+$/g, '')); | ||
|
||
parsedCardData.forEach((items, index) => { | ||
for (let i = 0; i < members.length; i++) { | ||
if (items.includes(`@${members[i].name}`)) { | ||
if (items.includes(`@${members[i].name}`) || items.includes(`@${members[i].name.toLowerCase()}`)) { | ||
members[i].tasks.push(cardDataWithoutMembers[index]); | ||
} | ||
} | ||
|
@@ -275,13 +311,18 @@ async function notifyMessage(title, columnID) { | |
); | ||
|
||
parsedData.forEach((items) => { | ||
dataToSend += `@${items.name}`; | ||
dataToSend += '\n'; | ||
/** Skip person with no tasks */ | ||
if (!items.tasks.length) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @talyguryn, Peter told me earlier to remove this, So we can identify a person without a task. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm. well then we can mark these people other way. maybe put them at the end of the message and mark as "no task" or any other way. i did this because i see big message which has only 50% useful information. let's discuss a better design format. |
||
|
||
dataToSend += `<b>${items.name}</b>\n`; | ||
|
||
items.tasks.forEach((data) => { | ||
dataToSend += `${data} \n`; | ||
dataToSend += `• ${data}\n`; | ||
}); | ||
dataToSend += '\n\n'; | ||
|
||
dataToSend += '\n'; | ||
}); | ||
|
||
return dataToSend; | ||
|
@@ -293,8 +334,7 @@ async function notifyMessage(title, columnID) { | |
* @returns {string} -parsed messages | ||
*/ | ||
function parseMeetingMessage(mentionList) { | ||
let message = `☝️ | ||
Join the meeting in Discord!\n`; | ||
let message = `☝️ Join the meeting in Discord!\n\n`; | ||
|
||
mentionList.split(' ').forEach((items) => { | ||
message += `@${items} `; | ||
|
@@ -310,8 +350,8 @@ async function main() { | |
const toDoJob = new CronJob( | ||
TO_DO_TIME, | ||
async () => { | ||
notify(await notifyMessage("📌 Sprint's backlog", COLUMN_NODE_ID_TO_DO), true) | ||
.then(() => console.log('PR Job Completed.')) | ||
notify(await notifyMessage("📌 Sprint's backlog", COLUMN_NODE_ID_TO_DO)) | ||
.then(() => console.log('Tasks Job Completed.')) | ||
.catch(console.error); | ||
}, | ||
null, | ||
|
@@ -322,9 +362,7 @@ async function main() { | |
const prJob = new CronJob( | ||
PR_TIME, | ||
async () => { | ||
notify( | ||
await notifyMessage('🚜 Pull requests for review', COLUMN_NODE_ID_PR, true) | ||
) | ||
notify(await notifyMessage('👀 Pull requests for review', COLUMN_NODE_ID_PR)) | ||
.then(() => console.log('PR Job Completed.')) | ||
.catch(console.error); | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@talyguryn I change this because replaceAll throwing me an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it work correctly for multiple entities?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup