|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2023 SUSE LLC |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +set -e |
| 8 | +# |
| 9 | +# For all packages prepared by build-packages-for-obs.sh in |
| 10 | +# $WORKSPACE/SRPMS/<package> prepare and submitt changed packages |
| 11 | +# to OBS. |
| 12 | +# |
| 13 | +# Use $OSCRC ot pass an osc configfile containing required credentials |
| 14 | +# (otherwise ~/.oscrc) |
| 15 | +# |
| 16 | +# srpm_package_defs() has a hardcoded list of packages excluded by default. |
| 17 | +# |
| 18 | +WORKSPACE=${WORKSPACE:-/tmp/push-packages-to-obs} |
| 19 | +PACKAGE="$@" |
| 20 | + |
| 21 | +GIT_ORG=${GIT_ORG:-galaxy} |
| 22 | +GIT_SRV=${GIT_SRV:-src.suse.de} |
| 23 | +GIT_USR=${GIT_USR:-gitea} |
| 24 | + |
| 25 | +FAKE_COMITTOBS=${FAKE_COMITTOBS:+1} |
| 26 | + |
| 27 | +# Set KEEP_SRPMS environment variable to TRUE if you want to keep your SRPMS |
| 28 | +# Useful if, for example, you are resubmitting the same set to several |
| 29 | +# projects in row |
| 30 | +KEEP_SRPMS=${KEEP_SRPMS:-FALSE} |
| 31 | + |
| 32 | +# BRANCH in upstream git and in internal gitea should be the same in the standard case |
| 33 | +GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 34 | +BRANCH=${BRANCH:-$GIT_BRANCH} |
| 35 | + |
| 36 | +GIT_CURR_HEAD=$(git rev-parse --short HEAD) |
| 37 | + |
| 38 | +## used for exec push.sh |
| 39 | +OSCAPI=${OSCAPI:-https://api.suse.de} |
| 40 | +GIT_DIR=$(git rev-parse --show-cdup) |
| 41 | +test -z "$GIT_DIR" || cd "$GIT_DIR" |
| 42 | +GIT_DIR=$(pwd) |
| 43 | +## |
| 44 | + |
| 45 | +grep -v -- "\(--help\|-h\|-?\)\>" <<<"$@" || { |
| 46 | + cat <<EOF |
| 47 | +Usage: push-packages-to-obs.sh [PACKAGE].. |
| 48 | +Submit changed packages from \$WORKSPACE/SRPMS/<package> ($WORKSPACE) |
| 49 | +to OBS ($GIT_ORG). Without argument all packages in SRPMS are processed. |
| 50 | +If OBS_TEST_PROJECT environment variable has been set, packages will be |
| 51 | +submitted to it, instead. This is useful for, for example, building a project |
| 52 | +that contains packages that have been changed in a Pull Request. |
| 53 | +EOF |
| 54 | + exit 0 |
| 55 | +} |
| 56 | + |
| 57 | +function srpm_package_defs() { |
| 58 | + # - "PKG_NAME" from $SRPM_DIR, using a hardcoded blacklist |
| 59 | + # of packages we do not submitt. |
| 60 | + # - define $PACKAGE to build a specific set of packages. |
| 61 | + # - usage: |
| 62 | + # while read PKG_NAME; do |
| 63 | + # ... |
| 64 | + # done < <(srpm_package_defs) |
| 65 | + # |
| 66 | + PACKAGE=$(find "$SRPM_DIR" -mindepth 1 -maxdepth 1 -type d -printf "%P\n") |
| 67 | + for N in $PACKAGE; do |
| 68 | + test -d "$SRPM_DIR/$N" || { |
| 69 | + echo "No package dir '$SRPM_DIR/$N'" >&2 |
| 70 | + exit 99 |
| 71 | + } |
| 72 | + echo "$N" |
| 73 | + done |
| 74 | +} |
| 75 | + |
| 76 | +# Here we have eveyfile (incl. .changes) in git, thus inside the tarball. |
| 77 | +# The tarballs rootdirs may differ, as they contain the revision number. |
| 78 | +# The specfile also contains the revision number. So do a quick check |
| 79 | +# for different .changes, then 'tardiff -p1' |
| 80 | +function copy_changed_package() |
| 81 | +{ |
| 82 | + local sdir="$1" |
| 83 | + test -d "$sdir" || { echo "No source dir '$sdir'" >&2; return 2; } |
| 84 | + local tdir="$2" |
| 85 | + test -d "$tdir" || { echo "No target dir '$tdir'" >&2; return 2; } |
| 86 | + local pkgname="$3" |
| 87 | + test -n "$pkgname" || { echo "No package name" >&2; return 2; } |
| 88 | + |
| 89 | + |
| 90 | + rm -rf "$tdir"/* |
| 91 | + |
| 92 | + for F in "$sdir"/*; do |
| 93 | + local stem="$(basename "$F")" |
| 94 | + case "$stem" in |
| 95 | + *.obscpio) |
| 96 | + if [[ "$stem" =~ ^${pkgname}.*$ ]]; then |
| 97 | + pushd $tdir |
| 98 | + mkdir ${pkgname} |
| 99 | + cpio -id < "$WORKSPACE/$F" |
| 100 | + popd |
| 101 | + else |
| 102 | + # extract vendor.tar.gz in vendor/ and node_module.tar.gz in node_modules/ |
| 103 | + sdirname=${stem/.*/} |
| 104 | + pushd $tdir |
| 105 | + mkdir ${sdirname} |
| 106 | + cpio -id < "$WORKSPACE/$F" |
| 107 | + popd |
| 108 | + fi |
| 109 | + ;; |
| 110 | + *.tar.*|*.tar|*.tgz|*.tbz2) |
| 111 | + if [[ "$stem" =~ ^${pkgname}.*$ ]]; then |
| 112 | + pushd $tdir |
| 113 | + mkdir ${pkgname} |
| 114 | + tar -x --strip-components=1 -f $WORKSPACE/$F -C ${pkgname} |
| 115 | + popd |
| 116 | + else |
| 117 | + # extract vendor.tar.gz in vendor/ and node_module.tar.gz in node_modules/ |
| 118 | + sdirname=${stem/.*/} |
| 119 | + pushd $tdir |
| 120 | + mkdir ${sdirname} |
| 121 | + tar -x --strip-components=1 -f $WORKSPACE/$F -C ${sdirname} |
| 122 | + popd |
| 123 | + fi |
| 124 | + ;; |
| 125 | + *.obsinfo) continue;; |
| 126 | + *) |
| 127 | + cp $F $tdir |
| 128 | + ;; |
| 129 | + esac |
| 130 | + done |
| 131 | + |
| 132 | + pushd $tdir |
| 133 | + diffs=$(git status --short | wc -l) |
| 134 | + test $diffs -gt 0 && { |
| 135 | + popd |
| 136 | + return 0 |
| 137 | + } || { |
| 138 | + git reset --hard |
| 139 | + popd |
| 140 | + return 1 |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +# unused: not sure if we want to create automatically or let this |
| 145 | +# be a manual process |
| 146 | +function git_create() { |
| 147 | + OTOKEN="...." |
| 148 | + |
| 149 | + #curl -X POST -H "Authorization: token $OTOKEN" \ |
| 150 | + # -H 'content-type: application/json' \ |
| 151 | + # --data "{\"name\":\"${PKG_NAME//+/_}\", \"object_format_name\":\"sha256\"}}" \ |
| 152 | + # "https://$GIT_SRV/api/v1/orgs/$GIT_ORG/repos" |
| 153 | + curl -X POST -H "Authorization: token $OTOKEN" \ |
| 154 | + -H 'content-type: application/json' \ |
| 155 | + --data "{\"name\":\"${PKG_NAME//+/_}\", \"object_format_name\":\"sha256\"}}" \ |
| 156 | + "https://$GIT_SRV/api/v1/user/repos" |
| 157 | + git clone $GIT_USR@$GIT_SRV:$GIT_ORG/$PKG_NAME |
| 158 | + pushd $PKG_NAME |
| 159 | + git checkout -b $BRANCH |
| 160 | + popd |
| 161 | +} |
| 162 | + |
| 163 | +function git_checkout_or_create() { |
| 164 | + pushd $GIT_ORG |
| 165 | + git clone $GIT_USR@$GIT_SRV:$GIT_ORG/$PKG_NAME || { popd; exit 1; } #git_create |
| 166 | + pushd $PKG_NAME |
| 167 | + git checkout $BRANCH || git checkout -b $BRANCH || { popd; popd; exit 1; } |
| 168 | + popd |
| 169 | + popd |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +function log_and_add_failure() { |
| 174 | + test $# -ge 1 || { echo "log_and_add_failure: Wrong args $#: $@" >&2; return 1; } |
| 175 | + local pkg_name="$1" |
| 176 | + local opt_msg="$2" |
| 177 | + FAILED_CNT=$(($FAILED_CNT+1)) |
| 178 | + FAILED_PKG="$FAILED_PKG$(echo -ne "\n $pkg_name${opt_msg:+ ($opt_msg)}")" |
| 179 | + echo "*** FAILED${opt_msg:+ ($opt_msg)} [$pkg_name]" |
| 180 | +} |
| 181 | + |
| 182 | +# go.. |
| 183 | +cd "$WORKSPACE" |
| 184 | +T_LOG="$WORKSPACE/tmplog" |
| 185 | +trap "test -f \"$T_LOG\" && /bin/rm -rf -- \"$T_LOG\" " 0 1 2 3 13 15 |
| 186 | + |
| 187 | +SRPM_DIR="SRPMS" |
| 188 | +test -d "$SRPM_DIR" || { |
| 189 | + echo "No'$SRPM_DIR' dir to process." >&2 |
| 190 | + exit 99 |
| 191 | +} |
| 192 | +rm -rf "$GIT_ORG" |
| 193 | +mkdir $GIT_ORG |
| 194 | + |
| 195 | +echo "Going to update $GIT_ORG from $SRPM_DIR..." |
| 196 | +UNCHANGED_CNT=0 |
| 197 | +SUCCEED_CNT=0 |
| 198 | +SUCCEED_PKG= |
| 199 | +FAILED_CNT=0 |
| 200 | +FAILED_PKG= |
| 201 | + |
| 202 | +while read PKG_NAME; do |
| 203 | + echo "=== Processing package [$PKG_NAME]" |
| 204 | + |
| 205 | + # prepare the srpm dir |
| 206 | + SRPM_PKG_DIR="$SRPM_DIR/$PKG_NAME" |
| 207 | + test -d "$SRPM_PKG_DIR" || { |
| 208 | + log_and_add_failure "$PKG_NAME" "no srpm dir" |
| 209 | + continue |
| 210 | + } |
| 211 | + |
| 212 | + # Is there is a push.sh script call it and remove it right after |
| 213 | + if [ -f "${SRPM_PKG_DIR}/push.sh" ]; then |
| 214 | + bash "${SRPM_PKG_DIR}/push.sh" ${OSCAPI} ${GIT_DIR} ${PKG_NAME} |
| 215 | + rm "${SRPM_PKG_DIR}/push.sh" |
| 216 | + fi |
| 217 | + |
| 218 | + GIT_PKG_DIR="$GIT_ORG/$PKG_NAME" |
| 219 | + rm -rf "$GIT_PKG_DIR" |
| 220 | + git_checkout_or_create |
| 221 | + |
| 222 | + test -z "$FAKE_COMITTOBS" || { |
| 223 | + echo "FAKE: Not comitting to OBS..." |
| 224 | + continue |
| 225 | + } |
| 226 | + |
| 227 | + if copy_changed_package "$SRPM_PKG_DIR" "$GIT_PKG_DIR" "${PKG_NAME}"; then |
| 228 | + echo "Package has changed, updating..." |
| 229 | + ( |
| 230 | + set -e |
| 231 | + cd "$GIT_PKG_DIR" |
| 232 | + git add -A . # $OSC addremove >/dev/null |
| 233 | + git status # $OSC status |
| 234 | + if [ -z "$FAKE_COMITTOBS" ]; then |
| 235 | + git commit -m "Git submit $GIT_BRANCH($GIT_CURR_HEAD)" # $OSC ci -m "Git submit $GIT_BRANCH($GIT_CURR_HEAD)" |
| 236 | + git push origin HEAD |
| 237 | + else |
| 238 | + echo "FAKE: Not comitting to OBS..." |
| 239 | + false |
| 240 | + fi |
| 241 | + ) || { |
| 242 | + log_and_add_failure "$PKG_NAME" "${FAKE_COMITTOBS:+fake }checkin" |
| 243 | + continue |
| 244 | + } |
| 245 | + SUCCEED_CNT=$(($SUCCEED_CNT+1)) |
| 246 | + SUCCEED_PKG="$SUCCEED_PKG$(echo -ne "\n $PKG_NAME")" |
| 247 | + else |
| 248 | + echo "Package is unchanged." |
| 249 | + UNCHANGED_CNT=$(($UNCHANGED_CNT+1)) |
| 250 | + fi |
| 251 | + if [ "${KEEP_SRPMS}" == "FALSE" ]; then |
| 252 | + rm -rf "$SRPM_PKG_DIR" |
| 253 | + fi |
| 254 | + rm -rf "$GIT_PKG_DIR" |
| 255 | +done < <(srpm_package_defs) |
| 256 | + |
| 257 | +echo "======================================================================" |
| 258 | +echo "Unchanged packages: $UNCHANGED_CNT" |
| 259 | +echo "Updated packages: $SUCCEED_CNT$SUCCEED_PKG" |
| 260 | +test $FAILED_CNT != 0 && { |
| 261 | + echo "Failed packages: $FAILED_CNT$FAILED_PKG" |
| 262 | +} |
| 263 | +echo "======================================================================" |
| 264 | + |
| 265 | +exit $FAILED_CNT |
0 commit comments