Skip to content

Merge pull request #6 from Rama-Moon/test/change-title #6

Merge pull request #6 from Rama-Moon/test/change-title

Merge pull request #6 from Rama-Moon/test/change-title #6

Workflow file for this run

name: Archive Worklog to Notion
on:
push:
branches:
- main
jobs:
archive:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install requests
- name: Process commits and send to Notion
env:
NOTION_API_KEY: ${{ secrets.NOTION_API_KEY }}
NOTION_DATABASE_ID: "25f0778f-fac8-802e-8444-c1a490714fd8"
GITHUB_REPOSITORY: ${{ github.repository }}
shell: python
run: |
import os
import requests
import json
import subprocess
api_key = os.getenv('NOTION_API_KEY')
db_id = os.getenv('NOTION_DATABASE_ID')
repo_name = os.getenv('GITHUB_REPOSITORY')
commits = json.loads('''${{ toJSON(github.event.commits) }}''')
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
for commit in commits:
commit_sha = commit['id']
commit_message_full = commit['message']
commit_title = commit_message_full.split('\n')[0]
author_name = commit['author']['name']
commit_timestamp = commit['timestamp']
# 1. 속성값으로 페이지 생성
page_data = {
"parent": {"database_id": db_id},
"properties": {
"Commit": {"title": [{"text": {"content": commit_title}}]},
"SHA": {"rich_text": [{"text": {"content": commit_sha}}]},
"Project": {"select": {"name": repo_name}},
"Date": {"date": {"start": commit_timestamp}}
}
}
create_response = requests.post("https://api.notion.com/v1/pages", headers=headers, json=page_data)
if create_response.status_code != 200:
print(f"Failed to create page for commit {commit_sha[:7]}.")
print(create_response.text)
continue
new_page_id = create_response.json()['id']
print(f"Successfully created page for commit {commit_sha[:7]}. Page ID: {new_page_id}")
# 2. 생성된 페이지에 콘텐츠(블록) 추가
code_diff = subprocess.run(['git', 'show', commit_sha], capture_output=True, text=True).stdout
if len(code_diff) > 1990:
code_diff = code_diff[:1990] + "\n..."
blocks_data = {
"children": [
{
"object": "block",
"type": "heading_2",
"heading_2": {"rich_text": [{"type": "text", "text": {"content": "Commit Message"}}]}
},
{
"object": "block",
"type": "paragraph",
"paragraph": {"rich_text": [{"type": "text", "text": {"content": commit_message_full}}]}
},
{
"object": "block",
"type": "heading_2",
"heading_2": {"rich_text": [{"type": "text", "text": {"content": "Code Diff"}}]}
},
{
"object": "block",
"type": "code",
"code": {
"rich_text": [{"type": "text", "text": {"content": code_diff}}],
"language": "diff"
}
}
]
}
append_response = requests.patch(f"https://api.notion.com/v1/blocks/{new_page_id}/children", headers=headers, json=blocks_data)
if append_response.status_code == 200:
print(f"Successfully appended content to page {new_page_id}.")
else:
print(f"Failed to append content to page {new_page_id}.")
print(append_response.text)