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

added the ability to download sources #165

Merged
merged 3 commits into from
Feb 15, 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ In case you don’t want to download translations from Crowdin (`download_transl
# This can be used to pass down any supported argument of the `upload translations` cli command, e.g.
upload_translations_args: '--auto-approve-imported --translate-hidden'

# Download sources options
download_sources: true
push_sources: true
# this can be used to pass down any supported argument of the `download sources` cli command, e.g.
download_sources_args: '--reviewed'

# Download translations options
download_translations: true
download_language: 'uk'
Expand Down
18 changes: 16 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,23 @@ inputs:
default: ''
required: false

# download sources options
download_sources:
description: 'Defines whether to download source files from the Crowdin project'
default: 'false'
required: false
push_sources:
description: 'Push downloaded sources to the branch'
default: 'true'
required: false
download_sources_args:
description: 'Additional arguments which will be passed to the `download sources` cli command'
default: ''
required: false

# download translations options
download_translations:
description: 'Make pull request of Crowdin translations'
description: 'Defines whether to download translation files from the Crowdin project'
default: 'false'
required: false
download_language:
Expand All @@ -59,7 +73,7 @@ inputs:
default: 'false'
required: false
push_translations:
description: 'Download translations with pushing to branch'
description: 'Push downloaded translations to the branch'
default: 'true'
required: false
commit_message:
Expand Down
46 changes: 36 additions & 10 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ upload_translations() {
crowdin upload translations "$@" $UPLOAD_TRANSLATIONS_OPTIONS
}

download_sources() {
if [ -n "$INPUT_DOWNLOAD_SOURCES_ARGS" ]; then
DOWNLOAD_SOURCES_OPTIONS="${DOWNLOAD_SOURCES_OPTIONS} ${INPUT_DOWNLOAD_SOURCES_ARGS}"
fi

echo "DOWNLOAD SOURCES"
crowdin download sources "$@" $DOWNLOAD_SOURCES_OPTIONS
}

download_translations() {
if [ -n "$INPUT_DOWNLOAD_LANGUAGE" ]; then
DOWNLOAD_TRANSLATIONS_OPTIONS="${DOWNLOAD_TRANSLATIONS_OPTIONS} --language=${INPUT_DOWNLOAD_LANGUAGE}"
Expand Down Expand Up @@ -64,7 +73,7 @@ download_translations() {
}

create_pull_request() {
LOCALIZATION_BRANCH="${1}"
BRANCH="${1}"

AUTH_HEADER="Authorization: token ${GITHUB_TOKEN}"
HEADER="Accept: application/vnd.github.v3+json; application/vnd.github.antiope-preview+json; application/vnd.github.shadow-cat-preview+json"
Expand Down Expand Up @@ -95,12 +104,12 @@ create_pull_request() {
fi
fi

PULL_REQUESTS_QUERY_PARAMS="?base=${BASE_BRANCH}&head=${LOCALIZATION_BRANCH}"
PULL_REQUESTS_QUERY_PARAMS="?base=${BASE_BRANCH}&head=${BRANCH}"

PULL_REQUESTS=$(echo "$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X GET "${PULLS_URL}${PULL_REQUESTS_QUERY_PARAMS}")" | jq --raw-output '.[] | .head.ref ')

# check if pull request exist
if echo "$PULL_REQUESTS " | grep -q "$LOCALIZATION_BRANCH "; then
if echo "$PULL_REQUESTS " | grep -q "$BRANCH "; then
echo "PULL REQUEST ALREADY EXIST"
else
echo "CREATE PULL REQUEST"
Expand All @@ -109,7 +118,7 @@ create_pull_request() {
BODY=",\"body\":\"${INPUT_PULL_REQUEST_BODY//$'\n'/\\n}\""
fi

PULL_RESPONSE_DATA="{\"title\":\"${INPUT_PULL_REQUEST_TITLE}\", \"base\":\"${BASE_BRANCH}\", \"head\":\"${LOCALIZATION_BRANCH}\" ${BODY}}"
PULL_RESPONSE_DATA="{\"title\":\"${INPUT_PULL_REQUEST_TITLE}\", \"base\":\"${BASE_BRANCH}\", \"head\":\"${BRANCH}\" ${BODY}}"
# create pull request
PULL_RESPONSE=$(curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${PULL_RESPONSE_DATA}" "${PULLS_URL}")

Expand Down Expand Up @@ -172,7 +181,7 @@ create_pull_request() {
}

push_to_branch() {
LOCALIZATION_BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}
BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}

REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${INPUT_GITHUB_BASE_URL}/${GITHUB_REPOSITORY}.git"

Expand All @@ -184,10 +193,10 @@ push_to_branch() {
git checkout "${GITHUB_REF#refs/heads/}"
fi

if [ -n "$(git show-ref refs/heads/${LOCALIZATION_BRANCH})" ]; then
git checkout "${LOCALIZATION_BRANCH}"
if [ -n "$(git show-ref refs/heads/${BRANCH})" ]; then
git checkout "${BRANCH}"
else
git checkout -b "${LOCALIZATION_BRANCH}"
git checkout -b "${BRANCH}"
fi

git add .
Expand All @@ -197,12 +206,12 @@ push_to_branch() {
return
fi

echo "PUSH TO BRANCH ${LOCALIZATION_BRANCH}"
echo "PUSH TO BRANCH ${BRANCH}"
git commit --no-verify -m "${INPUT_COMMIT_MESSAGE}"
git push --no-verify --force "${REPO_URL}"

if [ "$INPUT_CREATE_PULL_REQUEST" = true ]; then
create_pull_request "${LOCALIZATION_BRANCH}"
create_pull_request "${BRANCH}"
fi
}

Expand Down Expand Up @@ -327,6 +336,23 @@ if [ "$INPUT_UPLOAD_TRANSLATIONS" = true ]; then
upload_translations "$@"
fi

if [ "$INPUT_DOWNLOAD_SOURCES" = true ]; then
download_sources "$@"

if [ "$INPUT_PUSH_SOURCES" = true ]; then
[ -z "${GITHUB_TOKEN}" ] && {
echo "CAN NOT FIND 'GITHUB_TOKEN' IN ENVIRONMENT VARIABLES"
exit 1
}

[ -n "${INPUT_GPG_PRIVATE_KEY}" ] && {
setup_commit_signing
}

push_to_branch
fi
fi

if [ "$INPUT_DOWNLOAD_TRANSLATIONS" = true ]; then
download_translations "$@"

Expand Down