Skip to content

Commit 4a81fbd

Browse files
committed
daily merged prs
1 parent 23704a3 commit 4a81fbd

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Log number of daily merged PRs
2+
3+
on:
4+
schedule:
5+
# Run daily at midnight UTC to get the previous day's data
6+
- cron: '0 0 * * *'
7+
workflow_call:
8+
secrets:
9+
GOOGLE_APPLICATION_CREDENTIALS:
10+
required: true
11+
GOOGLE_SERVICE_ACCOUNT:
12+
required: true
13+
SPREADSHEET_ID:
14+
required: true
15+
SHEET_NAME:
16+
required: true
17+
GITHUB_TOKEN:
18+
required: true
19+
workflow_dispatch:
20+
21+
jobs:
22+
log-daily-pr-merges:
23+
name: Log number of daily merged PRs
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Download oauth2l
27+
run: |
28+
curl --silent https://storage.googleapis.com/oauth2l/1.3.2/linux_amd64.tgz | tar xz
29+
echo "$PWD/linux_amd64" >> "$GITHUB_PATH"
30+
31+
- name: Create service_account.json
32+
env:
33+
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
34+
GOOGLE_SERVICE_ACCOUNT: ${{ secrets.GOOGLE_SERVICE_ACCOUNT }}
35+
run: |
36+
echo "$GOOGLE_SERVICE_ACCOUNT" > "$GOOGLE_APPLICATION_CREDENTIALS"
37+
38+
- name: Get yesterday's date
39+
id: yesterday
40+
run: |
41+
# Get yesterday's date in YYYY-MM-DD format
42+
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
43+
echo "yesterday=$YESTERDAY" >> $GITHUB_OUTPUT
44+
echo "Yesterday's date: $YESTERDAY"
45+
46+
- name: Count PRs merged yesterday
47+
id: count_prs
48+
env:
49+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
50+
run: |
51+
# Get the repository name from GITHUB_REPOSITORY
52+
REPO_NAME=$(echo "$GITHUB_REPOSITORY" | cut -d'/' -f2)
53+
echo "Repository: $REPO_NAME"
54+
55+
# Count PRs merged yesterday into main branch
56+
# Using GitHub API to search for PRs merged on yesterday's date
57+
YESTERDAY="${{ steps.yesterday.outputs.yesterday }}"
58+
59+
# Search for PRs merged yesterday
60+
PR_COUNT=$(curl --silent \
61+
--header "Authorization: token $GITHUB_TOKEN" \
62+
--header "Accept: application/vnd.github.v3+json" \
63+
"https://api.github.com/search/issues?q=repo:$GITHUB_REPOSITORY+is:pr+is:merged+merged:$YESTERDAY..$YESTERDAY+base:main" \
64+
| jq '.total_count')
65+
66+
echo "PRs merged yesterday: $PR_COUNT"
67+
echo "pr_count=$PR_COUNT" >> $GITHUB_OUTPUT
68+
69+
- name: Write data to google sheets
70+
env:
71+
GOOGLE_APPLICATION_CREDENTIALS: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
72+
SPREADSHEET_ID: ${{ secrets.SPREADSHEET_ID }}
73+
SHEET_NAME: ${{ secrets.SHEET_NAME }}
74+
run: |
75+
YESTERDAY="${{ steps.yesterday.outputs.yesterday }}"
76+
PR_COUNT="${{ steps.count_prs.outputs.pr_count }}"
77+
78+
# Get OAuth token
79+
token=$(oauth2l fetch --scope https://www.googleapis.com/auth/spreadsheets)
80+
81+
# Read existing data to check if yesterday's row exists
82+
spreadsheet_data=$(curl --silent --header "Authorization: Bearer $token" \
83+
https://sheets.googleapis.com/v4/spreadsheets/"$SPREADSHEET_ID"/values/"$SHEET_NAME"!A:C)
84+
85+
# Check if yesterday's date exists in column A
86+
current_date_index=$(echo "$spreadsheet_data" | jq --arg yesterday "$YESTERDAY" \
87+
'(.values | map(.[0])) | (index($yesterday) | if . == null then null else . + 1 end)')
88+
89+
if [ "$current_date_index" == "null" ]; then
90+
# Date doesn't exist, append new row with PR count in column C
91+
curl --silent --header "Authorization: Bearer $token" \
92+
--header "Content-Type: application/json" \
93+
--request POST \
94+
--data "{\"values\":[[\"$YESTERDAY\", \"\", $PR_COUNT]]}" \
95+
https://sheets.googleapis.com/v4/spreadsheets/"$SPREADSHEET_ID"/values/"$SHEET_NAME"!A:C:append?valueInputOption=USER_ENTERED
96+
echo "Added new row for $YESTERDAY with $PR_COUNT PRs merged"
97+
else
98+
# Date exists, update column C with PR count
99+
curl --silent --header "Authorization: Bearer $token" \
100+
--header "Content-Type: application/json" \
101+
--request PUT \
102+
--data "{\"values\":[[$PR_COUNT]]}" \
103+
https://sheets.googleapis.com/v4/spreadsheets/"$SPREADSHEET_ID"/values/"$SHEET_NAME"!C"$current_date_index":C"$current_date_index"?valueInputOption=USER_ENTERED
104+
echo "Updated row $current_date_index for $YESTERDAY with $PR_COUNT PRs merged"
105+
fi

0 commit comments

Comments
 (0)