|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2024 MongoDB Inc |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +set -euo pipefail |
| 17 | +get_last_day_of_month() { |
| 18 | +last_day_of_month=0 |
| 19 | +case $1 in |
| 20 | +01|03|05|07|08|10|12) |
| 21 | +last_day_of_month=31 ;; |
| 22 | +04|06|09|11) |
| 23 | +last_day_of_month=30 ;; |
| 24 | +02) |
| 25 | +# February: check if it's a leap year |
| 26 | +if (( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ) )); then |
| 27 | +last_day_of_month=29 |
| 28 | +else |
| 29 | +last_day_of_month=28 |
| 30 | +fi |
| 31 | +;; |
| 32 | +esac |
| 33 | +echo $last_day_of_month |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +add_end_support_date() { |
| 38 | +new_json_list=$(echo "$1" | jq -c '.[]' | while IFS= read -r obj; do |
| 39 | +input_date=$(echo "$obj" | jq -r '.published_at') |
| 40 | +
|
| 41 | +# Extract the year, month, day, hour, minute, and second from the input date |
| 42 | +year="${input_date:0:4}" |
| 43 | +month="${input_date:5:2}" |
| 44 | +hour="${input_date:11:2}" |
| 45 | +minute="${input_date:14:2}" |
| 46 | +second="${input_date:17:2}" |
| 47 | +last_day_of_month=$(get_last_day_of_month "$month") |
| 48 | +new_year=$((year + 2)) |
| 49 | +
|
| 50 | +new_date="${new_year}-${month}-${last_day_of_month}T${hour}:${minute}:${second}Z" |
| 51 | +
|
| 52 | +echo "$obj" | jq --arg new_date "${new_date}" '.end_support_date = $new_date' |
| 53 | +done | jq -s '.') |
| 54 | + |
| 55 | +echo "$new_json_list" |
| 56 | +} |
| 57 | + |
| 58 | +page=1 |
| 59 | +api_version="2022-11-28" |
| 60 | + |
| 61 | +while true; do |
| 62 | + response=$(curl -s \ |
| 63 | + --request GET \ |
| 64 | + --url "https://api.github.com/repos/hashicorp/terraform/releases?per_page=100&page=$page" \ |
| 65 | + --header "Authorization: Bearer $GITHUB_TOKEN" \ |
| 66 | + --header "X-GitHub-Api-Version: $api_version") |
| 67 | + if [[ "$(printf '%s\n' "$response" | jq -e 'length == 0')" == "true" ]]; then |
| 68 | + break |
| 69 | + else |
| 70 | +versions=$(echo "$response" | jq -r '.[] | {version: .tag_name, published_at: .published_at}') |
| 71 | +filtered_versions_json=$(printf '%s\n' "${versions}" | jq -s '.') |
| 72 | +updated_date_versions=$(add_end_support_date "$filtered_versions_json") |
| 73 | +filtered_out_deprecated_versions=$(echo "$updated_date_versions" | jq 'map(select((.version | test("alpha|beta|rc"; "i") | not) and ((.end_support_date | fromdate) >= now))) | map(select(.version | endswith(".0")))') |
| 74 | + |
| 75 | +if [ -z ${json_array+x} ]; then |
| 76 | +# If json_array is empty, assign filtered_versions directly |
| 77 | +json_array=$(jq -n --argjson filtered_versions "${filtered_out_deprecated_versions}" '$filtered_versions') |
| 78 | +else |
| 79 | +# If json_array is not empty, append filtered_versions to it |
| 80 | +json_array=$(echo "$json_array" | jq --argjson filtered_versions "${filtered_out_deprecated_versions}" '. + $filtered_versions') |
| 81 | +fi |
| 82 | + |
| 83 | + ((page++)) |
| 84 | + fi |
| 85 | +done |
| 86 | + |
| 87 | +versions_array=$(printf '%s\n' "${json_array}" | jq -r '.[] | .version') |
| 88 | + |
| 89 | +formatted_output=$(echo "$versions_array" | awk 'BEGIN { ORS = "" } {gsub(/^v/,"",$1); gsub(/\.0$/,".x",$1); printf("%s\"%s\"", (NR==1?"":", "), $1)}' | sed 's/,"/," /g') |
| 90 | + |
| 91 | +echo "[$formatted_output]" | jq -c . |
0 commit comments