-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeedback.js
42 lines (38 loc) · 1.64 KB
/
feedback.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module.exports = async ({github, context}) => {
const query = `query($owner:String!, $name:String!, $issue_number:Int!) {
repository(owner:$owner, name:$name){
issue(number:$issue_number) {
comments(first:10,orderBy:{direction:DESC, field:UPDATED_AT}) {
nodes {
author {
avatarUrl(size: 24)
login
url
}
url
bodyText
updatedAt
}
}
}
}
}`;
const variables = {
owner: context.repo.owner,
name: context.repo.repo,
issue_number: context.issue.number
}
const result = await github.graphql(query, variables);
const renderComments = (comments) => {
return comments.reduce((prev, curr) => {
let sanitizedText = curr.bodyText.replace('<', '<').replace('>', '>').replace(/(\r\n|\r|\n)/g, "<br />").replace('|', '|').replace('[', '[');
// Convert updatedAt to a date with UTC+7 timezone
let date = new Date(curr.updatedAt);
let formattedDate = date.toLocaleString('en-US', { timeZone: 'Asia/Ho_Chi_Minh' });
return `${prev}|[<img src="${curr.author.avatarUrl}" alt="${curr.author.login}" width="24" /> ${curr.author.login}](${curr.author.url})|${formattedDate} (UTC+7)|${sanitizedText}|\n`;
}, "| Name | Date | Feedback |\n|---|---|---|\n");
};
const fileSystem = require('fs');
const readme = fileSystem.readFileSync('README.md', 'utf8');
fileSystem.writeFileSync('README.md', readme.replace(/(?<=<!-- Feedback -->.*\n)[\S\s]*?(?=<!-- \/Feedback -->|$(?![\n]))/gm, renderComments(result.repository.issue.comments.nodes)), 'utf8');
}