-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_linear_issues.py
More file actions
329 lines (266 loc) · 9.87 KB
/
export_linear_issues.py
File metadata and controls
329 lines (266 loc) · 9.87 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
"""
Linear Issues Export Script
This script retrieves all issues closed in the last 12 months from Linear
and exports them to a CSV file for audit evidence.
Copyright (c) 2025 24Slides
Licensed under the MIT License - see LICENSE file for details.
"""
import os
import sys
import csv
import requests
import time
from datetime import datetime, timedelta
from typing import List, Dict, Optional
# Try to load .env file if python-dotenv is available
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass # python-dotenv not installed, will use system environment variables
# Configuration constants
LINEAR_API_URL = "https://api.linear.app/graphql"
ISSUES_PER_PAGE = 100
OUTPUT_FILENAME = "linear_closed_issues_last_12mo.csv"
MONTHS_BACK = 12
MAX_RETRIES = 5
INITIAL_RETRY_DELAY = 1 # seconds
def get_twelve_months_ago() -> str:
"""
Calculate the date exactly 12 months ago from today.
Returns ISO 8601 formatted datetime string that Linear's API accepts.
"""
today = datetime.now()
twelve_months_ago = today - timedelta(days=365)
# Return in ISO 8601 format with 'Z' suffix for UTC
return twelve_months_ago.strftime("%Y-%m-%dT%H:%M:%S.000Z")
def safe_get_nested(data: Dict, *keys: str, default: str = "") -> str:
"""
Safely get a nested dictionary value.
Args:
data: The dictionary to search
*keys: The keys to traverse
default: Default value if key doesn't exist
Returns:
The value if found, otherwise the default
"""
current = data
for key in keys:
if current is None or not isinstance(current, dict):
return default
current = current.get(key)
return current if current is not None else default
def make_graphql_request(url: str, headers: Dict, query: str, variables: Dict, max_retries: int = MAX_RETRIES) -> Dict:
"""
Make a GraphQL request with exponential backoff retry logic for rate limiting.
Args:
url: GraphQL endpoint URL
headers: Request headers including authorization
query: GraphQL query string
variables: Query variables
max_retries: Maximum number of retry attempts
Returns:
JSON response data
Raises:
requests.exceptions.RequestException: If request fails after all retries
ValueError: If response contains GraphQL errors
"""
for attempt in range(max_retries):
try:
response = requests.post(
url,
json={"query": query, "variables": variables},
headers=headers,
timeout=30
)
# Handle rate limiting (HTTP 429)
if response.status_code == 429:
if attempt < max_retries - 1:
retry_delay = INITIAL_RETRY_DELAY * (2 ** attempt) # Exponential backoff
print(f"Rate limit hit. Retrying in {retry_delay} seconds... (Attempt {attempt + 1}/{max_retries})")
time.sleep(retry_delay)
continue
else:
raise requests.exceptions.RequestException("Rate limit exceeded after maximum retries")
# Handle other non-200 status codes
if response.status_code != 200:
error_msg = f"API request failed with status {response.status_code}: {response.text}"
raise requests.exceptions.RequestException(error_msg)
data = response.json()
# Check for GraphQL errors
if "errors" in data:
error_msg = f"GraphQL errors: {data['errors']}"
raise ValueError(error_msg)
# Validate response structure
if "data" not in data:
raise ValueError("Invalid response structure from Linear API")
return data
except requests.exceptions.Timeout:
if attempt < max_retries - 1:
retry_delay = INITIAL_RETRY_DELAY * (2 ** attempt)
print(f"Request timeout. Retrying in {retry_delay} seconds... (Attempt {attempt + 1}/{max_retries})")
time.sleep(retry_delay)
continue
else:
raise
except requests.exceptions.ConnectionError:
if attempt < max_retries - 1:
retry_delay = INITIAL_RETRY_DELAY * (2 ** attempt)
print(f"Connection error. Retrying in {retry_delay} seconds... (Attempt {attempt + 1}/{max_retries})")
time.sleep(retry_delay)
continue
else:
raise
raise requests.exceptions.RequestException("Failed after maximum retries")
def fetch_closed_issues(api_key: str) -> List[Dict]:
"""
Fetch all closed issues from Linear API with pagination.
Args:
api_key: Linear API key for authentication
Returns:
List of issues with all required fields
Raises:
requests.exceptions.RequestException: If API request fails
ValueError: If API returns invalid data
"""
headers = {
"Authorization": api_key,
"Content-Type": "application/json"
}
twelve_months_ago = get_twelve_months_ago()
# GraphQL query with pagination support
query = """
query ClosedIssues($cursor: String, $completedAfter: DateTimeOrDuration!, $first: Int!) {
issues(
first: $first
after: $cursor
filter: {
completedAt: { gte: $completedAfter }
state: { type: { in: ["completed", "canceled"] } }
}
) {
nodes {
identifier
title
completedAt
url
creator {
name
}
assignee {
name
}
team {
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
"""
all_issues = []
has_next_page = True
cursor = None
page_count = 0
print(f"Fetching issues completed after: {twelve_months_ago}")
print("=" * 60)
while has_next_page:
variables = {
"cursor": cursor,
"completedAfter": twelve_months_ago,
"first": ISSUES_PER_PAGE
}
# Make request with retry logic
data = make_graphql_request(LINEAR_API_URL, headers, query, variables)
if "issues" not in data["data"]:
raise ValueError("Invalid response structure from Linear API")
issues_data = data["data"]["issues"]
issues = issues_data["nodes"]
page_info = issues_data["pageInfo"]
all_issues.extend(issues)
page_count += 1
print(f"Page {page_count}: Fetched {len(issues)} issues (Total: {len(all_issues)})")
has_next_page = page_info["hasNextPage"]
cursor = page_info["endCursor"]
print("=" * 60)
print(f"Total issues fetched: {len(all_issues)}")
return all_issues
def export_to_csv(issues: List[Dict], filename: str) -> None:
"""
Export issues to a CSV file.
Args:
issues: List of issue dictionaries
filename: Output CSV filename
Raises:
IOError: If file cannot be written
"""
if not issues:
print("No issues to export.")
return
fieldnames = [
"identifier",
"title",
"completedAt",
"creator_name",
"assignee_name",
"url",
"team_name"
]
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for issue in issues:
row = {
"identifier": issue.get("identifier", ""),
"title": issue.get("title", ""),
"completedAt": issue.get("completedAt", ""),
"creator_name": safe_get_nested(issue, "creator", "name"),
"assignee_name": safe_get_nested(issue, "assignee", "name"),
"url": issue.get("url", ""),
"team_name": safe_get_nested(issue, "team", "name")
}
writer.writerow(row)
print(f"Successfully exported {len(issues)} issues to {filename}")
def main() -> None:
"""Main execution function."""
# Get API key from environment (or .env file if python-dotenv is installed)
api_key = os.environ.get("LINEAR_API_KEY")
if not api_key:
print("Error: LINEAR_API_KEY environment variable is not set.")
print("\nOptions:")
print("1. Set it directly: export LINEAR_API_KEY='your-api-key-here'")
print("2. Create a .env file: cp env.template .env (then edit with your key)")
sys.exit(1)
if api_key == "your-api-key-here":
print("Error: Please replace 'your-api-key-here' with your actual Linear API key.")
sys.exit(1)
print("Linear Issues Export - Last 12 Months")
print("=" * 60)
try:
# Fetch all closed issues
issues = fetch_closed_issues(api_key)
# Export to CSV
export_to_csv(issues, OUTPUT_FILENAME)
print("=" * 60)
print("Export completed successfully!")
except requests.exceptions.RequestException as e:
print(f"Network error: {e}", file=sys.stderr)
sys.exit(1)
except ValueError as e:
print(f"Data error: {e}", file=sys.stderr)
sys.exit(1)
except IOError as e:
print(f"File error: {e}", file=sys.stderr)
sys.exit(1)
except KeyboardInterrupt:
print("\nOperation cancelled by user.")
sys.exit(130)
except Exception as e:
print(f"Unexpected error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()