From 5c5120e3d74307a65c3799bb32f619d3b86fe734 Mon Sep 17 00:00:00 2001 From: Terry Geng Date: Sat, 31 Oct 2020 20:30:05 +0800 Subject: [PATCH] CI: Setup pushing-translation procedure in CI. --- .drone.yml | 19 +++++++++- scripts/commit_new_translation.sh | 20 ++++++++--- scripts/update_translation_to_server.sh | 48 +++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100755 scripts/update_translation_to_server.sh diff --git a/.drone.yml b/.drone.yml index 1bb1f47..417521f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -17,6 +17,23 @@ steps: - push - tag + - name: push-translation + image: debian + environment: + TRADUORA_R_CLIENT: + from_secret: TRADUORA_R_CLIENT + TRADUORA_R_SECRET: + from_secret: TRADUORA_R_SECRET + TRADUORA_W_CLIENT: + from_secret: TRADUORA_W_CLIENT + TRADUORA_W_SECRET: + from_secret: TRADUORA_W_SECRET + GITHUB_API: + from_secret: GITHUB_API + commands: + - apt update && apt install -y git python3-requests hub + - SOURCE_DIR=$(pwd) ./scripts/update_translation_to_server.sh + - name: translate-html image: python:3 commands: @@ -126,7 +143,7 @@ steps: from_secret: GITHUB_API commands: - apt update && apt install -y git python3-requests hub - - SOURCE_DIR=$(pwd) ./scripts/commit_new_translation.sh + - PUSH=1 SOURCE_DIR=$(pwd) ./scripts/commit_new_translation.sh node: location: external diff --git a/scripts/commit_new_translation.sh b/scripts/commit_new_translation.sh index cc2bc63..7359603 100755 --- a/scripts/commit_new_translation.sh +++ b/scripts/commit_new_translation.sh @@ -2,20 +2,32 @@ git remote set-url origin https://azlux:$GITHUB_API@github.com/azlux/botamusique/ +echo "=> Fetching for bot-traduora branch..." if git fetch origin bot-traduora; then + echo "==> bot-traduora branch exists" git branch bot-traduora FETCH_HEAD CREATE_PR=false else + echo "==> bot-traduora branch doesn't exist, create one" git branch bot-traduora CREATE_PR=true fi git checkout bot-traduora + +echo "=> Fetching updates from the server..." + $SOURCE_DIR/scripts/sync_translation.py --lang-dir $SOURCE_DIR/lang/ --client $TRADUORA_R_CLIENT --secret $TRADUORA_R_SECRET --fetch git add lang/* git status -if GIT_COMMITTER_NAME='Traduora Bot' GIT_COMMITTER_EMAIL='noreply@azlux.fr' git commit -m 'Bot: Update translation' --author "Traduora Bot "; then - git push origin bot-traduora - sleep 2 - if $CREATE_PR; then GITHUB_USER="azlux" GITHUB_TOKEN="$GITHUB_API" hub pull-request -m "Bot: TRADUORA Update"; fi +if $PUSH; then + echo "=> Pushing updates to bot-traduora branch..." + if GIT_COMMITTER_NAME='Traduora Bot' GIT_COMMITTER_EMAIL='noreply@azlux.fr' git commit -m 'Bot: Update translation' --author "Traduora Bot "; then + git push origin bot-traduora + sleep 2 + if $CREATE_PR; then GITHUB_USER="azlux" GITHUB_TOKEN="$GITHUB_API" hub pull-request -m "Bot: TRADUORA Update"; fi + exit 0 + fi + echo "==> There's nothing to push." + exit 0 fi diff --git a/scripts/update_translation_to_server.sh b/scripts/update_translation_to_server.sh new file mode 100755 index 0000000..7deff99 --- /dev/null +++ b/scripts/update_translation_to_server.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -e +PYTHON=$SOURCE_DIR/venv/bin/python + +git remote set-url origin https://azlux:$GITHUB_API@github.com/azlux/botamusique/ +git pull origin master + +echo "=> Checking if translations in this commit differ from the server..." + +git branch testing-translation master +git checkout testing-translation +$PYTHON $SOURCE_DIR/scripts/sync_translation.py --lang-dir $SOURCE_DIR/lang/ --client $TRADUORA_R_CLIENT --secret $TRADUORA_R_SECRET --fetch + +if [ -z "$(git diff)" ]; then + echo "==> No difference found." + exit 0 +fi + +echo "==> Modifications found." +echo "=> Check if the modifications are based on the translations on the server..." + +n=1 +COMMON_FOUND=false + +while [ $n -le 10 ]; do + echo "==> Comparing server's translations with master~$n ($(git show --oneline --quiet master~$n))" + CHANGED_LANG_FILE=$(git diff --name-only master~$n | grep "lang/" || true) + if [ -z "$CHANGED_LANG_FILE" ]; then + COMMON_FOUND=true + break + fi + let n++ +done + +if [ ! $COMMON_FOUND ]; then + echo "==> CONFLICTS: Previous commits doesn't share the same translations with the server." + echo " There are unmerged translation updates on the server." + echo " Please manually update these changes or wait for the pull request" + echo " created by the translation bot get merged." + exit 1 +fi + +echo "==> master~$n ($(git show --oneline --quiet master~$n)) shares the same translations with the server." + +echo "=> Preparing to push local translation updates to the server..." +$PYTHON $SOURCE_DIR/scripts/sync_translation.py --lang-dir $SOURCE_DIR/lang/ --client $TRADUORA_W_CLIENT --secret $TRADUORA_W_SECRET --push +exit 0