Skip to content

Commit

Permalink
feat: installs glab and adds functions to manage gitlab repos
Browse files Browse the repository at this point in the history
This PR adds the installation of the glab tool and functions to handle gitlab repos.
Relates to hacbs-release/app-interface-deployments#30
  • Loading branch information
theflockers committed Jul 17, 2023
1 parent 7c19cfb commit 4d9f4a6
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ ARG COSIGN_VERSION=1.13.1
ARG KUBECTL_VERSION=1.27.2
ARG OCP_VERSION=4.13.3
ARG YQ_VERSION=4.34.1
ARG GLAB_VERSION=1.31.0

RUN curl -L https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_amd64 -o /usr/bin/yq &&\
curl -L https://dl.k8s.io/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl -o /usr/bin/kubectl &&\
curl -L https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/${OCP_VERSION}/opm-linux.tar.gz |tar -C /usr/bin -xzf - &&\
chmod +x /usr/bin/{yq,kubectl,opm} &&\
curl -L https://gitlab.com/gitlab-org/cli/-/releases/v${GLAB_VERSION}/downloads/glab_${GLAB_VERSION}_Linux_x86_64.tar.gz | tar -C /usr -xzf - bin/glab &&\
chmod +x /usr/bin/{yq,kubectl,opm,glab} &&\
rpm -ivh https://github.com/sigstore/cosign/releases/download/v${COSIGN_VERSION}/cosign-${COSIGN_VERSION}.x86_64.rpm

RUN dnf -y --setopt=tsflags=nodocs install \
Expand All @@ -19,6 +21,9 @@ RUN dnf -y --setopt=tsflags=nodocs install \
skopeo \
&& dnf clean all

# Adds Red Hat IT Root CAs
ADD https://certs.corp.redhat.com/certs/2015-IT-Root-CA.pem https://certs.corp.redhat.com/certs/2022-IT-Root-CA.pem /etc/pki/ca-trust/source/anchors/
RUN update-ca-trust

COPY pyxis /home/pyxis
COPY utils /home/utils
Expand Down
82 changes: 82 additions & 0 deletions utils/git-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env sh
#
# Functions to manage git repositories

git_functions_usage() {
echo -e "Usage:"
echo -e "\t~$ export GIT_AUTHOR_NAME=<name>"
echo -e "\t~$ export GIT_AUTHOR_EMAIL=<email>"
echo -e "\t~$ export ACCESS_TOKEN=<github key>"
echo -e "\t~$ . git-functions"
}

git_config_ident() {
git config user.name "${GIT_AUTHOR_NAME}"
git config user.email "${GIT_AUTHOR_EMAIL}"
}

git_commit_and_push() {
OPTIONS=$(getopt -l "branch:,message:" -o "b:m:" -a -- "$@")
eval set -- "$OPTIONS"
while true; do
case "$1" in
-b|--branch)
shift
BRANCH=$1
;;
-m|--message)
shift
COMMIT_MSG="$1"
;;
--)
shift
break
;;
esac
shift
done
git checkout -b $BRANCH
git_config_ident
git commit -a -m "$COMMIT_MSG"
git push origin $BRANCH
}

git_clone_and_checkout() {
OPTIONS=$(getopt -l "repository:,revision:" -o "r:v:" -a -- "$@")
eval set -- "$OPTIONS"
while true; do
case "$1" in
-r|--repository)
shift
REPOSITORY=$1
;;
-v|--revision)
shift
REVISION="$1"
;;
--)
shift
break
;;
esac
shift
done

echo "Cloning the Repository..."

REPO_DIR=$(basename ${REPOSITORY} .git)

# rewrites the repo url with the auth token
CLONE_URL="https://oauth2:${ACCESS_TOKEN}@${REPO_URL#*://}"
git clone $CLONE_URL
cd $REPO_DIR
git checkout $REVISION
}

git_functions_init() {
# an actual user should be set
[ -z "${GIT_AUTHOR_NAME}" -o -z "${GIT_AUTHOR_EMAIL}" ] && git_functions_usage && return 1

# it should not continue if ACCESS_TOKEN
[ -z "${ACCESS_TOKEN}" ] && git_functions_usage && return 1
}
52 changes: 52 additions & 0 deletions utils/gitlab-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env sh
#
# Functions to manage gitlab repos.
#
# This library requires the `glab` cli (https://docs.gitlab.com/ee/integration/glab/)

gitlab_usage() {
echo -e "Usage:"
echo -e "\t~$ export GITLAB_HOST=<host>"
echo -e "\t~$ export ACCESS_TOKEN=<github key>"
echo -e "\t~$ . gitlab-functions"
}

gitlab_auth() {
gitlab_host=$1
glab auth login -h $gitlab_host --stdin <<< "${ACCESS_TOKEN}"
}

gitlab_create_mr() {
OPTIONS=$(getopt -l "head:,title:,description:" -o "h:t:d:" -a -- "$@")
eval set -- "$OPTIONS"
while true; do
case "$1" in
-h|--head)
shift
HEAD="$1";
;;
-t|--title)
shift
TITLE="$1";
;;
-d|--description)
shift
DESCRIPTION="$1";
;;
--)
shift
break
;;
esac
shift
done
glab mr create --title "${TITLE}" -b ${HEAD} --description "${DESCRIPTION}" | \
awk '/merge_request/ {print "merge_request: "$1}' |yq -o json
}

gitlab_init() {
# it should not continue if ACCESS_TOKEN or GITLAB_HOST is missing
[ -z "${ACCESS_TOKEN}" -o -z "${GITLAB_HOST}" ] && gitlab_usage && return 1

gitlab_auth ${GITLAB_HOST}
}

0 comments on commit 4d9f4a6

Please sign in to comment.