forked from openscript/openscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguestbook.js
36 lines (35 loc) · 1.5 KB
/
guestbook.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
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: 48)
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('[', '[');
return `${prev}|[<img src="${curr.author.avatarUrl}" alt="${curr.author.login}" width="48" /><br />${curr.author.login}](${curr.author.url})|${new Date(curr.updatedAt).toLocaleString()}<br />[Comment 🔗](${curr.url})|${sanitizedText}|\n`;
}, "| Name | Date | Message |\n|---|---|---|\n");
};
const fileSystem = require('fs');
const readme = fileSystem.readFileSync('README.md', 'utf8');
fileSystem.writeFileSync('README.md', readme.replace(/(?<=<!-- Guestbook -->.*\n)[\S\s]*?(?=<!-- \/Guestbook -->|$(?![\n]))/gm, renderComments(result.repository.issue.comments.nodes)), 'utf8');
}