-
Notifications
You must be signed in to change notification settings - Fork 7
129 lines (110 loc) · 5.23 KB
/
Copy pathcreate-issues.yml
File metadata and controls
129 lines (110 loc) · 5.23 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
name: Create Issue from Commit
on:
push:
branches:
- main
jobs:
create_issues:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Process commits safely
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BEFORE_SHA="${{ github.event.before }}"
CURRENT_SHA="${{ github.event.after }}"
# Check if BEFORE_SHA exists in the repository
if ! git rev-parse --verify "$BEFORE_SHA" > /dev/null 2>&1; then
echo "Warning: BEFORE_SHA ($BEFORE_SHA) not found in repository. This can happen with force push after squash/rebasing."
echo "Processing only the current commit $CURRENT_SHA"
# Get information about the current commit only
COMMIT_MSG=$(git log --format="%s" -n 1 $CURRENT_SHA)
COMMIT_AUTHOR=$(git log --format="%an" -n 1 $CURRENT_SHA)
# Create array with just the current commit
COMMITS="$CURRENT_SHA|$COMMIT_MSG|$COMMIT_AUTHOR"
else
echo "Processing commits from $BEFORE_SHA to $CURRENT_SHA"
# Check if there are commits in the range
if ! git log --oneline $BEFORE_SHA..$CURRENT_SHA --quiet > /dev/null 2>&1; then
echo "No commits found in range $BEFORE_SHA..$CURRENT_SHA"
echo "This can happen with force push after squash/rebasing."
echo "Skipping issue creation."
exit 0
fi
COMMITS=$(git log --pretty=format:"%H|%s|%an" $BEFORE_SHA..$CURRENT_SHA)
fi
# Exit if no commits to process
if [ -z "$COMMITS" ]; then
echo "No commits to process"
exit 0
fi
echo "Found commits to process:"
echo "$COMMITS"
while IFS='|' read -r COMMIT_ID COMMIT_MSG COMMIT_AUTHOR; do
echo "Processing commit: $COMMIT_ID by $COMMIT_AUTHOR"
# Additional safety check: verify commit exists
if ! git rev-parse --verify "$COMMIT_ID" > /dev/null 2>&1; then
echo "Warning: Commit $COMMIT_ID not found, skipping..."
continue
fi
# Convert commit message to uppercase for case-insensitive matching
upper_msg=$(echo "$COMMIT_MSG" | tr '[:lower:]' '[:upper:]')
case "$upper_msg" in
*"FIX:"*|*"FIXED:"*)
type="Bug"
;;
*"FEATURE:"*|*"FEAT:"*)
type="Feature"
;;
*"ENHANCEMENT:"*|*"ENHANCE:"*)
type="Enhancement"
;;
*"DOC:"*|*"DOCUMENTATION:"*)
type="Documentation"
;;
*)
type="None"
;;
esac
if [ "$type" != "None" ]; then
echo "Creating $type issue for commit: $COMMIT_ID"
# Remove commit type prefix from title for cleaner issue titles
TITLE=$(echo "$COMMIT_MSG" | sed -E 's/^(FIX|FIXED|FEATURE|FEAT|ENHANCEMENT|ENHANCE|DOC|DOCUMENTATION):\s*//i')
BODY="**Commit:** https://github.com/$GITHUB_REPOSITORY/commit/$COMMIT_ID\n**Author:** $COMMIT_AUTHOR\n\nThis issue was automatically created from commit $COMMIT_ID"
JSON="{\"title\":\"$TITLE\",\"body\":\"$BODY\",\"labels\":[\"$type\",\"Auto\"]}"
# Create GitHub issue via API
RESPONSE=$(curl -s -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/json" \
https://api.github.com/repos/${GITHUB_REPOSITORY}/issues \
-d "$JSON")
# Extract issue number from API response
ISSUE_NUMBER=$(echo "$RESPONSE" | grep -o '"number":[[:space:]]*[0-9]*' | grep -o '[0-9]*')
if [ -z "$ISSUE_NUMBER" ]; then
ISSUE_NUMBER=$(echo "$RESPONSE" | sed -n 's/.*"number":\([0-9]*\).*/\1/p')
fi
# If issue was created successfully, close it after a delay
if [ -n "$ISSUE_NUMBER" ] && [ "$ISSUE_NUMBER" != "null" ]; then
echo "Waiting 5 seconds before closing issue..."
sleep 5
# Close the issue
curl -s -X PATCH \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "Content-Type: application/json" \
https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/$ISSUE_NUMBER \
-d '{"state":"closed"}'
echo "Created and closed issue #$ISSUE_NUMBER for commit $COMMIT_ID"
fi
else
echo "Skipping commit $COMMIT_ID - no matching type"
fi
done <<< "$COMMITS"