-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplace_gitlab_values.sh
More file actions
executable file
·194 lines (148 loc) · 5.74 KB
/
Copy pathreplace_gitlab_values.sh
File metadata and controls
executable file
·194 lines (148 loc) · 5.74 KB
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
#!/bin/bash
set -e
# ==============================================================================
# Install dependencies
# ==============================================================================
if ! command -v git &> /dev/null; then
echo "Installing git..."
apk add --no-cache git
fi
# ==============================================================================
# Global variables
# ==============================================================================
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
CI_FILE=""
# ==============================================================================
# Utility functions
# ==============================================================================
log_section() {
echo ""
echo "=== $1 ==="
}
log_error() {
echo "ERROR: $1"
}
# ==============================================================================
# Core functions
# ==============================================================================
extract_application_id() {
log_section "Extracting Application ID"
local nrn=$(echo "$NP_ACTION_CONTEXT" | jq -r '.notification.nrn')
echo "NRN: $nrn"
if [ -z "$nrn" ] || [ "$nrn" == "null" ]; then
log_error "No NRN found in context"
exit 1
fi
APPLICATION_ID=$(echo "$nrn" | sed -n 's/.*application=\([0-9]*\).*/\1/p')
echo "APPLICATION_ID: $APPLICATION_ID"
if [ -z "$APPLICATION_ID" ]; then
log_error "Could not extract application_id from NRN"
exit 1
fi
}
get_metadata_application() {
log_section "Fetching Application Metadata"
local metadata_json=$(np metadata read --id "$APPLICATION_ID" --entity application --format json)
if [ -z "$metadata_json" ]; then
log_error "Could not fetch application metadata"
exit 1
fi
echo "Metadata JSON:"
# Extract metadata fields (from metadata_application_playground object)
AUTOR_PROYECTO=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Autor del Proyecto"] // empty')
DESCRIPCION_MICROSERVICIO=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Descripción del Microservicio"] // empty')
GRUPO=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Grupo"] // empty')
IDENTIFICADOR_BACKEND=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Identificador Backend"] // empty')
NOMBRE_AGILE_TEAM=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Nombre del Agile Team"] // empty')
RUTA_MICROSERVICIO=$(echo "$metadata_json" | jq -r '.metadata_application_playground["Ruta del Microservicio"] // empty')
# Echo each field
log_section "Metadata Fields"
echo "Autor del Proyecto: $AUTOR_PROYECTO"
echo "Descripción del Microservicio: $DESCRIPCION_MICROSERVICIO"
echo "Grupo: $GRUPO"
echo "Identificador Backend: $IDENTIFICADOR_BACKEND"
echo "Nombre del Agile Team: $NOMBRE_AGILE_TEAM"
echo "Ruta del Microservicio: $RUTA_MICROSERVICIO"
}
fetch_repository_url() {
log_section "Fetching Repository URL"
local app_data=$(np application read --id "$APPLICATION_ID" --format json)
if [ -z "$app_data" ]; then
log_error "Could not fetch application data"
exit 1
fi
REPO_URL=$(echo "$app_data" | jq -r '.repository_url // .repo_url // .repository // empty')
echo "REPO_URL: $REPO_URL"
if [ -z "$REPO_URL" ]; then
log_error "No repository URL found in application data"
exit 1
fi
}
clone_repository() {
log_section "Cloning Repository"
cd "$SCRIPT_DIR" && cd ..
local repo_name=$(basename "$REPO_URL" .git)
if [ -d "$repo_name" ]; then
echo "Repository folder '$repo_name' already exists, skipping clone"
cd "$repo_name"
return 0
fi
echo "Cloning in: $(pwd)"
if [ -n "$GITLAB_TOKEN_ENTITY" ]; then
echo "Using GITLAB_TOKEN_ENTITY for authentication"
local auth_url=$(echo "$REPO_URL" | sed "s|https://gitlab-ee.agil.movistar.com.ar|https://git:${GITLAB_TOKEN_ENTITY}@gitlab-ee.agil.movistar.com.ar|")
git clone "$auth_url"
else
echo "WARNING: No GITLAB_TOKEN_ENTITY found, attempting clone without authentication"
git clone "$REPO_URL"
fi
cd "$repo_name"
echo "Repository cloned successfully"
}
# Example to look for a file and do some changes
find_ci_file() {
log_section "Finding CI File"
CI_FILE=".github/workflows/ci.yml"
if [ ! -f "$CI_FILE" ]; then
log_error "No CI file found at .github/workflows/ci.yml"
exit 1
fi
echo "CI_FILE: $CI_FILE"
}
modify_ci_file() {
log_section "Modifying CI File"
echo "# Modified by entity-hooks" >> "$CI_FILE"
}
commit_and_push() {
log_section "Committing and Pushing Changes"
git config user.email "movistar@nullplatform.io"
git config user.name "Movistar"
git add .github/workflows/ci.yml
if git diff --cached --quiet; then
echo "No changes to commit"
return 0
fi
git commit -m "chore: update CI workflow via entity hooks"
git push origin HEAD
echo "Changes pushed successfully!"
}
# ==============================================================================
# Main
# ==============================================================================
main() {
echo "=========================================="
echo "=== Starting replace_gitlab_values.sh ==="
echo "=========================================="
extract_application_id
get_metadata_application
fetch_repository_url
clone_repository
# find_ci_file
# modify_ci_file
# commit_and_push
echo ""
echo "=========================================="
echo "=== Script completed successfully ==="
echo "=========================================="
}
main