|
1 | 1 | #!/bin/sh -l |
2 | 2 |
|
3 | | -# Run Replexica CLI to localize missing strings |
4 | | -# USe REPLEXICA_VERSION env variable to specify the version of Replexica CLI |
5 | | -npx replexica@${REPLEXICA_VERSION} i18n |
6 | | -# Return exit code 1 if the previous command fails |
7 | | -if [ $? -eq 1 ]; then |
8 | | - echo "::error::🚨 Replexica incurred an error while applying translations. Discord: https://replexica.com/go/discord" |
9 | | - exit 1 |
10 | | -fi |
11 | | -# Commit the changes into the current branch |
12 | | -git config --global --add safe.directory $PWD |
13 | | -git config --global user.name "Replexica" |
14 | | -git config --global user.email "support@replexica.com" |
15 | | -git add . |
16 | | -if git diff --staged --quiet; then |
17 | | - echo "::notice::Replexica has not found any missing translations" |
18 | | -else |
19 | | - git commit -m "feat: add missing translations" |
20 | | - # Pull the latest changes from the remote repository |
21 | | - git pull --rebase |
22 | | - git push |
23 | | - # retrieve the last commit hash |
24 | | - COMMIT_HASH=$(git rev-parse HEAD) |
25 | | - # build a github url to the commit |
26 | | - COMMIT_URL="https://github.com/$GITHUB_REPOSITORY/commit/$COMMIT_HASH" |
27 | | - # output the commit url to the github actions announcements using ::notice:: |
28 | | - echo "::notice::Replexica has just added missing translations and pushed them to the repo! The commit: $COMMIT_URL" |
29 | | -fi |
| 3 | +set -e |
| 4 | + |
| 5 | +# Function to run Replexica CLI |
| 6 | +run_replexica() { |
| 7 | + npx replexica@${REPLEXICA_VERSION} i18n |
| 8 | + if [ $? -eq 1 ]; then |
| 9 | + echo "::error::Replexica: Translation update failed. For assistance, join our Discord: https://replexica.com/go/discord" |
| 10 | + exit 1 |
| 11 | + fi |
| 12 | +} |
| 13 | + |
| 14 | +# Function to configure git |
| 15 | +configure_git() { |
| 16 | + git config --global --add safe.directory $PWD |
| 17 | + git config --global user.name "Replexica" |
| 18 | + git config --global user.email "support@replexica.com" |
| 19 | +} |
| 20 | + |
| 21 | +# Function to get the current branch name |
| 22 | +get_current_branch() { |
| 23 | + local branch_name="" |
| 24 | + |
| 25 | + if [ -n "$GITHUB_HEAD_REF" ]; then |
| 26 | + # Pull request |
| 27 | + branch_name="$GITHUB_HEAD_REF" |
| 28 | + elif [ -n "$GITHUB_REF_NAME" ]; then |
| 29 | + # Push or workflow_dispatch |
| 30 | + if echo "$GITHUB_REF" | grep -q "^refs/tags/"; then |
| 31 | + # It's a tag, return the default branch |
| 32 | + branch_name=$(gh api repos/:owner/:repo | jq -r .default_branch) |
| 33 | + else |
| 34 | + branch_name="$GITHUB_REF_NAME" |
| 35 | + fi |
| 36 | + elif [ -n "$GITHUB_REF" ]; then |
| 37 | + # Fallback for other cases |
| 38 | + branch_name=$(echo "$GITHUB_REF" | sed 's:^refs/[^/]*/::') |
| 39 | + else |
| 40 | + echo "::error::Replexica: Unable to determine the current branch name. For assistance, join our Discord: https://replexica.com/go/discord" |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + echo "$branch_name" |
| 45 | +} |
| 46 | + |
| 47 | +# Function to commit changes |
| 48 | +commit_changes() { |
| 49 | + git add . |
| 50 | + if ! git diff --staged --quiet; then |
| 51 | + git commit -m "${REPLEXICA_COMMIT_MESSAGE}" |
| 52 | + echo "Changes committed successfully" |
| 53 | + return 0 |
| 54 | + else |
| 55 | + echo "::notice::Replexica: All translations are up to date." |
| 56 | + return 1 |
| 57 | + fi |
| 58 | +} |
| 59 | + |
| 60 | +# Function to create or update PR |
| 61 | +create_or_update_pr() { |
| 62 | + local current_branch=$(get_current_branch) |
| 63 | + local pr_branch="replexica/${current_branch}" |
| 64 | + local pr_title="${REPLEXICA_PULL_REQUEST_TITLE}" |
| 65 | + |
| 66 | + # Create or update the branch |
| 67 | + git checkout -B "$pr_branch" |
| 68 | + git push -f origin "$pr_branch" |
| 69 | + |
| 70 | + # Check if PR already exists |
| 71 | + existing_pr=$(gh pr list --head "$pr_branch" --json number --jq '.[0].number') |
| 72 | + |
| 73 | + # Read PR body from adjacent file called pr.md |
| 74 | + pr_body_file="/pr.md" |
| 75 | + |
| 76 | + local pr_create_args="--title \"$pr_title\" --body-file \"$pr_body_file\" --head \"$pr_branch\"" |
| 77 | + local pr_edit_args="--title \"$pr_title\" --body-file \"$pr_body_file\"" |
| 78 | + |
| 79 | + add_assignees_to_pr_args |
| 80 | + add_labels_to_pr_args "$existing_pr" |
| 81 | + |
| 82 | + # Create new PR or update existing one |
| 83 | + if [ -n "$existing_pr" ]; then |
| 84 | + eval gh pr edit "$existing_pr" $pr_edit_args |
| 85 | + echo "::notice::Replexica: Updated existing PR #$existing_pr with translations. Review it here: https://github.com/$GITHUB_REPOSITORY/pull/$existing_pr" |
| 86 | + else |
| 87 | + new_pr=$(eval gh pr create $pr_create_args) |
| 88 | + echo "::notice::Replexica: Created new PR with translations. Review it here: $new_pr" |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Function to add assignees to PR arguments |
| 93 | +add_assignees_to_pr_args() { |
| 94 | + if [ -n "$REPLEXICA_PULL_REQUEST_ASSIGNEES" ]; then |
| 95 | + OLD_IFS="$IFS" |
| 96 | + IFS=',' |
| 97 | + for assignee in $REPLEXICA_PULL_REQUEST_ASSIGNEES; do |
| 98 | + pr_create_args="$pr_create_args --assignee \"$assignee\"" |
| 99 | + pr_edit_args="$pr_edit_args --add-assignee \"$assignee\"" |
| 100 | + done |
| 101 | + IFS="$OLD_IFS" |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +# Function to add labels to PR arguments |
| 106 | +add_labels_to_pr_args() { |
| 107 | + local existing_pr="$1" |
| 108 | + if [ -n "$REPLEXICA_PULL_REQUEST_LABELS" ]; then |
| 109 | + OLD_IFS="$IFS" |
| 110 | + IFS=',' |
| 111 | + for label in $REPLEXICA_PULL_REQUEST_LABELS; do |
| 112 | + # Check if label exists in the repository |
| 113 | + if ! gh label list | grep -q "^$label "; then |
| 114 | + # Create label if it doesn't exist in the repository |
| 115 | + gh label create "$label" --color "#1ac964" || true > /dev/null |
| 116 | + fi |
| 117 | + |
| 118 | + # For new PRs, add all labels |
| 119 | + if [ -z "$existing_pr" ]; then |
| 120 | + pr_create_args="$pr_create_args --label \"$label\"" |
| 121 | + else |
| 122 | + # For existing PRs, check if the label is already present |
| 123 | + if ! gh pr view "$existing_pr" --json labels --jq '.labels[].name' | grep -q "^$label$"; then |
| 124 | + pr_edit_args="$pr_edit_args --add-label \"$label\"" |
| 125 | + fi |
| 126 | + fi |
| 127 | + done |
| 128 | + IFS="$OLD_IFS" |
| 129 | + fi |
| 130 | +} |
| 131 | + |
| 132 | +# Main execution |
| 133 | +main() { |
| 134 | + if [ "$REPLEXICA_PULL_REQUEST" = "true" ]; then |
| 135 | + if [ -z "$GH_TOKEN" ]; then |
| 136 | + echo "::error::Replexica: GitHub token is missing. Add 'GH_TOKEN: \${{ github.token }}' to your Replexica action configuration env section." |
| 137 | + exit 1 |
| 138 | + fi |
| 139 | + fi |
| 140 | + |
| 141 | + # Run Replexica to update translations |
| 142 | + run_replexica |
| 143 | + |
| 144 | + # Configure git for committing changes |
| 145 | + configure_git |
| 146 | + |
| 147 | + # Commit changes if any are found |
| 148 | + if commit_changes; then |
| 149 | + if [ "$REPLEXICA_PULL_REQUEST" = "true" ]; then |
| 150 | + # Create or update pull request |
| 151 | + create_or_update_pr |
| 152 | + else |
| 153 | + # Push changes directly to the repository |
| 154 | + git pull --rebase |
| 155 | + git push |
| 156 | + COMMIT_HASH=$(git rev-parse HEAD) |
| 157 | + COMMIT_URL="https://github.com/$GITHUB_REPOSITORY/commit/$COMMIT_HASH" |
| 158 | + echo "::notice::Replexica: Translation updates pushed successfully. View the commit: $COMMIT_URL" |
| 159 | + fi |
| 160 | + fi |
| 161 | +} |
| 162 | + |
| 163 | +# Run the main function |
| 164 | +main |
0 commit comments