Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix menu insertion on short or empty descriptions #15

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions git-mr
Original file line number Diff line number Diff line change
Expand Up @@ -1603,25 +1603,40 @@ mr_menu_contents() {

mr_menu_print_title() {
local issue_code=$1
local menu_items=$2
local current_index=$3
local issue_title=$2
local issue_url=$3
local menu_items=$4
local current_index=$5

local search_url; search_url="https://${GITLAB_DOMAIN}/dashboard/merge_requests?scope=all&state=all&search=$(urlencode "$issue_code")&in=title&sort=created_asc"
local mr_count; mr_count=$(echo "$menu_items" | wc -l | tr -d ' ')

echo "================================================================================"
[[ -z $current_index ]] &&
echo " $(terminal_link "$search_url" "$issue_code") (${mr_count} merge request$([[ $mr_count -gt 1 ]] && echo 's'))" ||
echo " $(terminal_link "$search_url" "$issue_code") (merge request ${current_index}/${mr_count})"
if [[ -n $issue_title ]] && [[ -n $issue_url ]]; then
echo " $(terminal_link "$search_url" "$issue_code") $(terminal_link "$issue_url" "$issue_title") (${mr_count} merge request$([[ $mr_count -gt 1 ]] && echo 's'))"
has_links || echolor " ⇒ $issue_url" "midgray"
else
[[ -z $current_index ]] &&
echo " $(terminal_link "$search_url" "$issue_code") (${mr_count} merge request$([[ $mr_count -gt 1 ]] && echo 's'))" ||
echo " $(terminal_link "$search_url" "$issue_code") (merge request ${current_index}/${mr_count})"
fi
echo "================================================================================"
}

mr_menu_status() {
local issue_code=$1
local menu_items=$2

local issue_content issue_key issue_title issue_url
issue_content=$(jira_ticket_data "$issue_code")
eval "$(echo "$issue_content" | jq -r '
"issue_key=" + (.key // empty | @sh) + ";\n" +
"issue_title=" + (.fields.summary // empty | @sh) + ";\n"
')"
[[ -n $issue_title ]] && issue_url="https://${JIRA_INSTANCE}/browse/${issue_key}"

echo
mr_menu_print_title "$issue_code" "$menu_items"
mr_menu_print_title "$issue_code" "$issue_title" "$issue_url" "$menu_items"
echo

while IFS= read -r menu_item; do
Expand Down Expand Up @@ -1668,7 +1683,7 @@ mr_menu_show() {
local menu_items=$2

echo
mr_menu_print_title "$issue_code" "$menu_items"
mr_menu_print_title "$issue_code" "" "" "$menu_items"
echo

mr_menu_contents "$menu_items"
Expand Down Expand Up @@ -1766,7 +1781,7 @@ mr_menu_update_all() {
updated_description="$(mr_menu_replace_description "$mr_description" "$mr_menu_content")"

clear_screen
mr_menu_print_title "$issue_code" "$menu_items" "$i"
mr_menu_print_title "$issue_code" "" "" "$menu_items" "$i"
mr_menu_print_description "$updated_description" "$mr_url" "$mr_title" "$project_name"

mr_update_data='{}'
Expand Down Expand Up @@ -1817,8 +1832,10 @@ mr_menu_replace_description() {
[[ $menu_start -gt 0 && $menu_end -gt "$menu_start" ]] &&
has_menu=1

local menu_was_output=0

# Iterate over description lines and insert/replace menu
local i=1 mr_description_line
local i=1 mr_description_line prev_description_line
while IFS=$'\n' read -r mr_description_line; do
if [[ $has_menu -eq 1 ]]; then
if [[ $i -lt "$menu_start" || $i -gt "$menu_end" ]]; then
Expand All @@ -1829,20 +1846,30 @@ mr_menu_replace_description() {
if [[ $i -eq "$menu_end" ]]; then
echo_debug "Replacing menu"
echo "$menu_content"
menu_was_output=1
fi
fi
else
# output non-menu line
echo "$mr_description_line"
# insert menu once
if [[ $i -eq "2" ]]; then
if [[ $menu_was_output -eq 0 && -z "$mr_description_line" && $i -gt 1 ]]; then
echo_debug "Inserting menu"
echo "$menu_content"
echo
menu_was_output=1
fi
fi

prev_description_line="$mr_description_line"
((i += 1))
done < <(echo "$mr_description")

if [[ $menu_was_output -eq 0 ]]; then
echo_debug "Inserting menu (failsafe)"
[[ -n "$prev_description_line" ]] && echo
echo "$menu_content"
fi
}

mr_menu_colorize() {
Expand Down
150 changes: 128 additions & 22 deletions test/git-mr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1658,8 +1658,13 @@ End"
@test "Searches MRs across projects to build menu" {
load "test_helper/gitlab-mock-menu.bash"

run mr_menu
run mr_menu XY-789
assert_output "$(cat <<- EOF
No merge requests found for 'XY-789'.
EOF
)"

run mr_menu
assert_output "$(cat <<- EOF

================================================================================
Expand All @@ -1668,17 +1673,88 @@ End"

## Menu

* Project C: [MR 31 title](https://example.net/31)
* Project A: [MR 11 title](https://example.net/11)
* Project B: [MR 21 title](https://example.net/21)
* Project C: [MR 31 title](https://gitlab.example.net/proj-C/-/merge_requests/31)
* Project A: [MR 11 title](https://gitlab.example.net/proj-A/-/merge_requests/11)
* Project B: [MR 21 title](https://gitlab.example.net/proj-B/-/merge_requests/21)

--------------------------------------------------------------------------------
EOF
)"
}

@test "Prints menu title" {
run mr_menu_print_title "AB-123" "" "" "$(echo -e "a\nb\nc")"
assert_output "$(cat <<- EOF
================================================================================
AB-123 (3 merge requests)
================================================================================
EOF
)"

run mr_menu_print_title "AB-123" "" "" "$(echo -e "a\nb\nc")" 1
assert_output "$(cat <<- EOF
================================================================================
AB-123 (merge request 1/3)
================================================================================
EOF
)"

run mr_menu_print_title "AB-123" "My Issue" "https://example.com/AB-123" "$(echo -e "a\nb\nc")"
assert_output "$(cat <<- EOF
================================================================================
AB-123 My Issue (3 merge requests)
⇒ https://example.com/AB-123
================================================================================
EOF
)"
}

@test "Prints menu status" {
load "test_helper/gitlab-mock-menu.bash"
load "test_helper/jira-mock.bash"

run mr_menu_status "AB-123" "$(mr_menu_merge_requests "AB-123")"
assert_output "$(cat <<-EOF

================================================================================
AB-123 This is an issue (3 merge requests)
⇒ https://mycompany.example.net/browse/AB-123
================================================================================

* Project C: MR 31 title
⇒ https://gitlab.example.net/proj-C/-/merge_requests/31

🏷 [Accepted] (↣ main)

👍 3 👎 0 CI: ⏰ Can be merged: ✔


* Project A: MR 11 title
⇒ https://gitlab.example.net/proj-A/-/merge_requests/11

🏷 [QA] (↣ main)

👍 2 👎 0 CI: ⏱ Can be merged: ✔


* Project B: MR 21 title
⇒ https://gitlab.example.net/proj-B/-/merge_requests/21

🏷 [Review] (↣ main)

👍 0 👎 1 CI: ❌ Can be merged: ❌
EOF
)"
}

@test "Replaces menu in MR descriptions" {

local menu_content="## Menu
* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------"

# *** Replace menu in description ***
mr_description="# [AB-123 Test feature](https://example.net/AB-123)

This is an example.
Expand All @@ -1696,13 +1772,6 @@ This is an example.
* Lorem
* Ipsum

--------------------------------------------------------------------------------"

menu_content="## Menu

* New Menu item 1
* New Menu item 2

--------------------------------------------------------------------------------"

run mr_menu_replace_description "$mr_description" "$menu_content"
Expand All @@ -1713,10 +1782,8 @@ This is an example.
--------------------------------------------------------------------------------

## Menu

* New Menu item 1
* New Menu item 2

--------------------------------------------------------------------------------

## Commits
Expand All @@ -1726,7 +1793,7 @@ This is an example.

--------------------------------------------------------------------------------"


# *** Insert menu in description ***
mr_description="# [AB-123 Test feature](https://example.net/AB-123)

This is an example without menu.
Expand All @@ -1736,29 +1803,68 @@ This is an example without menu.
* Lorem
* Ipsum"

menu_content="## Menu
run mr_menu_replace_description "$mr_description" "$menu_content"
assert_output "# [AB-123 Test feature](https://example.net/AB-123)

## Menu
* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------

This is an example without menu.

## Commits

* Lorem
* Ipsum"

# *** Insert menu in empty description ***

mr_description=""
run mr_menu_replace_description "$mr_description" "$menu_content"
assert_output "
## Menu
* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------"

# *** Insert menu in minimal description ***

mr_description="This is a merge request."
run mr_menu_replace_description "$mr_description" "$menu_content"
assert_output "# [AB-123 Test feature](https://example.net/AB-123)
assert_output "This is a merge request.

## Menu

* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------"

--------------------------------------------------------------------------------
mr_description="This is a merge request
paragraph."
run mr_menu_replace_description "$mr_description" "$menu_content"
assert_output "This is a merge request
paragraph.

This is an example without menu.
## Menu
* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------"

## Commits
mr_description="This is
a merge request
longer
paragraph."
run mr_menu_replace_description "$mr_description" "$menu_content"
assert_output "This is
a merge request
longer
paragraph.

## Menu
* New Menu item 1
* New Menu item 2
--------------------------------------------------------------------------------"

* Lorem
* Ipsum"
}

################################################################################
Expand Down
Loading
Loading