|
1 | | - name: Archive Worklog to Notion |
2 | | - 2 |
3 | | - 3 on: |
4 | | - 4 push: |
5 | | - 5 branches: |
6 | | - 6 - main # main 브랜치에 push 될 때만 실행합니다. 필요시 다른 |
7 | | - 브랜치로 변경하세요. |
8 | | - 7 |
9 | | - 8 jobs: |
10 | | - 9 archive: |
11 | | - 10 runs-on: ubuntu-latest |
12 | | - 11 steps: |
13 | | - 12 - name: Checkout code |
14 | | - 13 uses: actions/checkout@v4 |
15 | | - 14 with: |
16 | | - 15 fetch-depth: 0 # 모든 커밋 히스토리를 가져오기 위해 |
17 | | - 필요합니다. |
18 | | - 16 |
19 | | - 17 - name: Set up Python |
20 | | - 18 uses: actions/setup-python@v4 |
21 | | - 19 with: |
22 | | - 20 python-version: '3.10' |
23 | | - 21 |
24 | | - 22 - name: Install dependencies |
25 | | - 23 run: pip install requests |
26 | | - 24 |
27 | | - 25 - name: Process commits and send to Notion |
28 | | - 26 env: |
29 | | - 27 NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }} |
30 | | - 28 NOTION_DATABASE_ID: "25f0778f-fac8-802e-8444-c1a490714fd8" |
31 | | - 29 GITHUB_REPOSITORY: ${{ github.repository }} |
32 | | - 30 run: | |
33 | | - 31 import os |
34 | | - 32 import requests |
35 | | - 33 import json |
36 | | - 34 import subprocess |
37 | | - 35 |
38 | | - 36 api_key = os.getenv('NOTION_API_KEY') |
39 | | - 37 db_id = os.getenv('NOTION_DATABASE_ID') |
40 | | - 38 repo_name = os.getenv('GITHUB_REPOSITORY') |
41 | | - 39 |
42 | | - 40 commits = json.loads('''${{ toJSON(github.event.commits) |
43 | | - }}''') |
44 | | - 41 |
45 | | - 42 headers = { |
46 | | - 43 "Authorization": f"Bearer {api_key}", |
47 | | - 44 "Content-Type": "application/json", |
48 | | - 45 "Notion-Version": "2022-06-28" |
49 | | - 46 } |
50 | | - 47 |
51 | | - 48 for commit in commits: |
52 | | - 49 commit_sha = commit['id'] |
53 | | - 50 commit_message_full = commit['message'] |
54 | | - 51 commit_title = commit_message_full.split('\n')[0] |
55 | | - 52 author_name = commit['author']['name'] |
56 | | - 53 commit_timestamp = commit['timestamp'] |
57 | | - 54 |
58 | | - 55 # 1. 속성값으로 페이지 생성 |
59 | | - 56 page_data = { |
60 | | - 57 "parent": {"database_id": db_id}, |
61 | | - 58 "properties": { |
62 | | - 59 "Commit": {"title": [{"text": {"content": |
63 | | - commit_title}}]}, |
64 | | - 60 "SHA": {"rich_text": [{"text": {"content": |
65 | | - commit_sha}}]}, |
66 | | - 61 "Project": {"select": {"name": repo_name}}, |
67 | | - 62 "Date": {"date": {"start": commit_timestamp}} |
68 | | - 63 } |
69 | | - 64 } |
70 | | - 65 |
71 | | - 66 create_response = |
72 | | - requests.post("https://api.notion.com/v1/pages", headers=headers, |
73 | | - json=page_data) |
74 | | - 67 |
75 | | - 68 if create_response.status_code != 200: |
76 | | - 69 print(f"Failed to create page for commit {commit_sha[ |
77 | | - :7]}.") |
78 | | - 70 print(create_response.text) |
79 | | - 71 continue |
80 | | - 72 |
81 | | - 73 new_page_id = create_response.json()['id'] |
82 | | - 74 print(f"Successfully created page for commit {commit_sha[ |
83 | | - :7]}. Page ID: {new_page_id}") |
84 | | - 75 |
85 | | - 76 # 2. 생성된 페이지에 콘텐츠(블록) 추가 |
86 | | - 77 code_diff = subprocess.run(['git', 'show', commit_sha], |
87 | | - capture_output=True, text=True).stdout |
88 | | - 78 if len(code_diff) > 1990: |
89 | | - 79 code_diff = code_diff[:1990] + "\n..." |
90 | | - 80 |
91 | | - 81 blocks_data = { |
92 | | - 82 "children": [ |
93 | | - 83 { |
94 | | - 84 "object": "block", |
95 | | - 85 "type": "heading_2", |
96 | | - 86 "heading_2": {"rich_text": [{"type": "text", |
97 | | - "text": {"content": "Commit Message"}}]} |
98 | | - 87 }, |
99 | | - 88 { |
100 | | - 89 "object": "block", |
101 | | - 90 "type": "paragraph", |
102 | | - 91 "paragraph": {"rich_text": [{"type": "text", |
103 | | - "text": {"content": commit_message_full}}]} |
104 | | - 92 }, |
105 | | - 93 { |
106 | | - 94 "object": "block", |
107 | | - 95 "type": "heading_2", |
108 | | - 96 "heading_2": {"rich_text": [{"type": "text", |
109 | | - "text": {"content": "Code Diff"}}]} |
110 | | - 97 }, |
111 | | - 98 { |
112 | | - 99 "object": "block", |
113 | | - 100 "type": "code", |
114 | | - 101 "code": { |
115 | | - 102 "rich_text": [{"type": "text", "text": { |
116 | | - "content": code_diff}}], |
117 | | - 103 "language": "diff" |
118 | | - 104 } |
119 | | - 105 } |
120 | | - 106 ] |
121 | | - 107 } |
122 | | - 108 |
123 | | - 109 append_response = |
124 | | - requests.patch(f"https://api.notion.com/v1/blocks/{new_page_id}/childre |
125 | | - n", headers=headers, json=blocks_data) |
126 | | - 110 |
127 | | - 111 if append_response.status_code == 200: |
128 | | - 112 print(f"Successfully appended content to page { |
129 | | - new_page_id}.") |
130 | | - 113 else: |
131 | | - 114 print(f"Failed to append content to page {new_page_id |
132 | | - }.") |
133 | | - 115 print(append_response.text) |
| 1 | +name: Archive Worklog to Notion |
134 | 2 |
|
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + archive: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Checkout code |
| 13 | + uses: actions/checkout@v4 |
| 14 | + with: |
| 15 | + fetch-depth: 0 |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v4 |
| 19 | + with: |
| 20 | + python-version: '3.10' |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: pip install requests |
| 24 | + |
| 25 | + - name: Process commits and send to Notion |
| 26 | + env: |
| 27 | + NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }} |
| 28 | + NOTION_DATABASE_ID: "25f0778f-fac8-802e-8444-c1a490714fd8" |
| 29 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 30 | + run: | |
| 31 | + import os |
| 32 | + import requests |
| 33 | + import json |
| 34 | + import subprocess |
| 35 | +
|
| 36 | + api_key = os.getenv('NOTION_API_KEY') |
| 37 | + db_id = os.getenv('NOTION_DATABASE_ID') |
| 38 | + repo_name = os.getenv('GITHUB_REPOSITORY') |
| 39 | + |
| 40 | + commits = json.loads('''${{ toJSON(github.event.commits) }}''') |
| 41 | +
|
| 42 | + headers = { |
| 43 | + "Authorization": f"Bearer {api_key}", |
| 44 | + "Content-Type": "application/json", |
| 45 | + "Notion-Version": "2022-06-28" |
| 46 | + } |
| 47 | +
|
| 48 | + for commit in commits: |
| 49 | + commit_sha = commit['id'] |
| 50 | + commit_message_full = commit['message'] |
| 51 | + commit_title = commit_message_full.split('\n')[0] |
| 52 | + author_name = commit['author']['name'] |
| 53 | + commit_timestamp = commit['timestamp'] |
| 54 | +
|
| 55 | + # 1. 속성값으로 페이지 생성 |
| 56 | + page_data = { |
| 57 | + "parent": {"database_id": db_id}, |
| 58 | + "properties": { |
| 59 | + "Commit": {"title": [{"text": {"content": commit_title}}]}, |
| 60 | + "SHA": {"rich_text": [{"text": {"content": commit_sha}}]}, |
| 61 | + "Project": {"select": {"name": repo_name}}, |
| 62 | + "Date": {"date": {"start": commit_timestamp}} |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + create_response = requests.post("https://api.notion.com/v1/pages", headers=headers, json=page_data) |
| 67 | + |
| 68 | + if create_response.status_code != 200: |
| 69 | + print(f"Failed to create page for commit {commit_sha[:7]}.") |
| 70 | + print(create_response.text) |
| 71 | + continue |
| 72 | +
|
| 73 | + new_page_id = create_response.json()['id'] |
| 74 | + print(f"Successfully created page for commit {commit_sha[:7]}. Page ID: {new_page_id}") |
| 75 | +
|
| 76 | + # 2. 생성된 페이지에 콘텐츠(블록) 추가 |
| 77 | + code_diff = subprocess.run(['git', 'show', commit_sha], capture_output=True, text=True).stdout |
| 78 | + if len(code_diff) > 1990: |
| 79 | + code_diff = code_diff[:1990] + "\n..." |
| 80 | +
|
| 81 | + blocks_data = { |
| 82 | + "children": [ |
| 83 | + { |
| 84 | + "object": "block", |
| 85 | + "type": "heading_2", |
| 86 | + "heading_2": {"rich_text": [{"type": "text", "text": {"content": "Commit Message"}}]} |
| 87 | + }, |
| 88 | + { |
| 89 | + "object": "block", |
| 90 | + "type": "paragraph", |
| 91 | + "paragraph": {"rich_text": [{"type": "text", "text": {"content": commit_message_full}}]} |
| 92 | + }, |
| 93 | + { |
| 94 | + "object": "block", |
| 95 | + "type": "heading_2", |
| 96 | + "heading_2": {"rich_text": [{"type": "text", "text": {"content": "Code Diff"}}]} |
| 97 | + }, |
| 98 | + { |
| 99 | + "object": "block", |
| 100 | + "type": "code", |
| 101 | + "code": { |
| 102 | + "rich_text": [{"type": "text", "text": {"content": code_diff}}], |
| 103 | + "language": "diff" |
| 104 | + } |
| 105 | + } |
| 106 | + ] |
| 107 | + } |
| 108 | +
|
| 109 | + append_response = requests.patch(f"https://api.notion.com/v1/blocks/{new_page_id}/children", headers=headers, json=blocks_data) |
| 110 | +
|
| 111 | + if append_response.status_code == 200: |
| 112 | + print(f"Successfully appended content to page {new_page_id}.") |
| 113 | + else: |
| 114 | + print(f"Failed to append content to page {new_page_id}.") |
| 115 | + print(append_response.text) |
0 commit comments