-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_linear_ids.py
More file actions
161 lines (134 loc) · 4.09 KB
/
Copy pathget_linear_ids.py
File metadata and controls
161 lines (134 loc) · 4.09 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
import sys
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
def prompt_api_key() -> str:
api_key = input("Linear API key: ").strip()
if not api_key:
print("API key is required.")
sys.exit(1)
return api_key
def create_client(api_key: str) -> Client:
transport = RequestsHTTPTransport(
url="https://api.linear.app/graphql",
headers={"Authorization": api_key},
)
return Client(transport=transport, fetch_schema_from_transport=True)
def get_teams(client: Client):
query = gql(
"""
query GetTeams {
teams {
nodes {
id
name
key
}
}
}
"""
)
result = client.execute(query)
return result["teams"]["nodes"]
def get_projects(client: Client, team_id: str):
query = gql(
"""
query GetProjects($teamId: String!) {
team(id: $teamId) {
projects {
nodes {
id
name
}
}
}
}
"""
)
result = client.execute(query, variable_values={"teamId": team_id})
return result["team"]["projects"]["nodes"]
def get_team_data(client: Client, team_id: str):
query = gql(
"""
query GetTeamData($teamId: String!) {
team(id: $teamId) {
states {
nodes {
id
name
type
}
}
labels {
nodes {
id
name
color
}
}
}
}
"""
)
result = client.execute(query, variable_values={"teamId": team_id})
return result["team"]
def choose_team(teams):
for i, team in enumerate(teams, 1):
print(f"{i}. {team['name']} (Key: {team['key']}, ID: {team['id']})")
while True:
choice = input("Select a team (number): ").strip()
if choice.isdigit():
index = int(choice) - 1
if 0 <= index < len(teams):
return teams[index]
print("Invalid choice.")
def main() -> int:
api_key = prompt_api_key()
try:
client = create_client(api_key)
except Exception as exc:
print(f"Failed to connect: {exc}")
return 1
try:
teams = get_teams(client)
except Exception as exc:
print(f"Failed to fetch teams: {exc}")
return 1
if not teams:
print("No teams found.")
return 1
team = choose_team(teams)
team_id = team["id"]
print(f"\nSelected team: {team['name']} (ID: {team_id})")
try:
projects = get_projects(client, team_id)
except Exception as exc:
print(f"Failed to fetch projects: {exc}")
return 1
if projects:
print("\nProjects (optional):")
for project in projects:
print(f"- {project['name']} (ID: {project['id']})")
try:
team_data = get_team_data(client, team_id)
except Exception as exc:
print(f"Failed to fetch team data: {exc}")
return 1
print("\nStates:")
for state in team_data["states"]["nodes"]:
print(f"- {state['name']} (ID: {state['id']}, Type: {state['type']})")
print("\nLabels:")
for label in team_data["labels"]["nodes"]:
print(f"- {label['name']} (ID: {label['id']}, Color: {label['color']})")
print("\n.env entries:")
print("LINEAR_API_KEY=<your_api_key>")
print(f"LINEAR_TEAM_ID={team_id}")
print("LINEAR_PROJECT_ID=<project_id>")
print("LINEAR_STATE_TODO_ID=<state_id>")
print("LINEAR_STATE_BACKLOG_ID=<state_id>")
print("LINEAR_LABEL_URGENT_ID=<label_id>")
print("LINEAR_LABEL_HIGH_PRIORITY_ID=<label_id>")
print("LINEAR_LABEL_MEDIUM_PRIORITY_ID=<label_id>")
print("LINEAR_LABEL_LOW_PRIORITY_ID=<label_id>")
return 0
if __name__ == "__main__":
raise SystemExit(main())