-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_pull_requests_since_last_version
executable file
·110 lines (92 loc) · 4.07 KB
/
get_pull_requests_since_last_version
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
#!/bin/bash
github_repo_url="$1" # Replace this with repo url
specific_tag="$2" # Replace this with the tag name you want to use
# Extract owner and repo from a GitHub URL
get_repo_and_owner() {
# Extract repository owner and name from GitHub URL
repo_owner=$(echo "$github_repo_url" | awk -F'/' '{print $4}')
repository_directory=$(echo "$github_repo_url" | awk -F'/' '{print $5}')
echo "Repo owner: $repo_owner; Repo name: $repository_directory"
}
# Extract pull request numbers from commit messages
get_pull_request_numbers() {
echo "$1" | grep -o "\#\d\+" | grep -o "\d\+"
}
# TODO optionally replace with GH CLI
# Check if a pull request is closed and merged
is_pull_request_closed_and_merged() {
pr_number=$1
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${repo_owner}/$repository_directory/pulls/$pr_number/merge
}
# Get commit messages for a merged pull request
get_pull_request_commits() {
pr_number=$1
commits=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${repo_owner}/$repository_directory/pulls/${pr_number}/commits" | jq -r '.[].commit.message')
echo "$commits"
}
# TODO refactor these two get_pull_request into one API call because this is wasteful
# Function to get the GitHub user responsible for a pull request using GitHub API
get_pull_request_user() {
pr_number=$1
user=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${repo_owner}/$repository_directory/pulls/${pr_number}" | jq -r '.user.login')
echo "$user"
}
# Function to get the GitHub pull request title using GitHub API
get_pull_request_title() {
pr_number=$1
title=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${repo_owner}/$repository_directory/pulls/${pr_number}" | jq -r '.title')
echo "$title"
}
get_repo_and_owner "$github_repo_url"
export repo_owner repository_directory
rm -rf "$repository_directory"
git clone "$github_repo_url"
cd "$repository_directory"
# Fetch specific tag from remote repository
git pull
git fetch origin "refs/tags/$specific_tag:refs/tags/$specific_tag"
# Get commits since the specific tag
git log --oneline "$specific_tag"..HEAD > commits.tmp
echo "" > clean.tmp
# Iterate through the commits and print information about closed and merged pull requests
CUR_BULLET=1
while IFS="" read -r commit; do
commit_message=$(echo "$commit" | cut -d' ' -f2-)
pull_request_numbers=$(get_pull_request_numbers "$commit_message")
has_merged_pull_requests=false
# Check each pull request and print information about merged ones
for pr_number in $pull_request_numbers; do
if is_pull_request_closed_and_merged "$pr_number"; then
user=$(get_pull_request_user "$pr_number")
if [[ -n "$user" ]]; then
user_profile_url="https://github.com/$user"
pr_title=$(get_pull_request_title "$pr_number")
echo "* $CUR_BULLET $pr_title ([#${pr_number}]($github_repo_url/pull/${pr_number}), [@${user}](${user_profile_url}))"
echo "* $pr_title ([#${pr_number}]($github_repo_url/pull/${pr_number}), [@${user}](${user_profile_url}))" >> clean.tmp
echo " Associated commit messages:"
pull_request_commits=$(get_pull_request_commits "$pr_number")
# This pipe chain deletes any commit messages that are blank, repeats, or contain "Signed-off"
echo "$pull_request_commits" | uniq | sed -e '/Signed-off.*/d' -e '/^$/d' -e 's/^/ - /'
has_merged_pull_requests=true
((CUR_BULLET++))
fi
fi
done
# Print an empty line between commits with merged pull requests
if $has_merged_pull_requests; then
echo
fi
done <<< "$(cat commits.tmp)"
cat clean.tmp