Skip to content

Commit

Permalink
Add file upload with git
Browse files Browse the repository at this point in the history
  • Loading branch information
jorge-romero committed May 28, 2024
1 parent 0349f04 commit b3decea
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/quickstarter/quickstarter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func executeStepUpload(t *testing.T, step TestStep, testdataPath string, tmplDat
if len(step.UploadParams.Repository) > 0 {
targetRepository = renderTemplate(t, step.UploadParams.Repository, tmplData)
}
stdout, stderr, err := utils.RunScriptFromBaseDir("tests/scripts/upload-file-to-bitbucket.sh", []string{
stdout, stderr, err := utils.RunScriptFromBaseDir("tests/scripts/upload-file-to-bitbucket-with-git.sh", []string{
fmt.Sprintf("--bitbucket=%s", config["BITBUCKET_URL"]),
fmt.Sprintf("--user=%s", config["CD_USER_ID"]),
fmt.Sprintf("--password=%s", cdUserPassword),
Expand All @@ -331,7 +331,7 @@ func executeStepUpload(t *testing.T, step TestStep, testdataPath string, tmplDat
fmt.Printf("%s", stdout)
if err != nil {
t.Fatalf(
"Execution of `upload-file-to-bitbucket.sh` failed: \nStdOut: %s\nStdErr: %s\nErr: %s\n",
"Execution of `upload-file-to-bitbucket-with-git.sh` failed: \nStdOut: %s\nStdErr: %s\nErr: %s\n",
stdout,
stderr,
err)
Expand Down
128 changes: 128 additions & 0 deletions tests/scripts/upload-file-to-bitbucket-with-git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env bash
set -eu
set -o pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

echo_done(){
echo -e "\033[92mDONE\033[39m: $1"
}

echo_warn(){
echo -e "\033[93mWARN\033[39m: $1"
}

echo_error(){
echo -e "\033[31mERROR\033[39m: $1"
}

echo_info(){
echo -e "\033[94mINFO\033[39m: $1"
}

BITBUCKET_URL=""
BITBUCKET_USER=""
BITBUCKET_PWD=""
BITBUCKET_PROJECT="unitt"
REPOSITORY=
BRANCH=master
FILE=
REPO_FILE=


function usage {
printf "Upload file to bitbucket.\n\n"
printf "This script will ask interactively for parameters by default.\n"
printf "However, you can also pass them directly. Usage:\n\n"
printf "\t-h|--help\t\tPrint usage\n"
printf "\t-v|--verbose\t\tEnable verbose mode\n"
printf "\t-i|--insecure\t\tAllow insecure server connections when using SSL\n"
printf "\n"
printf "\t-b|--bitbucket\t\tBitbucket URL, e.g. 'https://bitbucket.example.com'\n"
printf "\t-u|--user\t\tBitbucket user\n"
printf "\t-p|--password\t\tBitbucket password\n"
printf "\t-t|--project\tName of the Bitbucket project (defaults to '%s')\n" "${BITBUCKET_PROJECT}"
printf "\t-r|--repository\tName of the repository"
printf "\t-f|--file\tFile to upload"
printf "\t-n|--filename\tName of the file"

}


function create_url() {
url=$1
user=$2
password=$3

# URL encode the @ symbol in the username
user=$(echo $user | sed 's/@/%40/g')

protocol=$(echo $url | grep :// | sed -e's,^\(.*://\).*,\1,g')
url=$(echo $url | sed -e s,$protocol,,g)

echo "${protocol}${user}:${password}@${url}"
}

while [[ "$#" -gt 0 ]]; do
case $1 in

-v|--verbose) set -x;;

-h|--help) usage; exit 0;;

-b|--bitbucket) BITBUCKET_URL="$2"; shift;;
-b=*|--bitbucket=*) BITBUCKET_URL="${1#*=}";;

-u|--user) BITBUCKET_USER="$2"; shift;;
-u=*|--user=*) BITBUCKET_USER="${1#*=}";;

-p|--password) BITBUCKET_PWD="$2"; shift;;
-p=*|--password=*) BITBUCKET_PWD="${1#*=}";;

-t|--project) BITBUCKET_PROJECT="$2"; shift;;
-t=*|--project=*) BITBUCKET_PROJECT="${1#*=}";;

-r|--repository) REPOSITORY="$2"; shift;;
-r=*|--repository=*) REPOSITORY="${1#*=}";;

-f|--file) FILE="$2"; shift;;
-f=*|--file=*) FILE="${1#*=}";;

-n|--filename) REPO_FILE="$2"; shift;;
-n=*|--filename=*) REPO_FILE="${1#*=}";;

*) echo_error "Unknown parameter passed: $1"; exit 1;;
esac; shift; done

# Usage
url=$(create_url "$BITBUCKET_URL" "$BITBUCKET_USER" "$BITBUCKET_PWD")

# Create a temporary directory and store its name in a variable
TEMP_DIR=$(mktemp -d)

# Clone the repository into the temporary directory
git clone "${url}/scm/${BITBUCKET_PROJECT}/${REPOSITORY}.git" "${TEMP_DIR}"

# Change into the temporary directory
cd "${TEMP_DIR}"

# Switch to the desired branch
git checkout "${BRANCH}"

# Copy the file into the repository
cp -f "$SCRIPT_DIR/${FILE}" "${REPO_FILE}"

# Add the file to the repository
git add "${REPO_FILE}"

# Commit the change
git commit -m "Automated commit from script"

# Push the change
git push origin "${BRANCH}"

# Change back to the original directory
cd -

# Remove the temporary directory
rm -rf "${TEMP_DIR}"

0 comments on commit b3decea

Please sign in to comment.