-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecreate_team
executable file
·91 lines (80 loc) · 2.55 KB
/
recreate_team
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
#!/usr/bin/env bash
# Load environment variables from .env file
source .env
# Function to check if required environment variables are set
check_env_vars() {
if [ -z "$LINEAR_API_TOKEN" ]; then
echo "Error: LINEAR_API_TOKEN not set. Please source your .env file."
exit 1
fi
if [ -z "$LINEAR_TEAM_NAME" ]; then
echo "Error: LINEAR_TEAM_NAME not set. Please source your .env file."
exit 1
fi
}
# Function to find the team ID
find_team_id() {
curl -s -X POST https://api.linear.app/graphql \
-H "Authorization: $LINEAR_API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"query\":\"{ teams { nodes { id name } } }\"}" | jq -r ".data.teams.nodes[] | select(.name == \"$LINEAR_TEAM_NAME\") | .id"
}
# Function to delete the team
delete_team() {
local team_id="$1"
echo "Deleting team $LINEAR_TEAM_NAME..."
DELETE_RESPONSE=$(curl -s -X POST https://api.linear.app/graphql \
-H "Authorization: $LINEAR_API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"query\":\"mutation { teamDelete(id: \\\"$team_id\\\") { success } }\"}")
if [[ "$DELETE_RESPONSE" == *"\"success\":true"* ]]; then
echo "Successfully deleted team $LINEAR_TEAM_NAME."
else
echo "Error deleting team: $DELETE_RESPONSE"
exit 1
fi
}
# Function to create a new team
create_team() {
echo "Creating new team $LINEAR_TEAM_NAME..."
CREATE_RESPONSE=$(curl -s -X POST https://api.linear.app/graphql \
-H "Authorization: $LINEAR_API_TOKEN" \
-H "Content-Type: application/json" \
--data "{\"query\":\"mutation { teamCreate(input: { name: \\\"$LINEAR_TEAM_NAME\\\" }) { team { id name } } }\"}")
NEW_TEAM_ID=$(echo "$CREATE_RESPONSE" | jq -r '.data.teamCreate.team.id')
if [ -n "$NEW_TEAM_ID" ]; then
echo "Successfully created new team $LINEAR_TEAM_NAME with ID: $NEW_TEAM_ID"
else
echo "Error creating new team: $CREATE_RESPONSE"
exit 1
fi
}
# Main script execution
check_env_vars
# Parse command line options
FORCE=false
while getopts ":f" opt; do
case ${opt} in
f ) FORCE=true ;;
\? ) echo "Usage: $0 [-f]" >&2
exit 1 ;;
esac
done
TEAM_ID=$(find_team_id)
if [ -n "$TEAM_ID" ]; then
echo "Found team $LINEAR_TEAM_NAME with ID: $TEAM_ID"
if [ "$FORCE" = true ]; then
delete_team "$TEAM_ID"
else
read -p "Type 'delete team' to proceed with deletion: " user_input
if [ "$user_input" = "delete team" ]; then
delete_team "$TEAM_ID"
else
echo "Team deletion cancelled."
exit 0
fi
fi
else
echo "Team $LINEAR_TEAM_NAME not found. Proceeding to create a new team."
fi
create_team