Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.6 | AWS & File functions #16

Merged
merged 15 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 45 additions & 10 deletions aws-functions.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
#!/bin/bash

function bucketExist() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [shellcheck] reported by reviewdog 🐶
Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive. SC2148

BUCKET=$1
BUCKET="$1"

BUCKET_EXISTS=$(aws s3api head-bucket --bucket $BUCKET 2>&1 || true)
BUCKET_EXISTS=$(aws s3api head-bucket --bucket "$BUCKET" 2>&1 || true)
if [ -z "$BUCKET_EXISTS" ]; then
echo 0
else
echo 1
fi
}

function getAccountId() {
aws sts get-caller-identity --query "Account" --output text
}

function copyFileToS3() {
SOURCE_FILE=$1
S3_BUCKET=$2
KEY_NAME=$3
SOURCE_FILE="$1"
S3_BUCKET="$2"
KEY_NAME="$3"

aws s3 cp ${SOURCE_FILE} s3://${S3_BUCKET}/${KEY_NAME}
aws s3 cp "${SOURCE_FILE}" "s3://${S3_BUCKET}/${KEY_NAME}"
}

function copyFileFromS3() {
S3_BUCKET=$1
FILE_KEY=$2
FILE_PATH=$3
aws s3 cp s3://${S3_BUCKET}/${FILE_KEY} ${FILE_PATH}
S3_BUCKET="$1"
FILE_KEY="$2"
FILE_PATH="$3"
aws s3 cp "s3://${S3_BUCKET}/${FILE_KEY}" "${FILE_PATH}"
}

function policyExists() {
POLICY_ARN="$1"

aws iam get-policy --policy-arn "${POLICY_ARN}" >/dev/null 2>&1
echo $?
}

function createPolicy() {
POLICY_NAME="$1"
POLICY_FILE_PATH="$2"

aws iam create-policy \
--policy-name "${POLICY_NAME}" --no-paginate \
--policy-document "file://${POLICY_FILE_PATH}"
}

function roleExists() {
ROLE_NAME="$1"

aws iam get-role --role-name "${ROLE_NAME}" >/dev/null 2>&1
echo $?
}

function createRole() {
ROLE_NAME="$1"
POLICY_DOCUMENT="$2"
aws iam create-role --role-name "${ROLE_NAME}" --no-paginate --assume-role-policy-document "file://${POLICY_DOCUMENT}"
}
21 changes: 18 additions & 3 deletions file-functions.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

function isFileExist() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [shellcheck] reported by reviewdog 🐶
Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive. SC2148

FILEPATH=$1
FILEPATH="$1"

if [ -f "$FILEPATH" ]; then
echo 0
Expand All @@ -9,8 +11,21 @@ function isFileExist() {
}

function fileContainsString() {
FILEPATH=$1
FILEPATH="$1"
TEXT="$2"
grep -q ${TEXT} ${FILEPATH}
grep -q "${TEXT}" "${FILEPATH}"
echo $?
}

function getLineForAString() {
FILEPATH="$1"
TEXT="$2"
grep "${TEXT}" "${FILEPATH}"
}

function textExistsInALine(){
LINE="$1"
TEXT="$2"
echo "${LINE}" | grep -q "${TEXT}"
echo "$?"
}
20 changes: 15 additions & 5 deletions functions.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

generateOutput() {
Task=$1
Status=$2
Message=$3
ACTIVITY_SUB_TASK_CODE="$1"
Status="$2"
Message="$3"

EXECUTION_DIR="/bp/execution_dir"
OUTPUT_DIR="${EXECUTION_DIR}/${EXECUTION_TASK_ID}"
file_name="$OUTPUT_DIR/summary.json"
Expand Down Expand Up @@ -32,9 +32,19 @@ function getRepositoryTag() {
echo "$BUILD_REPOSITORY_TAG"
}

function getDockerfileParentPath() {
DOCKERFILE_ENTRY=$(jq -r .build_detail.dockerfile_path < /bp/data/environment_build)
getNthTextInALine "$DOCKERFILE_ENTRY" : 2
}

function getDockerfileName() {
DOCKERFILE_ENTRY=$(jq -r .build_detail.dockerfile_path < /bp/data/environment_build)
getNthTextInALine "$DOCKERFILE_ENTRY" : 1
}

function saveTaskStatus() {
TASK_STATUS=$1
ACTIVITY_SUB_TASK_CODE=$2
TASK_STATUS="$1"
ACTIVITY_SUB_TASK_CODE="$2"

if [ "$TASK_STATUS" -eq 0 ]
then
Expand Down
47 changes: 47 additions & 0 deletions git-functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

function switchBranch() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [shellcheck] reported by reviewdog 🐶
Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive. SC2148

TGT_BRANCH="$1"

git checkout "$TGT_BRANCH"
}

function showStatusInShortFormat() {
git status -s
}

function findConflictingFiles() {
SRC_BRANCH="$1"
TGT_BRANCH="$2"

git checkout -q "${TGT_BRANCH}"
# git pull origin "${TGT_BRANCH}"

git checkout -q -b temp_merge_branch
git merge -q "$SRC_BRANCH" 1> /dev/null

conflicts=$(git diff --name-only --diff-filter=U)
git merge --abort
git checkout -q "${TGT_BRANCH}"
git branch -q -D temp_merge_branch
echo "$conflicts"
}

function getLastAuthorOfFile() {
BRANCH="$1"
FILE="$2"

git checkout -q "$BRANCH"

git log -1 --pretty=format:"%an" "${FILE}"
}

function getLastAuthorOfFiles() {
BRANCH="$1"
FILES="$2"
for FILE in "${FILES[@]}"
do
author=$(getLastAuthorOfFile "$BRANCH" "$FILE")
echo "${FILE}: ${author}"
done
}
10 changes: 5 additions & 5 deletions log-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ COLOR_START="\e["
COLOR_END="\e[0m"

function logColoredMessage() {
COLOR=$1
LOG_LEVEL=$2
COLOR="$1"
LOG_LEVEL="$2"
MESSAGE="$3"

CURRENT_DATE=$(date "+%D: %T")
Expand All @@ -19,16 +19,16 @@ function logColoredMessage() {
function logInfoMessage() {
MESSAGE="$1"

logColoredMessage ${GREEN} INFO "${MESSAGE}"
logColoredMessage "${GREEN}" INFO "${MESSAGE}"
}

function logErrorMessage() {
MESSAGE="$1"

logColoredMessage ${RED} ERROR "${MESSAGE}"
logColoredMessage "${RED}" ERROR "${MESSAGE}"
}

function logWarningMessage() {
MESSAGE="$1"
logColoredMessage ${YELLOW} WARNING "${MESSAGE}"
logColoredMessage "${YELLOW}" WARNING "${MESSAGE}"
}
14 changes: 12 additions & 2 deletions str-functions.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#!/bin/bash

function isStrNonEmpty() {
STR=$1
STR="$1"
if [ -z "$STR" ]; then
echo 1
else
echo 0
fi
}
}

function getNthTextInALine() {
LINE="$1"
SEPARATOR="$2"
POSITION="$3"

echo "${LINE}" | awk -F "${SEPARATOR}" -v POS="${POSITION}" "{print $POS}"
}