-
Notifications
You must be signed in to change notification settings - Fork 1
98 lines (94 loc) · 2.95 KB
/
sync-meeting-notes-pr.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: HackMD Sync Meeting Notes Action
permissions:
contents: write
pull-requests: write
on:
workflow_call:
inputs:
pr_number:
description: PR number being synced
required: true
type: string
secrets:
GITHUB_USER_TOKEN:
description: A token with sufficient permissions to commit and push to the target repository
required: false
HACKMD_TOKEN:
description: API token for HackMD.io
required: true
jobs:
main:
name: Sync notes from HackMD
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: "3.9"
- name: Set up dependencies
run: |
python3 -V
python3 -m pip install requests
- name: Get PR description
id: pr-description
uses: actions/github-script@v6
with:
result-encoding: string
script: |
let env = process.env
const resp = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
return resp.data.body
- name: Parse PR description
run: |
python3 <<EOF
import os
from pathlib import Path
result = """
${{ steps.pr-description.outputs.result }}
""".strip()
for line in result.splitlines():
line = line.strip()
if line.startswith(("HACKMDID=", "DOCPATH=")):
key, value = line.split("=", 1)
with open(os.environ['GITHUB_ENV'], 'a') as f:
f.write(f"{key}={value}\n")
EOF
- name: Sync from HackMD
if: ${{ !inputs.dry_run }}
env:
HACKMD_TOKEN: ${{ secrets.HACKMD_TOKEN }}
run: |
python3 <<EOF
import os
from pathlib import Path
import requests
url = f"https://api.hackmd.io/v1/notes/{os.environ['HACKMDID']}"
headers = {"Authorization": f"Bearer $HACKMD_TOKEN"}
r = requests.get(url, headers=headers)
if not r.ok:
print(r.content)
r.raise_for_status()
data = r.json()
print(data)
Path(os.environ['DOCPATH']).write_text(data['content'])
EOF
- name: Commit changes (if any)
run: |
set -x
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
git status
if ! git diff --exit-code "$DOCPATH"; then
git add "$DOCPATH"
git commit -m "Update $DOCPATH"
git push origin ${{ github.head_ref || github.ref_name }}
else
echo "No changes detected."
fi