-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_notion_integration.py
147 lines (121 loc) · 4.86 KB
/
github_notion_integration.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import requests
from datetime import datetime, timedelta
import dateutil.parser
import pytz
import openai
GITHUB_TOKEN = "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"
GITHUB_API_URL = "https://api.github.com"
GITHUB_REPOSITORY = "mikezamayias/healpen"
NOTION_API_URL = "https://api.notion.com/v1"
NOTION_TOKEN = "secret_ucvdiN832KtFsGb6WPgXaKQOqUv5ulTDTy3ovCtrHcg"
NOTION_DATABASE_ID = "e6bd47eb50a7471c86a3343728c42081"
OPENAI_API_KEY = "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"
# Set your OpenAI API key
openai.api_key = OPENAI_API_KEY
github_headers = {"Authorization": f"token {GITHUB_TOKEN}"}
notion_headers = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
}
def get_database_entries():
response = requests.post(
f"{NOTION_API_URL}/databases/{NOTION_DATABASE_ID}/query",
headers=notion_headers,
)
pages = response.json().get("results", [])
entries = {}
for page in pages:
date_property = (
page.get("properties", {}).get("Date", {}).get("date", {}).get("start", "")
)
date = dateutil.parser.parse(date_property)
entries[date.strftime("%Y-%m-%d")] = page["id"]
return entries
def generate_summary(commit_messages):
prompt = "Summarize the work done in these commits in a concise and straightforward way. The summary should be no more than 18 words and in Greek language. Ensure accuracy of spelling, grammar, and punctuation, while preserving the original meaning and length of the text. Speak with a confident, formal, and friendly tone."
chat_message = {
"role": "system",
"content": "\n".join(commit_messages) + f"\n\n{prompt}",
}
response = openai.ChatCompletion.create(
model="gpt-4", messages=[chat_message], max_tokens=100, temperature=0.1
)
summary = response.choices[0].message.content.strip()
return summary
def create_database_entry(date, commits):
if not commits:
payload = {
"parent": {"database_id": NOTION_DATABASE_ID},
"properties": {
"title": {"title": [{"text": {"content": "Απουσία"}}]},
"Date": {"date": {"start": date.strftime("%Y-%m-%d")}},
},
}
else:
commit_urls = [commit["html_url"] for commit in commits]
commit_messages = [commit["commit"]["message"] for commit in commits]
content = "\n".join(commit_messages)
summary = generate_summary(commit_messages)
children = []
for url, message in zip(commit_urls, commit_messages):
child_block = {
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [
{
"type": "text",
"text": {"content": message, "link": {"url": url}},
},
],
},
}
children.append(child_block)
payload = {
"parent": {"database_id": NOTION_DATABASE_ID},
"properties": {
"title": {"title": [{"text": {"content": summary}}]},
"Date": {"date": {"start": date.strftime("%Y-%m-%d")}},
},
"children": children,
}
print(f"Creating database entry for {date.strftime('%Y-%m-%d')}")
print("Payload:")
print(payload)
response = requests.post(
f"{NOTION_API_URL}/pages",
json=payload,
headers=notion_headers,
)
if response.status_code == 200:
print(f"Created database entry for {date.strftime('%Y-%m-%d')}")
else:
print(f"Failed to create database entry for {date.strftime('%Y-%m-%d')}")
print(response.text)
database_entries = get_database_entries()
start_date = datetime(2023, 6, 1)
end_date = datetime(2023, 11, 30)
delta = timedelta(days=1)
while start_date <= end_date:
date_str = start_date.strftime("%Y-%m-%d")
if date_str not in database_entries:
since = start_date.isoformat()
until = (start_date + delta).isoformat()
response = requests.get(
f"{GITHUB_API_URL}/repos/{GITHUB_REPOSITORY}/commits?since={since}&until={until}",
headers=github_headers,
)
commits = sorted(response.json(), key=lambda c: c["commit"]["author"]["date"])
if commits:
commit_texts = [commit["commit"]["message"] for commit in commits]
summary = generate_summary(commit_texts)
print(f"Generated summary for {date_str}")
print(summary)
create_database_entry(start_date, commits)
else:
print(f"No commits available for {date_str}.")
create_database_entry(start_date, commits)
start_date += delta
print(f"Processed {date_str}")
print("Script execution completed.")