-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·184 lines (155 loc) · 5.21 KB
/
main.sh
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
#!/bin/bash
# Check if the required environment variables are set
if [ -z "$GITHUB_USER" ] || [ -z "$GITHUB_TOKEN" ]; then
echo "Error: GITHUB_USER or GITHUB_TOKEN environment variables are not set."
exit 1
fi
# Today's date in ISO 8601 format
TODAY=$(date -u +"%Y-%m-%dT00:00:00Z")
# Function to check if a closed PR was merged
check_merged_status() {
pr_url="$1"
# Fetch the PR details
pr_details=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "$pr_url")
merged_status=$(echo "$pr_details" | jq -r '.merged // empty')
if [ "$merged_status" == "true" ]; then
echo "Merged"
else
echo "Closed"
fi
}
# Function to check the review status of a PR
check_review_status() {
pr_url="$1"
# Fetch the PR reviews
reviews=$(curl -s -H "Authorization: token $GITHUB_TOKEN" "$pr_url/reviews")
review_status=$(echo "$reviews" | jq -r '.[].state // empty' | sort -u)
if [ -z "$review_status" ]; then
echo "Review Required"
else
echo "$review_status"
fi
}
# Function to fetch today's pull requests
fetch_todays_prs() {
page=1
pr_count=0 # Start counting from 0
echo "Pull Requests"
echo
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/search/issues?q=type:pr+author:$GITHUB_USER+created:>$TODAY&per_page=100&page=$page")
# Check if the response contains items
items_count=$(echo "$response" | jq '.total_count // 0')
if [ "$items_count" -eq 0 ]; then
break
fi
# Check if the response is valid
if echo "$response" | jq -e . >/dev/null 2>&1; then
# Iterate over each PR item
while read -r pr; do
title=$(echo "$pr" | jq -r '.title')
state=$(echo "$pr" | jq -r '.state')
created_at=$(echo "$pr" | jq -r '.created_at')
url=$(echo "$pr" | jq -r '.html_url')
pr_url=$(echo "$pr" | jq -r '.pull_request.url // empty')
repo_name=$(echo "$url" | awk -F '/' '{print $(NF-3) "/" $(NF-2)}')
draft=$(echo "$pr" | jq -r '.draft')
# Check merged status if the PR is closed
if [ "$state" == "closed" ]; then
merged_status=$(check_merged_status "$pr_url")
status_text="($merged_status)"
else
if [ "$draft" == "true" ]; then
status_text="(Draft)"
else
review_status=$(check_review_status "$pr_url")
status_text="($review_status)"
fi
fi
echo "$((pr_count + 1)). Title: $title"
echo " a. Repository: $repo_name"
echo " b. Status: $state $status_text"
echo " c. Created At: $created_at"
echo " d. URL: $url"
echo
# Increment PR count
pr_count=$((pr_count + 1))
done < <(echo "$response" | jq -c '.items[]')
else
echo "Error: Unable to parse response."
echo "$response"
exit 1
fi
page=$((page + 1))
done
if [ "$pr_count" -eq 0 ]; then
echo "No pull requests found for today."
fi
}
# Function to fetch PRs reviewed by the user
fetch_reviewed_prs() {
page=1
review_count=0 # Start counting from 0
echo "Reviewed Pull Requests"
echo
while :; do
response=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/search/issues?q=type:pr+reviewed-by:$GITHUB_USER+created:>$TODAY&per_page=100&page=$page")
# Check if the response contains items
items_count=$(echo "$response" | jq '.total_count // 0')
if [ "$items_count" -eq 0 ]; then
break
fi
# Check if the response is valid
if echo "$response" | jq -e . >/dev/null 2>&1; then
# Iterate over each PR item
while read -r pr; do
title=$(echo "$pr" | jq -r '.title')
state=$(echo "$pr" | jq -r '.state')
created_at=$(echo "$pr" | jq -r '.created_at')
url=$(echo "$pr" | jq -r '.html_url')
pr_url=$(echo "$pr" | jq -r '.pull_request.url // empty')
repo_name=$(echo "$url" | awk -F '/' '{print $(NF-3) "/" $(NF-2)}')
draft=$(echo "$pr" | jq -r '.draft')
# Check merged status if the PR is closed
if [ "$state" == "closed" ]; then
merged_status=$(check_merged_status "$pr_url")
status_text="($merged_status)"
else
if [ "$draft" == "true" ]; then
status_text="(Draft)"
else
review_status=$(check_review_status "$pr_url")
status_text="($review_status)"
fi
fi
echo "$((review_count + 1)). Title: $title"
echo " a. Repository: $repo_name"
echo " b. Status: $state $status_text"
echo " c. Created At: $created_at"
echo " d. URL: $url"
echo
# Increment review count
review_count=$((review_count + 1))
done < <(echo "$response" | jq -c '.items[]')
else
echo "Error: Unable to parse response."
echo "$response"
exit 1
fi
page=$((page + 1))
done
if [ "$review_count" -eq 0 ]; then
echo "No reviewed pull requests found for today."
fi
}
# Check if jq is installed
if ! command -v jq &>/dev/null; then
echo "This script requires jq. Please install it."
exit 1
fi
# Fetch and display today's PRs
fetch_todays_prs
# Fetch and display reviewed PRs
fetch_reviewed_prs