-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub_repo_version_scan.sh
109 lines (92 loc) · 3.68 KB
/
github_repo_version_scan.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
#!/bin/bash
set -e
SCRIPT_NAME=$(basename $0)
pre_config(){
apt update && wait && apt install unzip zip wget logrotate
}
parse_json(){
echo "${1//\"/}" | tr -d '\n' | tr -d '\r' | sed "s/.*$2:\([^,}]*\).*/\1/"
}
get_repo_version(){
local REMOTE_REPO_VERSION=""
local LATEST_RELEASE_INFO=$(curl --silent https://api.github.com/repos/$1/releases/latest)
if ! echo "$LATEST_RELEASE_INFO" | grep -q "Not Found"; then
REMOTE_REPO_VERSION=$(parse_json "$LATEST_RELEASE_INFO" "tag_name")
fi
echo $REMOTE_REPO_VERSION | tr -d '\r\n'
}
check_need_build(){
remote_version=$2
this_version=$1
need_build="no"
if [[ ${this_version:0:1} != "v" ]]; then
need_build="yes"
echo $need_build
return
fi
remote_version_no_v=${remote_version:1}
this_version_no_v=${this_version:1}
sorted_versions=$(echo -e "$remote_version_no_v\n$this_version_no_v" | sort -V)
small_version=$(echo "$sorted_versions" | head -n1)
if [ "$remote_version_no_v" != "$small_version" ]; then
need_build="yes"
break
fi
echo $need_build
}
get_repo_latest_upload_url(){
local LATEST_UPLOAD_URL=""
local LATEST_RELEASE_INFO=$(curl --silent https://api.github.com/repos/$1/releases/latest)
if ! echo "$LATEST_RELEASE_INFO" | grep -q "Not Found"; then
LATEST_UPLOAD_URL=$(parse_json "$LATEST_RELEASE_INFO" "upload_url"),label}
fi
echo $LATEST_UPLOAD_URL
}
check_file_exist_from_repo_latest(){
local response=$(curl --silent https://api.github.com/repos/$1/releases/latest)
local assetNames=$(echo "$response" | grep -oP '"name": "\K[^"]+')
assetExists="no"
for name in $assetNames; do
if [[ "$name" == "$2" ]]; then
assetExists="yes"
break
fi
done
echo $assetExists
}
handle_input(){
if [[ $1 == "--check_need_update" ]]; then
local current_repo_rul=$2
local remote_repo_url=$3
if [[ $current_repo_rul == "" ]] || [[ $current_repo_rul == " " ]] || [[ $remote_repo_url == "" ]] || [[ $remote_repo_url == " " ]]; then
echo "{\"code\":-1,\"msg\":\"Input Prarms Error:Prarms Cannot Be Empty\"}"
return
fi
if [[ $(echo "$current_repo_rul" | cut -d'/' -f1) != "github.com" ]] || [[ $(echo "$remote_repo_url" | cut -d'/' -f1) != "github.com" ]]; then
echo "{\"code\":-1,\"msg\":\"This Script Only github.com is supported\"}"
return
fi
local current_repo_suffix=$(echo "$current_repo_rul" | cut -d'/' -f2-)
local remote_repo_suffix=$(echo "$remote_repo_url" | cut -d'/' -f2-)
local current_repo_latest_version=$(get_repo_version "$current_repo_suffix")
if [[ $current_repo_latest_version == "" ]] || [[ $current_repo_latest_version == " " ]]; then
local isTrue="yes"
echo $isTrue
return
fi
local remote_repo_latest_version=$(get_repo_version "$remote_repo_suffix")
check_need_build "$current_repo_latest_version" "$remote_repo_latest_version"
elif [[ $1 == "--get_latest_version" ]]; then
local latest_version=$(get_repo_version "$(echo "$2" | cut -d'/' -f2-)")
echo $latest_version | tr -d '\r\n'
elif [[ $1 == "--get_latest_upload_url" ]]; then
local latest_upload_url=$(get_repo_latest_upload_url "$(echo "$2" | cut -d'/' -f2-)")
echo $latest_upload_url | tr -d '\r\n'
elif [[ $1 == "--check_file_exist_from_repo_latest" ]]; then
local is_exist=$(check_file_exist_from_repo_latest "$(echo "$2" | cut -d'/' -f2-)" "$3")
echo $is_exist | tr -d '\r\n'
else
echo "{\"code\":-1,\"msg\":\"Methods not yet supported\"}"
fi
}
handle_input "$@"