-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
104 lines (92 loc) · 2.93 KB
/
sync.py
File metadata and controls
104 lines (92 loc) · 2.93 KB
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
import json
import requests
import os
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
TRELLO_KEY = os.getenv("TRELLO_KEY")
TRELLO_TOKEN = os.getenv("TRELLO_TOKEN")
project_ids = json.loads(os.getenv("GITHUB_PROJECT_ID", "[]"))
TRELLO_LIST_ID = os.getenv("TRELLO_LIST_ID")
POSTED_IDS_FILE = ".cache/posted_ids.json"
HEADERS = {"Authorization": f"Bearer {GITHUB_TOKEN}"}
GRAPHQL_URL = "https://api.github.com/graphql"
def load_posted_ids():
if os.path.exists(POSTED_IDS_FILE):
with open(POSTED_IDS_FILE, "r") as f:
return set(json.load(f))
return set()
def save_posted_ids(ids):
os.makedirs(os.path.dirname(POSTED_IDS_FILE), exist_ok=True)
with open(POSTED_IDS_FILE, "w") as f:
json.dump(list(ids), f)
def fetch_project_items(project_id):
query = """
query($id: ID!) {
node(id: $id) {
... on ProjectV2 {
items(first: 20) {
nodes {
id
content {
... on Issue {
title
url
body
}
... on DraftIssue {
title
body
}
}
}
}
}
}
}
"""
variables = {"id": project_id}
print("Sending GraphQL query to GitHub...")
response = requests.post(
GRAPHQL_URL,
json={"query": query, "variables": variables},
headers=HEADERS
)
response.raise_for_status()
print("🔍 Raw GraphQL response:")
print(json.dumps(response.json(), indent=2))
return response.json()["data"]["node"]["items"]["nodes"]
def post_to_trello(title, description):
url = "https://api.trello.com/1/cards"
params = {
"key": TRELLO_KEY,
"token": TRELLO_TOKEN,
"idList": TRELLO_LIST_ID,
"name": title,
"desc": description
}
requests.post(url, params=params)
def main():
posted_ids = load_posted_ids()
original_count = len(posted_ids)
print(f"Loaded 🔁 Previously synced IDs: {original_count}")
print("🔍 Project IDs:", project_ids)
for project_id in project_ids:
print(f"📌 Syncing project: {project_id}")
items = fetch_project_items(project_id)
for item in items:
gid = item["id"]
content = item["content"]
if gid not in posted_ids and content:
title = content["title"]
body = content.get("body", "")
url = content.get("url", "")
desc = f"{body}\n\n{url}"
print(f"🟢 Posting new card: {title}")
post_to_trello(title, desc)
posted_ids.add(gid)
else:
print(f"⏩ Skipping already posted item: {gid}")
if len(posted_ids) > original_count:
save_posted_ids(posted_ids)
print(f"✅ Sync complete. Total posted IDs: {len(posted_ids)}")
if __name__ == "__main__":
main()