diff --git a/.github/workflows/create_pr_on_pr.yml b/.github/workflows/create_pr_on_pr.yml index 576905d..a8bfdb5 100644 --- a/.github/workflows/create_pr_on_pr.yml +++ b/.github/workflows/create_pr_on_pr.yml @@ -60,25 +60,27 @@ jobs: continue fi - # TODO: Remove - Get the date of the latest commit on the english branch - # latest_commit_date=$(git show -s --format=%ci ${EN_BRANCH}) + echo "Changed files: $changed_files" - # echo "Commits on the English branch that were made after the latest commit on the translation branch at $latest_commit_date" - # latest_commit_en_branch=$(git show --format=%H -s ${EN_BRANCH}) - # latest_commit_info=$(git log ${EN_BRANCH} --since="$latest_commit_date" --format="%H %s" --reverse) - # commit_messages=$(echo "$latest_commit_info" | cut -d' ' -f2-) - # latest_commit_main=$(git show --format=%H -s origin/main) - # echo $latest_commit_en_branch - # echo $latest_commit_main - # echo $latest_commit_info - - # # Check if there are new commits - # if [ "$latest_commit_en_branch" == "$latest_commit_main" ]; then - # echo "No new commits to include in PR for ${TRANSLATION_BRANCH}" - # continue - # fi - # TODO: Remove + #! Get the date of the latest commit on the english branch + latest_commit_date=$(git show -s --format=%ci ${EN_BRANCH}) + + echo "Commits on the English branch that were made after the latest commit on the translation branch at $latest_commit_date" + latest_commit_en_branch=$(git show --format=%H -s ${EN_BRANCH}) + latest_commit_info=$(git log ${EN_BRANCH} --since="$latest_commit_date" --format="%H %s" --reverse) + commit_messages=$(echo "$latest_commit_info" | cut -d' ' -f2-) + latest_commit_main=$(git show --format=%H -s origin/master) + + echo $latest_commit_en_branch + echo $latest_commit_main + echo $latest_commit_info + + #! Check if there are new commits + if [ "$latest_commit_en_branch" == "$latest_commit_main" ]; then + echo "No new commits to include in PR for ${TRANSLATION_BRANCH}" + continue + fi #! Check if a PR already exists for this branch PR_EXISTS=$(gh pr list --head "${TRANSLATION_BRANCH}" --state open --json number --jq length) @@ -93,34 +95,39 @@ jobs: fi echo "Pull Request Number: $PR_NUMBER" + + #! Initialize new PR body + new_pr_body="# Automated pull request for $lang handbook version ${VERSION_SUFFIX/_en/}" #! Add new commits as checkboxes to the PR description IFS=$'\n' # Change the Internal Field Separator to newline for correct iteration over lines checkboxes="" for file in $changed_files; do - commits=$(git log --oneline "${EN_BRANCH}" -- "$file" | awk '{print "#"$1}' | paste -s -d ', ' -) - checkboxes="$checkboxes- [ ] Chapter \`$file\` has new changes in the following commit(s) ($commits)." - - #! Mention a user in the PR description - case "$lang" in - - "vn") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; - "fr") checkboxes="$checkboxes @oliviabboyd, please review changes and check the box when you finish" ;; - "es") checkboxes="$checkboxes @amateo250, please review changes and check the box when you finish" ;; - "jp") checkboxes="$checkboxes @hitomik723, please review changes and check the box when you finish" ;; - "tr") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; - "pt") checkboxes="$checkboxes @Luccan97, please review changes and check the box when you finish" ;; - "ru") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; - esac - checkboxes="$checkboxes\n" + #! List only new commits compared with the main branch, tail -n +2 to skip the first line of output from git log, the latest commit will not be added into the message + list_commits=$(git log origin/main..${TRANSLATION_BRANCH} --follow --pretty=format:"%H" -- $file | tail -n +2 | paste -sd, - | sed 's/,/, /g') + for commit in $latest_commit_en_branch; do + checkboxes="$checkboxes- [ ] Chapter [\`$file\`](https://github.com/${{ github.repository }}/pull/$PR_NUMBER/files?file-filters%5B%5D=.qmd&show-viewed-files=true) has new changes in the following commit(s): $list_commits. " + + #! Mention a user in the PR description + case "$lang" in + "vn") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; + "fr") checkboxes="$checkboxes @oliviabboyd, please review changes and check the box when you finish" ;; + "es") checkboxes="$checkboxes @amateo250, please review changes and check the box when you finish" ;; + "jp") checkboxes="$checkboxes @hitomik723, please review changes and check the box when you finish" ;; + "tr") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; + "pt") checkboxes="$checkboxes @Luccan97, please review changes and check the box when you finish" ;; + "ru") checkboxes="$checkboxes @ntluong95, please review changes and check the box when you finish" ;; + esac + + checkboxes="$checkboxes"$'\n'"$checkbox" + + done done - - # Retrieve the current PR description - current_pr_body=$(gh pr view $PR_NUMBER --json body --jq '.body') - - # Append checkboxes to the current PR description - new_pr_body=$(printf "%s\n%s" "$current_pr_body" "$checkboxes") + if [ -n "$checkboxes" ]; then + # Append the checkboxes to the new PR body + new_pr_body="$new_pr_body"$'\n'"$checkboxes" + fi gh api repos/${{ github.repository }}/issues/$PR_NUMBER --method PATCH --field body="$new_pr_body"