-
-
Notifications
You must be signed in to change notification settings - Fork 176
64 lines (57 loc) · 2.52 KB
/
guestbook.yml
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Credit to https://github.com/openscript/openscript :)
on:
issue_comment:
types: [created, edited, deleted]
name: Guestbook
jobs:
update_guestbook:
name: Update guestbook
if: ${{ github.event.issue.title == 'Guestbook' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Update guestbook from comments
uses: actions/github-script@v6
with:
script: |
const truncateString = (str, num) => str.length > num ? str.slice(0, num) + "..." : str;
const query = `query($owner:String!, $name:String!, $issue_number:Int!) {
repository(owner:$owner, name:$name){
issue(number:$issue_number) {
comments(first:5,orderBy:{direction:DESC, field:UPDATED_AT}) {
nodes {
author {
avatarUrl(size: 24)
login
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 text = curr.bodyText.replace(/(\r\n|\r|\n)/g, "<br />").replace('|', '|');
text = truncateString(text, 125);
return `${prev}| <a href="${curr.author.url}"><img width="24" src="${curr.author.avatarUrl}" alt="${curr.author.login}" /> ${curr.author.login}</a> |${new Date(curr.updatedAt).toLocaleString()}|${text}|\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');
- name: Push guestbook update
run: |
git config --global user.name 'Github Actions'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
git commit -am ':sparkles: update guestbook'
git push