-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmolior-project-manager
executable file
·352 lines (305 loc) · 7.32 KB
/
molior-project-manager
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/sh
if [ `id -u` -eq 0 ]; then
echo Error: do not run as root >&2
exit 1
fi
set -e
. /usr/lib/molior-tools/molior.sh.inc
PROGRAMM_NAME="$0"
aquire_user
usage()
{
ERROR="$1"
echo "Usage: $PROGRAMM_NAME [OPTIONS] PROJECT VERSION OPERATION"
echo "Options:"
echo " -m MOLIOR_URL Alternative molior url Default in ~/.moliorrc()"
echo " -x PATTERN Exclude pattern (one ore multiple)"
echo " -a Add a new version instead of overwriteing the old one"
echo
echo "Operations:"
echo " * get clone or pull git repositories"
echo " * create create new projectversion in all repositories"
echo " -v NEWVERSION new version to create"
echo " * push push changes to git server"
echo " -v NEWVERSION new version branch to push"
echo " * run COMMAND run any command in the git repositories"
echo
echo
echo "Examples:"
echo " molior-project-manager myproject 0.1 get"
echo " molior-project-manager myproject 0.1 create -v release1.0"
echo " molior-project-manager myproject 0.1 push -v release1.0"
echo " molior-project-manager myproject 0.1 release -v release1.0"
if [ -n "$ERROR" ]; then
echo
echo "$ERROR"
fi
exit 1
}
# save all the arguments for the later guidance
ALL_ARGS="$@"
APPEND_VERSION=0
while getopts "m:v:x:a" opt
do
case $opt in
m)
MOLIOR_URL="$OPTARG"
shift 2
;;
x)
EXCLUDE="$OPTARG|$EXCLUDE"
shift 2
;;
a)
APPEND_VERSION=1
shift 1
;;
\?)
usage "Invalid option $opt"
;;
:)
usage "Option -$OPTARG requires an argument."
;;
esac
done
PROJECT=$1
PROJECT_VERSION=$2
OPERATION=$3
if [ -z "$PROJECT" ]
then
usage "You have to specify a project!"
fi
if [ -z "$PROJECT_VERSION" ]
then
usage "You have to specify a project version"
fi
if [ -z "$OPERATION" ]
then
usage "You have to specify a operation"
fi
shift 3
# execute a callback on each repository
for_repo()
{
CALLBACK=$1
shift
for REPO in $REPOSITORIES
do
REPO_URL=`echo $REPO | cut -d# -f1`
GIT_REF=`echo $REPO | cut -d# -f2`
if [ -z "$GIT_REF" ]; then
GIT_REF=master
fi
REPO_DIRECTORY=$(echo "$REPO_URL" | cut -d/ -f5 | rev | cut -d. -f2- | rev)
echo
echo === $REPO_DIRECTORY ===
$CALLBACK $@ || true
done
}
_get()
{
if [ -d "$REPO_DIRECTORY" ]
then
#cd "$REPO_DIRECTORY"
#git checkout $GIT_REF
#git pull --rebase --all
#cd - >/dev/null
echo "Already cloned"
else
REPO_NAME=`basename $REPO_URL | sed s/\.git$//`
mkdir $REPO_NAME
cd $REPO_NAME
git init .
git remote add origin $REPO_URL
git fetch --depth 1 origin $GIT_REF
if echo $GIT_REF | grep -q ^v; then
git checkout FETCH_HEAD
else
git checkout $GIT_REF
fi
cd - >/dev/null
fi
}
_create_version()
{
if [ ! -d "$REPO_DIRECTORY" ]; then
echo "Error: repo $REPO_DIRECTORY not found"
return
fi
while getopts "v:" opt
do
case $opt in
v)
NEWVERSION="$OPTARG"
shift 2
;;
\?)
usage "Invalid option $opt"
;;
:)
usage "Option -$OPTARG requires an argument."
;;
esac
done
if [ -z "$NEWVERSION" ]; then
usage "Error: please specify NEWVERSION (-v)"
fi
if [ ! -f $REPO_DIRECTORY/debian/molior.yml ]; then
echo "Error: debian/molior.yml not found"
return
fi
cd "$REPO_DIRECTORY"
git checkout -b $NEWVERSION
if [ "$?" -ne 0 ]; then
cd - > /dev/null
return
fi
if grep -q target_repo_version debian/molior.yml; then
# legacy upgrade for old molior version pre 1.2.0~alpha
sed -i "s/target_repo_version:.*$/target_repo_version: '$NEWVERSION'/g" debian/molior.yml
else
if [ "$APPEND_VERSION" -ne 1 ]; then
for entry in `molior-parseconfig target list`
do
target_project=`echo $entry | cut -d/ -f1`
target_version=`echo $entry | cut -d/ -f2`
echo "Removing target: $target_project/$target_version"
molior-parseconfig target remove $target_project $target_version
done
fi
# add the new project version
molior-parseconfig target add "$PROJECT" "$NEWVERSION"
# FIXME: version has to be a string
fi
# commit the changes
git add debian/molior.yml
git commit -sm "release for $PROJECT version $NEWVERSION"
cd - > /dev/null
}
_push()
{
if [ ! -d "$REPO_DIRECTORY" ]; then
echo "Error: repo $REPO_DIRECTORY not found"
return
fi
while getopts "v:" opt
do
case $opt in
v)
NEWVERSION="$OPTARG"
shift 2
;;
\?)
usage "Invalid option $opt"
;;
:)
usage "Option -$OPTARG requires an argument."
;;
esac
done
if [ -z "$NEWVERSION" ]; then
usage "Error: please specify NEWVERSION (-v)"
fi
cd "$REPO_DIRECTORY"
git push --set-upstream origin $NEWVERSION
cd - > /dev/null
}
_release()
{
if [ ! -d "$REPO_DIRECTORY" ]; then
echo "Error: repo $REPO_DIRECTORY not found"
return
fi
cd "$REPO_DIRECTORY"
create-release -np --skip-edit
cd - > /dev/null
}
_run()
{
if [ ! -d "$REPO_DIRECTORY" ]; then
echo "Error: repo $REPO_DIRECTORY not found"
return
fi
cd "$REPO_DIRECTORY"
$@
cd - > /dev/null
}
_cleanup()
{
REPO_URL="$1"
REPO_DIRECTORY=$(echo "$REPO_URL" | cut -d/ -f5 | rev | cut -d. -f2- | rev)
rm -rf "$REPO_DIRECTORY"
}
COOKIE_FILE="$(mktemp)"
molior_session_call()
{
URL_PATH="$1" && shift
curl $MOLIOR_URL$URL_PATH -s --retry 10 \
--cookie "$COOKIE_FILE" \
--cookie-jar "$COOKIE_FILE" $@
}
aquire_pass
# login to the molior session
molior_session_call "/plogin" \
--data-urlencode "username=$MOLIOR_USER" \
--data-urlencode "password=$MOLIOR_PASSWORD" \
--location > /dev/null
# get all the repository urls
PROJECT_ID=$( \
molior_session_call "/api/projects?show_all=true" | \
jq -r ".results[] | select(.name==\"${PROJECT}\").id" \
)
if [ -z "$PROJECT_VERSION" ]
then
PROJECT_VERSION=$( \
molior_session_call "/api/projects/$PROJECT_ID" | \
jq '.versions_map | to_entries[]' | \
jq -s -r 'sort_by(.key)[-1].value' \
)
fi
echo "Getting repositories from version: $PROJECT_VERSION"
PROJECT_VERSION_ID=$( \
molior_session_call "/api/projects/$PROJECT_ID" | \
jq -r ".versions_map | to_entries[] | select(.value==\"${PROJECT_VERSION}\").key" \
)
REPOSITORIES=$( \
molior_session_call "/api/repositories?project_version_id=$PROJECT_VERSION_ID" | \
jq -r '.results[] | .url + "#" + .projectversion.last_gitref' \
)
REPOSITORIES=$( \
echo "$REPOSITORIES" | \
sed 's,\s,\n,g'\
)
if [ ! -z "$EXCLUDE" ]
then
REPOSITORIES=$(echo "$REPOSITORIES" | grep -Ev "$(echo "$EXCLUDE" | sed 's,.$,,')")
fi
case $OPERATION in
get)
for_repo _get $@
echo
echo "Repositories have been cloned or pulled (where available)"
echo "You may want to create a new project version:"
echo " $0 $PROJECT $PROJECT_VERSION create -v [NEWVERSION]"
;;
create)
for_repo _create_version $@
echo "New project version has been set (where applicable): $NEWVERSION"
echo "You may want to review the changes and push them:"
echo " $0 $PROJECT $PROJECT_VERSION push"
;;
push)
for_repo _push $@
;;
run)
for_repo _run $@
;;
*)
usage "Unknown operation: $OPERATION"
;;
esac
exit 0
if [ "$3" = "--cleanup" ]
then
for_repo _cleanup
fi