-
Notifications
You must be signed in to change notification settings - Fork 101
/
auto_generate_release_notes.sh
executable file
·118 lines (96 loc) · 2.92 KB
/
auto_generate_release_notes.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
#! /bin/sh
###### Script writer: Shivansh Saini (@shivanshs9) [26/10/2018] ######
## Initial Configurations
REPO_URI="sendgrid/python-http-client"
GITHUB_USERNAME="sendgrid"
CHANGELOG_FILE="CHANGELOG.md"
## Helper functions
get_line_no() {
release="$1"
grep -n -m 1 "$release" "$CHANGELOG_FILE" | cut -d ":" -f 1
}
get_current_tag() {
git describe --abbrev=0 --tags
}
get_latest_tag() {
curl -s $API_RELEASES_URL"/latest" | python3 -c \
"import sys, json; print(json.load(sys.stdin)['tag_name'])" 2>/dev/null
}
get_body() {
current_local_tag="$1"
last_deployed_tag="$2"
start=$(get_line_no "${current_local_tag:1}")
: $(( start++ ))
end=$(get_line_no "${last_deployed_tag:1}")
: $(( end-- ))
# Getting the updated content in CHANGELOG.md and making it JSON-safe
sed "${start},${end}p" "$CHANGELOG_FILE" -n | sed -z 's/\n/\\n/g'
}
print_help() {
echo -e "\nUsage:"
echo -e "\t$0 [options]"
echo -e "\nDescription:"
echo -e "\tA shell utility script to auto-generate release notes of configured repository based \
on the latest release tag and the current tag. Looks for changes in the configured changelog file."
echo -e "\nOptions:"
options="\t-h, --help|Show help.\n\
\t-u, --username <username>|Calls the Github API providing username as <username>.\n\
\t-r, --repository <repo_uri>|Configures the repository to use github <repo_uri>, which is in the format of <owner>/<repo>.\n\
\t-c, --changelog <file>|Configures to look for <file> for release notes. The release notes should start from\
the next line of the tag name."
echo -e $options | column -t -s '|'
}
## Main script
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-u|--username)
GITHUB_USERNAME="$2"
shift
shift
;;
-r|--repository)
REPO_URI="$2"
shift
shift
;;
-c|--changelog)
CHANGELOG_FILE="$2"
shift
shift
;;
-h|--help)
print_help
exit
;;
*)
echo "Invalid argument. Use $0 --help to get help."
exit 2
;;
esac
done
API_RELEASES_URL="https://api.github.com/repos/"${REPO_URI}"/releases"
if [[ ! -f "$CHANGELOG_FILE" ]]; then
echo "ERROR: \"$CHANGELOG_FILE\" file doesn't exist."
exit 1
fi
echo -n "Getting current release tag..."
current_local_tag=$(get_current_tag)
echo " $current_local_tag"
echo -n "Getting latest release tag..."
last_deployed_tag=$(get_latest_tag)
echo " $last_deployed_tag"
if [[ -z "$last_deployed_tag" ]]; then
echo "ERROR: Unable to find last release tag from \"$REPO_URI.\""
exit 1
fi
if [[ "$last_deployed_tag" = "$current_local_tag" ]]; then
echo "SKIPPING: Local tag is up-to-date with the latest release tag."
exit
fi
body=$(get_body "$current_local_tag" "$last_deployed_tag")
title="$current_local_tag"
echo "----Releasing \"$current_local_tag\" to \"$REPO_URI\"----"
data="{\"tag_name\": \"$current_local_tag\", \"name\": \"$title\", \"body\": \"$body\"}"
curl -s -X POST -H "Content-Type:application/json" -u "$GITHUB_USERNAME" \
"$API_RELEASES_URL" --data-binary @- <<<$data