OPENDEV: TEST - Update rebase job to track and sync all upstream branches

Change-Id: I9c26c34d2375a6a0f0de392ef3d893cd42c4d8c3
diff --git a/jjb/opendev/global-templates-opendev-push-upstream-test.yaml b/jjb/opendev/global-templates-opendev-push-upstream-test.yaml
new file mode 100644
index 0000000..df2ccca
--- /dev/null
+++ b/jjb/opendev/global-templates-opendev-push-upstream-test.yaml
@@ -0,0 +1,42 @@
+- job-template:
+    name: 'opendev-{project-name}-push-upstream'
+
+    node: infra-build01-ubuntu1804
+
+    disabled: false
+
+    concurrent: true
+
+    properties:
+      - logrotate
+
+    parameters:
+      - string:
+          name: PROJECT
+          default: '{project}'
+          description: JJB configured PROJECT parameter to identify a Nordix Gerrit project
+      - nordix-gerrit-parameters
+
+    scm:
+      - git-scm-gerrit:
+          ssh-credentials-id: nordixinfra-nordix-gerrit-ssh
+          branch: $GERRIT_BRANCH
+          refspec: $GERRIT_REFSPEC
+
+    triggers:
+      - nordix-gerrit-patchset-created-or-push-upstream:
+          project: 'opendev/{project}'
+          branch: '**'
+          files: '**'
+
+    wrappers:
+      - build-timeout:
+          timeout: 10
+
+    builders:
+      - shell:
+          !include-raw-escape: ./push-upstream-jobs.sh
+
+    publishers:
+      - notify-slack:
+          slack-channel: '#infra'
diff --git a/jjb/opendev/global-templates-opendev-rebase-test.yaml b/jjb/opendev/global-templates-opendev-rebase-test.yaml
new file mode 100644
index 0000000..0112cd8
--- /dev/null
+++ b/jjb/opendev/global-templates-opendev-rebase-test.yaml
@@ -0,0 +1,47 @@
+- job-template:
+    name: 'opendev-{project-name}-rebase'
+
+    node: infra-build01-ubuntu1804
+
+    disabled: false
+
+    concurrent: false
+
+    properties:
+      - logrotate
+
+    parameters:
+      - opendev-gerrit-parameters
+      - string:
+          name: PROJECT
+          default: '{project}'
+          description: JJB configured PROJECT parameter to identify a Nordix Gerrit project
+      - string:
+          name: NORDIX_REMOTE
+          default: 'ssh://infra@gerrit.nordix.org:29418/opendev/{project}.git'
+          description: The remote to add for Nordix.
+
+    scm:
+      - git-scm:
+          ssh-credentials-id: nordixinfra-lfn-gerrit-ssh
+          branch: '*'
+          refspec: ''
+
+    triggers:
+      - opendev-gerrit-trigger-change-merged:
+          project: '{project}'
+          branch: '**'
+          files: '**'
+      - timed: "@midnight"
+
+    wrappers:
+      - build-timeout:
+          timeout: 10
+
+    builders:
+      - shell:
+          !include-raw-escape: ./rebase-jobs.sh
+
+    publishers:
+      - notify-slack:
+          slack-channel: '#infra'
diff --git a/jjb/opendev/opendev-sandbox/opendev-sandbox-push-upstream-jobs.yml b/jjb/opendev/opendev-sandbox/opendev-sandbox-push-upstream-jobs.yml
index 8862f06..88dc761 100644
--- a/jjb/opendev/opendev-sandbox/opendev-sandbox-push-upstream-jobs.yml
+++ b/jjb/opendev/opendev-sandbox/opendev-sandbox-push-upstream-jobs.yml
@@ -21,9 +21,6 @@
     name: opendev-sandbox-push-upstream
     project: opendev/sandbox
     project-name: opendev-sandbox
-    stream:
-      - master:
-          branch: '{stream}'
 
     jobs:
-      - 'opendev-{project-name}-{stream}-push-upstream'
+      - 'opendev-{project-name}-push-upstream'
diff --git a/jjb/opendev/opendev-sandbox/opendev-sandbox-rebase-jobs.yml b/jjb/opendev/opendev-sandbox/opendev-sandbox-rebase-jobs.yml
index 71f1fd6..81d1eb4 100644
--- a/jjb/opendev/opendev-sandbox/opendev-sandbox-rebase-jobs.yml
+++ b/jjb/opendev/opendev-sandbox/opendev-sandbox-rebase-jobs.yml
@@ -21,9 +21,6 @@
     name: opendev-sandbox-rebase
     project: opendev/sandbox
     project-name: opendev-sandbox
-    stream:
-      - master:
-          branch: '{stream}'
 
     jobs:
-      - 'opendev-{project-name}-{stream}-rebase'
+      - 'opendev-{project-name}-rebase'
diff --git a/jjb/opendev/rebase-jobs.sh b/jjb/opendev/rebase-jobs.sh
new file mode 100755
index 0000000..b223f90
--- /dev/null
+++ b/jjb/opendev/rebase-jobs.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+set -o errexit
+set -o nounset
+set -o pipefail
+
+cd $WORKSPACE
+# When the GERRIT_BRANCH is not set the rebase job wasn't triggered by merge
+# but it's executed as part of timer. In this case we will make sure that all
+# branches from remote are synced with Nordix Gerrit.
+if [[ -z "${GERRIT_BRANCH:-}" ]] ; then
+    echo "-- Fetch all branches from OpenDev for project: ${PROJECT} --"
+    REMOTE_BRANCHES=$(git branch -r | grep -v 'HEAD -> ' | awk '/origin/ {gsub("origin/","");print}')
+    for branch in ${REMOTE_BRANCHES}  ; do
+        git branch -f --track ${branch} origin/${branch}
+    done
+# The job was triggered by merge on OpenDev. We will make sure that branch set
+# in GERRIT_BRANCH is in sync with Nordix Gerrit.
+else
+    echo "-- Fetch branch: ${GERRIT_BRANCH} from OpenDev for project: ${PROJECT} --"
+    REMOTE_BRANCHES=${GERRIT_BRANCH}
+    git branch -f --track ${GERRIT_BRANCH} origin/${GERRIT_BRANCH}
+fi
+git fetch origin -v
+echo "-- Adding new remote $NORDIX_REMOTE for sync --"
+git remote add nordix $NORDIX_REMOTE
+git remote -v
+echo "-- Merging changes from OpenDev remote branches to local branch: --"
+for branch in ${REMOTE_BRANCHES}  ; do
+    echo "------ Branch name: ${branch}"
+    git checkout ${branch} -q
+    git merge origin/${branch} --ff-only --stat
+done
+echo "-- Pushing changes to Nordix Gerrit --"
+git push nordix --all -v
+echo "--------------------------------------------------------------------"
+# Make sure the Nordix is set from Jenkins Job configuration
+# parameter: NORDIX_REMOTE in next job run
+if git remote | grep nordix > /dev/null ; then
+    git remote rm nordix
+fi