-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade-go-version.sh
202 lines (173 loc) · 7.33 KB
/
upgrade-go-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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/bin/bash
get_latest_go_version() {
latest_version=$(curl -sL https://golang.org/VERSION?m=text)
echo "$latest_version"
}
extract_version() {
# Extract the first line
first_line=$(echo "$1" | head -n 1)
# Remove the "go" prefix to get the version
version=$(echo "$first_line" | sed 's/go//')
# Drop the last value from the version using cut
version=$(echo "$version" | cut -d. -f1,2)
# Return the extracted version
echo "$version"
}
update_version() {
local filename="$1"
if [ "$filename" = ".drone.yml" ]; then
echo '************************Before change start************************'
# cat "$filename"
echo '************************Before change end************************'
# Search for the existing Go version and replace it with the new version
go_version=$(grep -E "image: golang:*" "$filename" | head -n 1 | awk -F ':' '{print $3}')
echo 'existing file go_version: '$go_version
# Replace the existing Go version with the new version
sed -i "s/golang:${go_version}/golang:${new_go_version}/g" "$filename"
# Commit the changes
echo '************************After change start************************'
#cat "$filename"
echo '************************After change end************************'
elif [ "$filename" = "go.mod" ]; then
# Your additional condition for ".anotherfile"
echo '************************Before change start************************'
#cat "$filename"
echo '************************Before change end************************'
# Search for the existing Go version and replace it with the new version
go_version=$(grep -E "^go [0-9]+\.[0-9]+" "$filename" | awk '{print $2}')
echo 'existing file go_version: '$go_version
# Replace the existing Go version with the new version
sed -i "s/go ${go_version}/go ${new_go_version}/g" "$filename"
# Commit the changes
echo '************************After change start************************'
# cat "$filename"
echo '************************After change end************************'
# Add your logic for ".anotherfile" here
else
echo "Not a valid file. Skipping modification."
fi
}
get_repo_info() {
local repo_url="$1"
# Remove '.git' from the end of the URL
repo_url="${repo_url%.git}"
# Split the URL by "/"
IFS="/" read -ra url_parts <<< "$repo_url"
# Get the length of the array
length=${#url_parts[@]}
# Extract repository name and owner
export repo_name="${url_parts[length-1]}"
export repo_owner="${url_parts[length-2]}"
echo "Repository Name: $repo_name"
echo "Repository Owner: $repo_owner"
}
clone_repo(){
local repo="$1"
local feature_branch="$2"
# Clone the repository
echo "***************clone repo repo: $repo \n\n"
repo_name=$(basename "$repo" .git)
echo 'Repo : '$repo
get_repo_info $repo
echo "***************in call Repository Name: $repo_name"
echo "**************in call Repository Owner: $repo_owner"
git config --global user.email "[email protected]"
git config --global user.name "rahkumar56"
git remote set-url origin https://rahkumar56:[email protected]/$repo_owner/$repo_name
pwd
# git clone "$repo"
git clone "https://rahkumar56:${pat_token}@github.com/${repo_owner}/${repo_name}.git"
cd "$repo_name" || exit
#Git Config
git config --global user.email "[email protected]"
git config --global user.name "rahkumar56"
git remote set-url origin https://rahkumar56:[email protected]/$repo_owner/$repo_name
pwd
ls -la
export base_ranch=$(git rev-parse --abbrev-ref HEAD)
# Print the branch name
echo "Current Git branch: $base_ranch"
git checkout -b $feature_branch
git push origin $feature_branch
git fetch origin
git pull origin
}
commit_generate_pr(){
echo $pat_token
echo $pat_token |base64
# Push the changes to a new branch
git status
git add .
git status
git commit -m "Updated ci-manager-config.yml with latest plugin versions"
git push origin $feature_branch
# Create a pull request
# NOTE: You'll need to integrate with a platform-specific API or use a tool like Hub for GitHub.
# Example for GitHub using Hub:
#hub pull-request -m "Update Go version to $new_go_version"
echo 'commit and push is success'
#Generate PR
echo 'Going to hit generate PR curl in side sh file.'
get_repo_info $repo
echo "***************in call Repository Name: $repo_name"
echo "**************in call Repository Owner: $repo_owner"
echo $repo_name
echo $repo_owner
echo $pat_token |base64
echo $pat_token
echo $feature_branch
echo $base_ranch
url='https://api.github.com/repos/'$repo_owner'/'$repo_name'/pulls'
echo $url
body='{ "title":"Updated go version to the latest version", "body":"Updated go version to the latest version", "head":"'$repo_owner':'$feature_branch'", "base":"'$base_ranch'" }'
echo $body
echo $pat_token
curl_response=$(curl --location "$url" \
--header 'Accept: application/vnd.github+json' \
--header "Authorization: Bearer $pat_token" \
--header 'Content-Type: application/json' \
--data "$body")
# Parse the latest version from the response using jq
pr_url=$(echo "$curl_response" | jq -r '.url')
echo 'pr_url::'$pr_url
echo 'curl_response :: '$curl_response
echo 'slack_webhook url::'$slack_webhook
response=$(curl --location --silent --output - "$slack_webhook" \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode "payload={\"channel\": \"ci-release-validation\", \"username\": \"ci-release-validation\", \"type\": \"mrkdwn\", \"text\": \"*Security Automation: PR is generated for plugins repos:* :white_check_mark: $pr_url\", \"icon_emoji\": \":harnesshd:\"}")
echo "Response: $response"
}
#repositories=(<+pipeline.variables.repoUrl>)
echo 'Inside pipeline file'
echo 'feature_branch: ' $feature_branch
echo 'inp_repositories : ' $inp_repositories
echo 'pat_token: ' $pat_token
#input repo urls
IFS=',' read -ra repositories <<< "$inp_repositories"
echo "repositories elements: ${repositories[@]}"
echo 'repositories :'$repositories
#repositories=("https://github.com/drone-plugins/drone-gcs.git")
latest_version=$(get_latest_go_version)
echo "Latest version value in checkfornew verson:$latest_version"
new_go_version=$(extract_version "$latest_version")
echo 'Latest new_go_version:'$new_go_version
#new_go_version='1.20'
echo 'Latest new_go_version:'$new_go_version
# Ensure that git and hub are installed
command -v git >/dev/null 2>&1 || { echo >&2 "Git is required but not installed. Aborting."; exit 1; }
command -v hub >/dev/null 2>&1 || { echo >&2 "Hub is required but not installed. Aborting."; exit 1; }
hub version
# Loop through the repositories
for repo in "${repositories[@]}"; do
# Clone the repository
echo "***************Started Execution for the repo: $repo_name \n\n"
repo_name=$(basename "$repo" .git)
echo 'Repo : '$repo
clone_repo $repo $feature_branch
#Update go version in files
update_version ".drone.yml"
update_version "go.mod"
commit_generate_pr
echo "***************Ended Execution for the repo: $repo_name \n\n"
cd ..
done