This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinear.py
182 lines (162 loc) · 4.61 KB
/
linear.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import requests
import datetime
from apis.qdrant import upsert
from apis.openai import generate_embeddings
from tqdm import tqdm
import dotenv
import os
# Replace YOUR_API_KEY with your Linear API key
api_key = os.environ.get("LINEAR_API_KEY")
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# Set the date after which you want to retrieve issues
# One day ago in this format (YYYY-MM-DDTHH:mm:ss.sssZ)
# date_filter = (datetime.datetime.now() - datetime.timedelta(days=1)).isoformat()
date_filter = "2023-01-01T00:00:00.000Z"
# Convert the date string to a datetime object
date_filter = datetime.datetime.fromisoformat(date_filter.replace("Z", "+00:00"))
query = """
query($cursor: String, $pageSize: Int) {
issues(first: $pageSize, after: $cursor) {
pageInfo {
endCursor
hasNextPage
}
nodes {
id
title
description
state {
name
}
estimate
priorityLabel
creator {
name
}
assignee {
name
}
labels {
nodes {
name
}
}
cycle {
name
}
createdAt
startedAt
completedAt
canceledAt
parent {
id
}
updatedAt
url
}
}
}
"""
def get_issues(date_filter, cursor=None, page_size=50):
query = """
query GetIssues($cursor: String, $pageSize: Int, $updatedAfter: DateTime) {
issues(first: $pageSize, after: $cursor, filter: {updatedAt:{ gt: $updatedAfter}}) {
nodes {
id
title
description
state {
name
}
estimate
priorityLabel
creator {
name
}
assignee {
name
}
labels {
nodes {
name
}
}
cycle {
name
}
createdAt
startedAt
completedAt
canceledAt
parent {
id
}
updatedAt
url
}
pageInfo {
endCursor
hasNextPage
}
}
}
"""
variables = {
"cursor": cursor,
"pageSize": page_size,
"updatedAfter": date_filter.isoformat(),
}
response = requests.post(
"https://api.linear.app/graphql",
json={"query": query, "variables": variables},
headers=headers,
)
data = response.json()
if response.status_code == 200:
issues = data["data"]["issues"]["nodes"]
end_cursor = data["data"]["issues"]["pageInfo"]["endCursor"]
has_next_page = data["data"]["issues"]["pageInfo"]["hasNextPage"]
if has_next_page:
return issues + get_issues(
date_filter, cursor=end_cursor, page_size=page_size
)
else:
return issues
else:
raise Exception(f'Error fetching issues: {data.get("errors", "")}')
def main():
try:
issues = get_issues(date_filter)
for issue in tqdm(issues, total=len(issues)):
print(issue)
labels = ", ".join([label["name"] for label in issue["labels"]["nodes"]])
creator = issue["creator"]["name"] if issue["creator"] else "N/A"
assignee = issue["assignee"]["name"] if issue["assignee"] else "N/A"
parent_id = issue["parent"]["id"] if issue["parent"] else "N/A"
payload = {
"url": issue["url"],
"timestamp": issue["updatedAt"],
"title": issue["title"],
"body": issue["description"] if issue["description"] else "",
"platform": "linear",
"member": creator,
"attributes": {
"status": issue["state"]["name"],
"estimate": issue["estimate"],
"priority": issue["priorityLabel"],
"labels": labels,
"cycle": issue["cycle"]["name"] if issue["cycle"] else "N/A",
"created": issue["createdAt"],
"assignee": assignee,
"parent": parent_id,
},
}
print(payload)
embed = generate_embeddings(payload["title"] + "\n" + payload["body"])
if embed:
upsert([abs(hash(str(issue["id"])))], [payload], [embed])
except Exception as e:
print(e)
raise e
if __name__ == "__main__":
main()