spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / .github / workflows / log-issue-events.yml
176 lines · 6.7 KBRaw
1name: Log GitHub Issue Events
2
3on:
4 issues:
5 types: [opened, closed]
6
7jobs:
8 log-issue-created:
9 if: github.event.action == 'opened'
10 runs-on: ubuntu-latest
11 timeout-minutes: 5
12 permissions:
13 contents: read
14 issues: read
15
16 steps:
17 - name: Log issue creation to Statsig
18 env:
19 STATSIG_API_KEY: ${{ secrets.STATSIG_API_KEY }}
20 run: |
21 ISSUE_NUMBER=${{ github.event.issue.number }}
22 REPO=${{ github.repository }}
23 ISSUE_TITLE=$(echo '${{ github.event.issue.title }}' | sed "s/'/'\\\\''/g")
24 AUTHOR="${{ github.event.issue.user.login }}"
25 CREATED_AT="${{ github.event.issue.created_at }}"
26
27 if [ -z "$STATSIG_API_KEY" ]; then
28 echo "STATSIG_API_KEY not found, skipping Statsig logging"
29 exit 0
30 fi
31
32 # Prepare the event payload
33 EVENT_PAYLOAD=$(jq -n \
34 --arg issue_number "$ISSUE_NUMBER" \
35 --arg repo "$REPO" \
36 --arg title "$ISSUE_TITLE" \
37 --arg author "$AUTHOR" \
38 --arg created_at "$CREATED_AT" \
39 '{
40 events: [{
41 eventName: "github_issue_created",
42 value: 1,
43 metadata: {
44 repository: $repo,
45 issue_number: ($issue_number | tonumber),
46 issue_title: $title,
47 issue_author: $author,
48 created_at: $created_at
49 },
50 time: (now | floor | tostring)
51 }]
52 }')
53
54 # Send to Statsig API
55 echo "Logging issue creation to Statsig for issue #${ISSUE_NUMBER}"
56
57 RESPONSE=$(curl -s -w "\n%{http_code}" -X POST https://events.statsigapi.net/v1/log_event \
58 -H "Content-Type: application/json" \
59 -H "STATSIG-API-KEY: ${STATSIG_API_KEY}" \
60 -d "$EVENT_PAYLOAD")
61
62 HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
63 BODY=$(echo "$RESPONSE" | head -n-1)
64
65 if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 202 ]; then
66 echo "Successfully logged issue creation for issue #${ISSUE_NUMBER}"
67 else
68 echo "Failed to log issue creation for issue #${ISSUE_NUMBER}. HTTP ${HTTP_CODE}: ${BODY}"
69 fi
70
71 log-issue-closed:
72 if: github.event.action == 'closed'
73 runs-on: ubuntu-latest
74 timeout-minutes: 5
75 permissions:
76 contents: read
77 issues: read
78
79 steps:
80 - name: Log issue closure to Statsig
81 env:
82 STATSIG_API_KEY: ${{ secrets.STATSIG_API_KEY }}
83 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
84 run: |
85 ISSUE_NUMBER=${{ github.event.issue.number }}
86 REPO=${{ github.repository }}
87 ISSUE_TITLE=$(echo '${{ github.event.issue.title }}' | sed "s/'/'\\\\''/g")
88 CLOSED_BY="${{ github.event.issue.closed_by.login }}"
89 CLOSED_AT="${{ github.event.issue.closed_at }}"
90 STATE_REASON="${{ github.event.issue.state_reason }}"
91
92 if [ -z "$STATSIG_API_KEY" ]; then
93 echo "STATSIG_API_KEY not found, skipping Statsig logging"
94 exit 0
95 fi
96
97 # Get additional issue data via GitHub API
98 echo "Fetching additional issue data for #${ISSUE_NUMBER}"
99 ISSUE_DATA=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
100 -H "Accept: application/vnd.github.v3+json" \
101 "https://api.github.com/repos/${REPO}/issues/${ISSUE_NUMBER}")
102
103 COMMENTS_COUNT=$(echo "$ISSUE_DATA" | jq -r '.comments')
104
105 # Get reactions data
106 REACTIONS_DATA=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
107 -H "Accept: application/vnd.github.v3+json" \
108 "https://api.github.com/repos/${REPO}/issues/${ISSUE_NUMBER}/reactions")
109
110 REACTIONS_COUNT=$(echo "$REACTIONS_DATA" | jq '. | length')
111
112 # Check if issue was closed automatically (by checking if closed_by is a bot)
113 CLOSED_AUTOMATICALLY="false"
114 if [[ "$CLOSED_BY" == *"[bot]"* ]]; then
115 CLOSED_AUTOMATICALLY="true"
116 fi
117
118 # Check if closed as duplicate by state_reason
119 CLOSED_AS_DUPLICATE="false"
120 if [ "$STATE_REASON" = "duplicate" ]; then
121 CLOSED_AS_DUPLICATE="true"
122 fi
123
124 # Prepare the event payload
125 EVENT_PAYLOAD=$(jq -n \
126 --arg issue_number "$ISSUE_NUMBER" \
127 --arg repo "$REPO" \
128 --arg title "$ISSUE_TITLE" \
129 --arg closed_by "$CLOSED_BY" \
130 --arg closed_at "$CLOSED_AT" \
131 --arg state_reason "$STATE_REASON" \
132 --arg comments_count "$COMMENTS_COUNT" \
133 --arg reactions_count "$REACTIONS_COUNT" \
134 --arg closed_automatically "$CLOSED_AUTOMATICALLY" \
135 --arg closed_as_duplicate "$CLOSED_AS_DUPLICATE" \
136 '{
137 events: [{
138 eventName: "github_issue_closed",
139 value: 1,
140 metadata: {
141 repository: $repo,
142 issue_number: ($issue_number | tonumber),
143 issue_title: $title,
144 closed_by: $closed_by,
145 closed_at: $closed_at,
146 state_reason: $state_reason,
147 comments_count: ($comments_count | tonumber),
148 reactions_count: ($reactions_count | tonumber),
149 closed_automatically: ($closed_automatically | test("true")),
150 closed_as_duplicate: ($closed_as_duplicate | test("true"))
151 },
152 time: (now | floor | tostring)
153 }]
154 }')
155
156 # Send to Statsig API
157 echo "Logging issue closure to Statsig for issue #${ISSUE_NUMBER}"
158
159 RESPONSE=$(curl -s -w "\n%{http_code}" -X POST https://events.statsigapi.net/v1/log_event \
160 -H "Content-Type: application/json" \
161 -H "STATSIG-API-KEY: ${STATSIG_API_KEY}" \
162 -d "$EVENT_PAYLOAD")
163
164 HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
165 BODY=$(echo "$RESPONSE" | head -n-1)
166
167 if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 202 ]; then
168 echo "Successfully logged issue closure for issue #${ISSUE_NUMBER}"
169 echo "Closed by: $CLOSED_BY"
170 echo "Comments: $COMMENTS_COUNT"
171 echo "Reactions: $REACTIONS_COUNT"
172 echo "Closed automatically: $CLOSED_AUTOMATICALLY"
173 echo "Closed as duplicate: $CLOSED_AS_DUPLICATE"
174 else
175 echo "Failed to log issue closure for issue #${ISSUE_NUMBER}. HTTP ${HTTP_CODE}: ${BODY}"
176 fi