-
Notifications
You must be signed in to change notification settings - Fork 45
187 lines (158 loc) · 6.67 KB
/
youtrack-export-all.yml
File metadata and controls
187 lines (158 loc) · 6.67 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
name: Export All Issues to YouTrack
on:
workflow_dispatch:
jobs:
export-all-issues:
name: Export all issues to YouTrack
runs-on: ubuntu-latest
env:
YOUTRACK_URL: https://youtrack.jetbrains.com
YOUTRACK_TOKEN: ${{ secrets.YOUTRACK_TOKEN }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Resolve github-issue tag ID
id: resolve-tag
run: |
TAG_ID=$(curl -sf \
-H "Authorization: Bearer $YOUTRACK_TOKEN" \
"$YOUTRACK_URL/api/tags?fields=id,name&query=github-issue" \
| jq -r '.[0].id')
if [ -z "$TAG_ID" ] || [ "$TAG_ID" = "null" ]; then
echo "Tag not found, creating..."
TAG_ID=$(curl -sf \
-X POST \
-H "Authorization: Bearer $YOUTRACK_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name": "github-issue"}' \
"$YOUTRACK_URL/api/tags?fields=id" \
| jq -r '.id')
fi
echo "tag_id=$TAG_ID" >> "$GITHUB_OUTPUT"
echo "Resolved tag ID: $TAG_ID"
- name: Fetch and export all issues
shell: bash {0}
env:
TAG_ID: ${{ steps.resolve-tag.outputs.tag_id }}
run: |
PAGE=1
EXPORTED=0
SKIPPED=0
FAILED=0
echo "Fetching issues from ${{ github.repository }}..."
while true; do
echo "--- Fetching page $PAGE ---"
ISSUES=$(gh api "repos/${{ github.repository }}/issues?state=open&per_page=100&page=$PAGE&sort=created&direction=asc" \
--jq '[.[] | select(.pull_request == null)]')
if [ $? -ne 0 ]; then
echo "::error::Failed to fetch issues page $PAGE"
exit 1
fi
COUNT=$(echo "$ISSUES" | jq 'length')
echo "Found $COUNT issue(s) on page $PAGE"
if [ "$COUNT" -eq 0 ]; then
break
fi
for i in $(seq 0 $((COUNT - 1))); do
ISSUE=$(echo "$ISSUES" | jq ".[$i]")
ISSUE_TITLE=$(echo "$ISSUE" | jq -r '.title')
ISSUE_BODY=$(echo "$ISSUE" | jq -r '.body // ""')
ISSUE_URL=$(echo "$ISSUE" | jq -r '.html_url')
ISSUE_NUMBER=$(echo "$ISSUE" | jq -r '.number')
LABELS=$(echo "$ISSUE" | jq -r '[.labels[].name] | join(",")')
echo ""
echo "Processing issue #$ISSUE_NUMBER: '$ISSUE_TITLE' [labels: $LABELS]"
# Skip Dependency Dashboard (issue #8)
if [ "$ISSUE_NUMBER" = "8" ]; then
echo " -> Skipping (Dependency Dashboard)"
SKIPPED=$((SKIPPED + 1))
continue
fi
# Map type
if echo "$LABELS" | grep -q "bug"; then
YT_TYPE="Bug"
elif echo "$LABELS" | grep -q "feature"; then
YT_TYPE="Feature"
else
YT_TYPE="Task"
fi
echo " -> Mapped type: $YT_TYPE"
# Check if already exported by searching YouTrack
echo " -> Checking for duplicates in YouTrack..."
SEARCH_RESULT=$(curl -s \
-H "Authorization: Bearer $YOUTRACK_TOKEN" \
-G --data-urlencode "query=project: KRPC tag: github-issue summary: [GitHub] $ISSUE_TITLE" \
"$YOUTRACK_URL/api/issues?fields=idReadable&\$top=1")
EXISTING=$(echo "$SEARCH_RESULT" | jq 'if type == "array" then length else 0 end' 2>/dev/null || echo "0")
if [ "$EXISTING" -gt 0 ]; then
EXISTING_ID=$(echo "$SEARCH_RESULT" | jq -r '.[0].idReadable')
echo " -> Skipping (already exists as $EXISTING_ID)"
SKIPPED=$((SKIPPED + 1))
continue
fi
# Create issue
DESCRIPTION="$(printf 'Original GitHub issue: %s\n\n---\n\n%s' "$ISSUE_URL" "$ISSUE_BODY")"
PAYLOAD=$(jq -n \
--arg summary "[GitHub] $ISSUE_TITLE" \
--arg description "$DESCRIPTION" \
--arg type "$YT_TYPE" \
'{
project: { shortName: "KRPC" },
summary: $summary,
description: $description,
customFields: [
{
name: "Type",
"$type": "SingleEnumIssueCustomField",
value: { name: $type }
},
{
name: "Scope",
"$type": "MultiEnumIssueCustomField",
value: [{ name: "Other" }]
}
]
}')
echo " -> Creating YouTrack issue..."
RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST \
-H "Authorization: Bearer $YOUTRACK_TOKEN" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" \
"$YOUTRACK_URL/api/issues?fields=idReadable")
HTTP_CODE=$(echo "$RESPONSE" | tail -1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" -lt 200 ] || [ "$HTTP_CODE" -ge 300 ]; then
echo " -> FAILED to create (HTTP $HTTP_CODE): $BODY"
FAILED=$((FAILED + 1))
continue
fi
YT_ID=$(echo "$BODY" | jq -r '.idReadable')
echo " -> Created: $YT_ID"
# Tag issue
echo " -> Tagging $YT_ID with 'github-issue' (tag ID: $TAG_ID)..."
TAG_RESPONSE=$(curl -s -w "\n%{http_code}" \
-X POST \
-H "Authorization: Bearer $YOUTRACK_TOKEN" \
-H "Content-Type: application/json" \
-d "{\"id\": \"$TAG_ID\"}" \
"$YOUTRACK_URL/api/issues/$YT_ID/tags")
TAG_HTTP=$(echo "$TAG_RESPONSE" | tail -1)
TAG_BODY=$(echo "$TAG_RESPONSE" | sed '$d')
if [ "$TAG_HTTP" -lt 200 ] || [ "$TAG_HTTP" -ge 300 ]; then
echo " -> WARNING: Failed to tag $YT_ID (HTTP $TAG_HTTP): $TAG_BODY"
else
echo " -> Tagged successfully"
fi
echo " -> Exported #$ISSUE_NUMBER -> $YT_ID (Type: $YT_TYPE)"
EXPORTED=$((EXPORTED + 1))
done
PAGE=$((PAGE + 1))
done
echo ""
echo "=== Export Summary ==="
echo "Exported: $EXPORTED"
echo "Skipped: $SKIPPED"
echo "Failed: $FAILED"
if [ "$FAILED" -gt 0 ]; then
echo "::warning::$FAILED issue(s) failed to export"
fi