Skip to content

Commit

Permalink
added the ability to download sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Smolinskyi committed Feb 15, 2023
1 parent 64e208e commit 418fa46
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 33 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ 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'

sources_commit_message: 'New Crowdin sources by Github Action'
sources_branch_name: sources_crowdin_action
sources_create_pull_request: true
sources_pull_request_title: 'New Crowdin sources'
sources_pull_request_body: 'New Crowdin pull request with sources'
sources_pull_request_labels: 'enhancement, good first issue'
sources_pull_request_assignees: 'crowdin-bot'
sources_pull_request_reviewers: 'crowdin-reviewer'
# This is the name of the git branch to with pull request will be created.
# If not specified default repository branch will be used.
sources_pull_request_base_branch_name: not_default_branch

# Download translations options
download_translations: true
download_language: 'uk'
Expand Down
49 changes: 49 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,55 @@ inputs:
default: ''
required: false

# download sources options
download_sources:
description: 'Make pull request of Crowdin sources'
default: 'false'
required: false
push_sources:
description: 'Download sources with pushing to branch'
default: 'true'
required: false
download_sources_args:
description: 'Additional arguments which will be passed to the `download sources` cli command'
default: ''
required: false
sources_commit_message:
description: 'Commit message for download sources'
default: 'New Crowdin sources by Github Action'
required: false
sources_branch_name:
description: 'To download translations to the specified version branch'
default: 'sources_crowdin_action'
required: false
sources_create_pull_request:
description: 'Create pull request after pushing to branch'
default: 'true'
required: false
sources_pull_request_title:
description: 'The title of the new pull request'
default: 'New Crowdin sources by Github Action'
required: false
sources_pull_request_body:
description: 'The contents of the pull request'
required: false
sources_pull_request_assignees:
description: 'Add up to 10 assignees to the created pull request (separated by comma)'
required: false
sources_pull_request_reviewers:
description: 'Usernames of people from whom a review is requested for this pull request (separated by comma)'
required: false
sources_pull_request_labels:
description: 'To add labels for created pull request'
required: false
sources_pull_request_base_branch_name:
description: 'Create pull request to specified branch instead of default one'
required: false
sources_skip_ref_checkout:
description: 'Skip default git checkout on GITHUB_REF'
default: 'false'
required: false

# download translations options
download_translations:
description: 'Make pull request of Crowdin translations'
Expand Down
121 changes: 88 additions & 33 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,24 @@ download_translations() {
}

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

if [ "${PUSH_TYPE}" == "sources" ]; then
PULL_REQUEST_TITLE="${INPUT_SOURCES_PULL_REQUEST_TITLE}"
PULL_REQUEST_BODY="${INPUT_SOURCES_PULL_REQUEST_BODY}"
PULL_REQUEST_ASSIGNEES="${INPUT_SOURCES_PULL_REQUEST_ASSIGNEES}"
PULL_REQUEST_REVIEWERS="${INPUT_SOURCES_PULL_REQUEST_REVIEWERS}"
PULL_REQUEST_LABELS="${INPUT_SOURCES_PULL_REQUEST_LABELS}"
PULL_REQUEST_BASE_BRANCH_NAME="${INPUT_SOURCES_PULL_REQUEST_BASE_BRANCH_NAME}"
else
PULL_REQUEST_TITLE="${INPUT_PULL_REQUEST_TITLE}"
PULL_REQUEST_BODY="${INPUT_PULL_REQUEST_BODY}"
PULL_REQUEST_ASSIGNEES="${INPUT_PULL_REQUEST_ASSIGNEES}"
PULL_REQUEST_REVIEWERS="${INPUT_PULL_REQUEST_REVIEWERS}"
PULL_REQUEST_LABELS="${INPUT_PULL_REQUEST_LABELS}"
PULL_REQUEST_BASE_BRANCH_NAME="${INPUT_PULL_REQUEST_BASE_BRANCH_NAME}"
fi

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 All @@ -85,8 +111,8 @@ create_pull_request() {

echo "CHECK IF ISSET SAME PULL REQUEST"

if [ -n "$INPUT_PULL_REQUEST_BASE_BRANCH_NAME" ]; then
BASE_BRANCH="$INPUT_PULL_REQUEST_BASE_BRANCH_NAME"
if [ -n "$PULL_REQUEST_BASE_BRANCH_NAME" ]; then
BASE_BRANCH="$PULL_REQUEST_BASE_BRANCH_NAME"
else
if [ -n "$GITHUB_HEAD_REF" ]; then
BASE_BRANCH=${GITHUB_HEAD_REF}
Expand All @@ -95,21 +121,21 @@ 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"

if [ -n "$INPUT_PULL_REQUEST_BODY" ]; then
BODY=",\"body\":\"${INPUT_PULL_REQUEST_BODY//$'\n'/\\n}\""
if [ -n "$PULL_REQUEST_BODY" ]; then
BODY=",\"body\":\"${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\":\"${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 All @@ -118,52 +144,52 @@ create_pull_request() {
PULL_REQUESTS_NUMBER=$(echo "${PULL_RESPONSE}" | jq '.number')
view_debug_output

if [ -n "$INPUT_PULL_REQUEST_LABELS" ]; then
PULL_REQUEST_LABELS=$(echo "[\"${INPUT_PULL_REQUEST_LABELS}\"]" | sed 's/, \|,/","/g')
if [ -n "$PULL_REQUEST_LABELS" ]; then
REQUEST_LABELS=$(echo "[\"${PULL_REQUEST_LABELS}\"]" | sed 's/, \|,/","/g')

if [ "$(echo "$PULL_REQUEST_LABELS" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
if [ "$(echo "$REQUEST_LABELS" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
echo "ADD LABELS TO PULL REQUEST"

ISSUE_URL="${REPO_URL}/issues/${PULL_REQUESTS_NUMBER}"

LABELS_DATA="{\"labels\":${PULL_REQUEST_LABELS}}"
LABELS_DATA="{\"labels\":${REQUEST_LABELS}}"

# add labels to created pull request
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X PATCH --data "${LABELS_DATA}" "${ISSUE_URL}"
else
echo "JSON OF pull_request_labels IS INVALID: ${PULL_REQUEST_LABELS}"
echo "JSON OF pull_request_labels IS INVALID: ${REQUEST_LABELS}"
fi
fi

if [ -n "$INPUT_PULL_REQUEST_ASSIGNEES" ]; then
PULL_REQUEST_ASSIGNEES=$(echo "[\"${INPUT_PULL_REQUEST_ASSIGNEES}\"]" | sed 's/, \|,/","/g')
if [ -n "$PULL_REQUEST_ASSIGNEES" ]; then
REQUEST_ASSIGNEES=$(echo "[\"${PULL_REQUEST_ASSIGNEES}\"]" | sed 's/, \|,/","/g')

if [ "$(echo "$PULL_REQUEST_ASSIGNEES" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
if [ "$(echo "$REQUEST_ASSIGNEES" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
echo "ADD ASSIGNEES TO PULL REQUEST"

ASSIGNEES_URL="${REPO_URL}/issues/${PULL_REQUESTS_NUMBER}/assignees"

ASSIGNEES_DATA="{\"assignees\":${PULL_REQUEST_ASSIGNEES}}"
ASSIGNEES_DATA="{\"assignees\":${REQUEST_ASSIGNEES}}"

# add assignees to created pull request
curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${ASSIGNEES_DATA}" "${ASSIGNEES_URL}"
else
echo "JSON OF pull_request_assignees IS INVALID: ${PULL_REQUEST_ASSIGNEES}"
echo "JSON OF pull_request_assignees IS INVALID: ${REQUEST_ASSIGNEES}"
fi
fi

if [ -n "$INPUT_PULL_REQUEST_REVIEWERS" ]; then
PULL_REQUEST_REVIEWERS=$(echo "[\"${INPUT_PULL_REQUEST_REVIEWERS}\"]" | sed 's/, \|,/","/g')
if [ -n "$PULL_REQUEST_REVIEWERS" ]; then
REQUEST_REVIEWERS=$(echo "[\"${PULL_REQUEST_REVIEWERS}\"]" | sed 's/, \|,/","/g')

if [ "$(echo "$PULL_REQUEST_REVIEWERS" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
if [ "$(echo "$REQUEST_REVIEWERS" | jq -e . > /dev/null 2>&1; echo $?)" -eq 0 ]; then
echo "ADD REVIEWERS TO PULL REQUEST"

REVIEWERS_URL="${REPO_URL}/pulls/${PULL_REQUESTS_NUMBER}/requested_reviewers"
REVIEWERS_DATA="{\"reviewers\":${PULL_REQUEST_REVIEWERS}}"
REVIEWERS_DATA="{\"reviewers\":${REQUEST_REVIEWERS}}"

curl -sSL -H "${AUTH_HEADER}" -H "${HEADER}" -X POST --data "${REVIEWERS_DATA}" "${REVIEWERS_URL}"
else
echo "JSON OF pull_request_reviewers IS INVALID: ${PULL_REQUEST_REVIEWERS}"
echo "JSON OF pull_request_reviewers IS INVALID: ${REQUEST_REVIEWERS}"
fi
fi

Expand All @@ -172,22 +198,34 @@ create_pull_request() {
}

push_to_branch() {
LOCALIZATION_BRANCH=${INPUT_LOCALIZATION_BRANCH_NAME}
PUSH_TYPE="${1}"

if [ "${PUSH_TYPE}" == "sources" ]; then
BRANCH="${INPUT_SOURCES_BRANCH_NAME}"
COMMIT_MESSAGE="${INPUT_SOURCES_COMMIT_MESSAGE}"
SKIP_REF_CHECKOUT="${INPUT_SOURCES_SKIP_REF_CHECKOUT}"
CREATE_PULL_REQUEST="${INPUT_SOURCES_CREATE_PULL_REQUEST}"
else
BRANCH="${INPUT_LOCALIZATION_BRANCH_NAME}"
COMMIT_MESSAGE="${INPUT_COMMIT_MESSAGE}"
SKIP_REF_CHECKOUT="${INPUT_SKIP_REF_CHECKOUT}"
CREATE_PULL_REQUEST="${INPUT_CREATE_PULL_REQUEST}"
fi

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

echo "CONFIGURATION GIT USER"
git config --global user.email "${INPUT_GITHUB_USER_EMAIL}"
git config --global user.name "${INPUT_GITHUB_USER_NAME}"

if [ "$INPUT_SKIP_REF_CHECKOUT" != true ] && [ ${GITHUB_REF#refs/heads/} != $GITHUB_REF ]; then
if [ "$SKIP_REF_CHECKOUT" != true ] && [ ${GITHUB_REF#refs/heads/} != $GITHUB_REF ]; then
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 +235,12 @@ push_to_branch() {
return
fi

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

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

Expand Down Expand Up @@ -327,6 +365,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 "sources"
fi
fi

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

Expand All @@ -340,7 +395,7 @@ if [ "$INPUT_DOWNLOAD_TRANSLATIONS" = true ]; then
setup_commit_signing
}

push_to_branch
push_to_branch "translations"
fi
fi

Expand Down

0 comments on commit 418fa46

Please sign in to comment.