-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathget-version.sh
81 lines (64 loc) · 2.6 KB
/
get-version.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
#!/bin/bash
set -euxo pipefail # Ensures the script exits on errors and unset variables
# Function to parse JSON data using jq
parseData() {
local jsonData="$1"
if echo "$jsonData" | jq . >/dev/null 2>&1; then
echo "$jsonData" | jq -c '[.[] | select(.prerelease == false) | {published_at, tag_name}]'
else
echo "Error: Failed to parse JSON data." >&2
exit 1
fi
}
# Function to get paginated data
getPaginatedData() {
local url=$1
local token=$2
local data='[]'
while :; do
# Fetch data using curl
response=$(curl -s -w "%{http_code}" -H "Authorization: Bearer $token" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github.v3+json" "$url")
http_code="${response: -3}" # Extract HTTP status code
response_body="${response%???}" # Extract body
if [[ "$http_code" != "200" ]]; then
echo "Error: Failed to fetch data from GitHub API (HTTP status: $http_code)." >&2
echo "Response: $response_body" >&2
exit 1
fi
# Parse the JSON data
parsedData=$(parseData "$response_body")
# Append parsed data to the data array
data=$(echo "$data" "$parsedData" | jq -s 'add')
# Check the link header for pagination
linkHeader=$(curl -s -I -H "Authorization: Bearer $token" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github.v3+json" "$url" | grep -i 'link:')
if [[ $linkHeader == *'rel="next"'* ]]; then
# Extract the next URL from the link header using regex
nextUrl=$(echo "$linkHeader" | grep -o '<[^>]*>; rel="next"' | sed 's/<\(.*\)>; rel="next"/\1/')
url=$nextUrl
else
break
fi
done
# Output the collected data
echo "$data"
}
if [ "$#" -lt 1 ]; then
echo "Usage: $0 GITHUB_TOKEN"
exit 1
fi
# Initial URL
initialUrl="https://api.github.com/repos/cli/cli/releases?per_page=100"
token="$1"
current_version="$(grep -Eo "ENV GITHUB_CLI_VERSION .+" debian/Dockerfile | cut -d' ' -f3)"
# Get the paginated data
data=$(getPaginatedData "$initialUrl" "$token")
# Sort the data by the published_at attribute in descending order
sortedData=$(echo "$data" | jq 'sort_by(.published_at) | reverse')
# Determine the next version.
tags=$(echo "$sortedData" | jq -r '.[] | .tag_name' | tr '\n' ' ')
next_version="$(echo "$tags" | (grep -Eo "(.*?) v$current_version( |$)" || true) | awk '{print $(NF-1)}' | cut -c2-)"
echo "$next_version"