Skip to content

Commit

Permalink
Fix bug when no commits exist
Browse files Browse the repository at this point in the history
  • Loading branch information
noraworld committed Jul 11, 2022
1 parent cef758b commit cd05c40
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions main
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ pr_list_json() {
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/commits/{}/pulls"
)

commits_associated_with_merged_prs=$(
echo "$merged_prs" |
jq -r .[].number |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/pulls/{}/commits" --jq .[].sha
)
if [ "$merged_prs" != "" ]; then
commits_associated_with_merged_prs=$(
echo "$merged_prs" |
jq -r .[].number |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/pulls/{}/commits" --jq .[].sha
)
else
commits_associated_with_merged_prs=""
fi

no_merge_commits=$(
git --no-pager log --oneline --reverse --no-merges --no-abbrev-commit origin/"$BASE"..origin/"$HEAD" |
Expand All @@ -57,15 +61,29 @@ pr_list_json() {

# (no merge commits) - (commits associated with merged PRs) =
# (squash commits & rebase commits & commits not associated with any PRs)
squash_and_rebase_and_orphan_commits=$(
echo "$no_merge_commits" |
grep -v "$commits_associated_with_merged_prs" |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/commits/{}/pulls"
)

# Combine (merged PRs) & (squash commits & rebase commits & commits not associated with any PRs)
if [ "$no_merge_commits" != "" ] && [ "$commits_associated_with_merged_prs" != "" ]; then
squash_and_rebase_prs=$(
echo "$no_merge_commits" |
grep -v "$commits_associated_with_merged_prs" |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/commits/{}/pulls"
)
elif [ "$no_merge_commits" != "" ]; then
squash_and_rebase_prs=$(
echo "$no_merge_commits" |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/commits/{}/pulls"
)
elif [ "$commits_associated_with_merged_prs" != "" ]; then
squash_and_rebase_prs=$(
echo "$commits_associated_with_merged_prs" |
xargs -I {} gh api "https://api.github.com/repos/$REPOSITORY/commits/{}/pulls"
)
else
squash_and_rebase_prs=""
fi

# Combine (merged PRs) & (squash and rebase PRs)
result=$(
printf "%s\n%s" "$merged_prs" "$squash_and_rebase_and_orphan_commits" |
printf "%s\n%s" "$merged_prs" "$squash_and_rebase_prs" |
jq -s 'add'
)

Expand Down

0 comments on commit cd05c40

Please sign in to comment.