[damned-lies/gettext: 1/2] feat: pre-commit hook to detect string changes




commit 7abe7aaf8baf79a874027b94d84d4c12c1b318b0
Author: Guillaume Bernard <associations guillaume-bernard fr>
Date:   Sun May 2 16:12:54 2021 +0200

    feat: pre-commit hook to detect string changes

 .githooks/check_messages_did_change_or_not.sh | 26 ++++++++++++++++++++++++++
 .githooks/pre-commit                          | 13 +++++++++++++
 .gitlab-ci.yml                                | 21 +++++++++++++++++++++
 3 files changed, 60 insertions(+)
---
diff --git a/.githooks/check_messages_did_change_or_not.sh b/.githooks/check_messages_did_change_or_not.sh
new file mode 100644
index 00000000..6bc287a2
--- /dev/null
+++ b/.githooks/check_messages_did_change_or_not.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+
+DIRNAME="$(dirname "${BASH_SOURCE[0]}")"
+
+get_translations_changed_differences_po_file() {
+    lang_code="fr"
+    original_po_file=$(mktemp --suffix original_po.po)
+    original_po_file_cleaned=$(mktemp --suffix original_po_cleaned.po)
+    translation_diff=$(mktemp --suffix translation_diff.po)
+
+    # Clean (remove strings coming from database) the currently committed PO file
+    cp po/${lang_code}.po "${original_po_file}"
+    # This removes messages that only exist in the database-content.py file generated by the update-trans 
command
+    perl -0777 -pe "s/#: (?:database-content\.py:[0-9]{1,5}\s*)*(?:\n.+)?\nmsgid ((?:\".*\"\n)*)msgstr 
((?:\".*\"\n)*)//g" "${original_po_file}" > "${original_po_file_cleaned}"
+    msgattrib --no-obsolete -o "${original_po_file_cleaned}" "${original_po_file_cleaned}"
+
+    # Call the update-trans command in order to update all the translations
+    "${DIRNAME}/../manage.py" update-trans ${lang_code} > /dev/null
+
+    # Compare code only strings and cleaned PO file contents. If the file is empty, no code string was 
updated
+    msgcomm locale/${lang_code}/LC_MESSAGES/code.po "${original_po_file_cleaned}" --sort-output --no-wrap 
--less-than=2 | sed '/^#[[:blank:]]\|#$/d' > "${translation_diff}"
+
+    # Replace the modified po file with the original one
+    cp "${original_po_file}" po/${lang_code}.po
+    echo "${translation_diff}"
+}
diff --git a/.githooks/pre-commit b/.githooks/pre-commit
new file mode 100755
index 00000000..732c6144
--- /dev/null
+++ b/.githooks/pre-commit
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+DIRNAME="$(dirname "${BASH_SOURCE[0]}")"
+
+source "${DIRNAME}/check_messages_did_change_or_not.sh"
+
+echo -e "\e[33mChecking if code strings are updated by this commit…\e[0m"
+translation_diff=$(get_translations_changed_differences_po_file)
+if [[ -s "${translation_diff}" ]]; then
+    echo -e "\e[1m\e[31m\t→ There are some code string changes. Please, open the ${translation_diff} to see 
the changes.\e[0m"
+else
+    echo -e "\e[1m\e[32m\t→ No string has been changed in the source code.\e[0m"
+fi
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c79c141a..3f5def8a 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -100,3 +100,24 @@ static-analysis:flake8:
     paths:
       - flake8.txt
     when: always
+
+
+code-string-changes:
+  stage: test
+  extends: .debian-based-job
+  variables:
+    TRANSLATION_DIFF_PO_FILENAME: translation_diff.po
+  script:
+    - echo "SECRET_KEY = 'key'" > damnedlies/local_settings.py
+    - ./manage.py migrate
+    - source "./.githooks/check_messages_did_change_or_not.sh"
+    - translation_diff=$(get_translations_changed_differences_po_file)
+    - mv "${translation_diff}" "${TRANSLATION_DIFF_PO_FILENAME}"
+    - if [[ -s ${TRANSLATION_DIFF_PO_FILENAME} ]]; then exit 1; else exit 0; fi
+  artifacts:
+    name: ${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHA}
+    expire_in: 2 days
+    expose_as: "Changed strings PO file"
+    paths:
+      - "${TRANSLATION_DIFF_PO_FILENAME}"
+    when: on_failure


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]