OpenDev: Introducing OpenDev to Nordix
[infra/cicd.git] / jjb / opendev / push-upstream-jobs.sh
1 #!/bin/bash
2
3 # ============LICENSE_START=======================================================
4 #  Copyright (C) 2019 The Nordix Foundation. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # SPDX-License-Identifier: Apache-2.0
19 # ============LICENSE_END=========================================================
20
21 #
22 # This script will take the changes that are pushed to Nordix Gerrit and push upstream to OpenDev
23 # Pre-requisites for script to run successfully:
24 # - Author name in Nordix Gerrit have to be the same as OpenDev username
25 #   i.e. need to run "git config --global user.name <OpenDev username>"
26 # - infra public key on build server needs to be added to your users SSH Public Keys in OpenDev Gerrit
27 #
28 set -o nounset
29 set -o pipefail
30
31 cd $WORKSPACE
32 echo "Retrieving information from commit to push towards OpenDev"
33 project=$(git config --local remote.origin.url | awk -F "29418/opendev/" '{print $2}')
34 message=$(git show -s --pretty=%B | grep -vi 'Signed-off-by')
35 username=$(git show -s --pretty=%an)
36
37 pattern=" "
38 if [[ "$username" =~ $pattern ]]
39 then
40     echo "Incorrect username, use OpenDev username as git user.name when pushing to Nordix"
41     exit 1
42 fi
43
44 echo "Setting user name and email"
45 git config user.email $(git show -s --pretty=%ae)
46 git config user.name $username
47
48 echo "Checking out branch on master with new changes"
49 git reset HEAD~1 --soft
50 git checkout -b delivery_branch origin/$BRANCH
51
52 git config --get remote.upstream.url
53 retVal=$?
54
55 if [[ $retVal -eq 0 ]]
56 then
57   git remote rm upstream
58   git remote add upstream "ssh://$username@review.opendev.org:29418/$project.git"
59 else
60   git remote add upstream "ssh://$username@review.opendev.org:29418/$project.git"
61 fi
62
63 echo "Committing changes and pushing upstream"
64 git commit -as -m "$message"
65 git push upstream HEAD:refs/for/$BRANCH
66
67 retVal1=$?
68 if [[ $retVal1 -eq 0 ]]
69 then
70   echo "Push upstream to OpenDev succeeded"
71 else
72   echo "Push upstream to OpenDev failed"
73   exit 2
74 fi
75
76 git checkout $BRANCH
77 git branch -D delivery_branch