Skip to content
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ export JIRA_OK_ID="xx"
#export GIT_MR_TIMEOUT=10
```

Environment-only configuration:
```bash
## What to show as merge request title in `git mr menu status`. Available values:
## - "both": Merge request title and branch (default)
## - "title": Merge request title only
## - "branch": Merge request source branch only
#GIT_MR_MENU_STATUS_SHOW=both

## Separator between merge request title and branch when GIT_MR_MENU_STATUS_SHOW=both
#GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR=" " # default
#GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR="\n " # send to the next line
```

#### Jira & Gitlab tokens

To create a Jira API Token, go to:
Expand Down
33 changes: 18 additions & 15 deletions doc/generate-sample-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ GITLAB_QA_LABELS="Testing"
GITLAB_OK_LABELS="Accepted"

issue_code="XY-1234"

ticket_title="${issue_code} Lorem Ipsum"
ticket_url="https://mycompany.atlassian.net/browse/${issue_code}"
ticket_link="$(markdown_link "$ticket_title" "$ticket_url")"
issue_title="Lorem Ipsum"
issue_full_title="${issue_code} ${issue_title}"
issue_url="https://mycompany.atlassian.net/browse/${issue_code}"
issue_link="$(markdown_link "$issue_full_title" "$issue_url")"

mr_title="Feature/${issue_code} Lorem Ipsum"
mr_url="https://myapp.gitlab.com/my/project/merge_requests/6"
Expand All @@ -37,7 +37,7 @@ fake_prompt() {
local branch=${2:-"feature/xy-1234-lorem-ipsum"}

colorize "\n___________________________________________________________________________________________\n\n\n" "gray"
echo "$(colorize "me@mystation" "green"):$(colorize "~/my-project" "lightblue") $(colorize "($branch)" "purple") $(colorize "↔ ✔" "green") $ $cmd"
echo "$(colorize "me@mystation" "green"):$(colorize "~/my-project" "lightblue") $(colorize "($branch)" "purple") $(colorize "↔ ✔ " "green") $ $cmd"
}

c_same() {
Expand All @@ -60,7 +60,7 @@ sample_mr() {
cat <<EOF

--------------------------------------------------------------------------------
$(mr_print_description "$ticket_link" "* **78330c9 In vulputate quam ac ultrices volutpat**
$(mr_print_description "$issue_link" "* **78330c9 In vulputate quam ac ultrices volutpat**
* **0010a6a Curabitur vel purus sed tortor finibus posuere**
* **3621817 Aenean sed sem hendrerit ex egestas** ")

Expand All @@ -78,7 +78,7 @@ sample_mr_extended() {
cat <<EOF

--------------------------------------------------------------------------------
$(mr_print_description "$ticket_link" "* **78330c9 In vulputate quam ac ultrices volutpat**
$(mr_print_description "$issue_link" "* **78330c9 In vulputate quam ac ultrices volutpat**
Some commit description
* **0010a6a Curabitur vel purus sed tortor finibus posuere**
Extended description
Expand Down Expand Up @@ -158,7 +158,7 @@ sample_mr_update() {

$(mr_print_title "$mr_title" "$mr_url")

$(markdown_title "$ticket_link")
$(markdown_title "$issue_link")

Vivamus venenatis tortor et neque sollicitudin, eget suscipit est malesuada.
Suspendisse nec odio id arcu sagittis pulvinar ut nec lacus.
Expand Down Expand Up @@ -213,7 +213,7 @@ sample_mr_update_links() {

$(mr_print_title "$mr_title" "$mr_url")

$(markdown_title "$ticket_link")
$(markdown_title "$issue_link")

Vivamus venenatis tortor et neque sollicitudin, eget suscipit est malesuada.
Suspendisse nec odio id arcu sagittis pulvinar ut nec lacus.
Expand Down Expand Up @@ -342,22 +342,25 @@ sample_mr_menu_status() {
cat <<EOF

================================================================================
$(terminal_link "$search_url" "$issue_code") (3 merge requests)
$(terminal_link "$search_url" "$issue_code") $(terminal_link "$search_url" "$issue_title") (3 merge requests)
================================================================================

EOF

echo "* $(colorize "Some Project" "bold"): $(terminal_link "https://myapp.gitlab.com/some/project/-/merge_requests/12" "Feature/XY-1234 Lorem Ipsum")"
echo -n "* $(colorize "Some Project" "bold"): $(terminal_link "https://myapp.gitlab.com/some/project/-/merge_requests/12" "Feature/XY-1234 Lorem Ipsum")"
echo " $(colorize "( " "gray")$(terminal_link "https://myapp.gitlab.com/some/project/-/merge_requests/12" "$(colorize "feat/XY-1234-lorem" "lightpurple")")$(colorize ")" "gray")"
has_links || echolor " ⇒ https://myapp.gitlab.com/some/project/-/merge_requests/12" "midgray"
mr_print_status "$mr1" "$approvals1" "$threads1"
echo

echo "* $(colorize "Other Project" "bold"): $(terminal_link "https://myapp.gitlab.com/other/project/-/merge_requests/34" "Feature/XY-1234 Quisque sed")"
echo -n "* $(colorize "Other Project" "bold"): $(terminal_link "https://myapp.gitlab.com/other/project/-/merge_requests/34" "Feature/XY-1234 Quisque sed")"
echo " $(colorize "( " "gray")$(terminal_link "https://myapp.gitlab.com/other/project/-/merge_requests/34" "$(colorize "feat/XY-1234-quisque" "lightpurple")")$(colorize ")" "gray")"
has_links || echolor " ⇒ https://myapp.gitlab.com/some/project/-/merge_requests/34" "midgray"
mr_print_status "$mr2" "$approvals2" "$threads2"
echo

echo "* $(colorize "Third Project" "bold"): $(terminal_link "https://myapp.gitlab.com/third/project/-/merge_requests/56" "Feature/XY-1234 Nunc vestibulum")"
echo -n "* $(colorize "Third Project" "bold"): $(terminal_link "https://myapp.gitlab.com/third/project/-/merge_requests/56" "Feature/XY-1234 Nunc vestibulum")"
echo " $(colorize "( " "gray")$(terminal_link "https://myapp.gitlab.com/third/project/-/merge_requests/56" "$(colorize "feat/XY-1234-nunc" "lightpurple")")$(colorize ")" "gray")"
has_links || echolor " ⇒ https://myapp.gitlab.com/some/project/-/merge_requests/56" "midgray"
mr_print_status "$mr3" "$approvals3" "$threads3"
echo
Expand Down Expand Up @@ -451,9 +454,9 @@ if [[ "$#" -gt 0 ]]; then
sample_mr_update_links
;;
menu)
sample_mr_menu
# sample_mr_menu
sample_mr_menu_status
sample_mr_menu_update
# sample_mr_menu_update
;;
merge)
sample_mr_merge
Expand Down
Binary file modified doc/git-mr-menu-status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 53 additions & 23 deletions git-mr
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,7 @@ gitlab_request() {
--max-time "${GIT_MR_TIMEOUT}" \
"${gitlab_base_url}/${request_url}") || exit_error "$ERR_GITLAB_API" "Gitlab request error"

if [[ $request_verb != "GET" ]]; then
[[ -z "$(gitlab_check_error "$result")" ]] || return "$ERR_GITLAB"
fi
gitlab_check_error "$result" || return "$ERR_GITLAB"

echo "$result"
}
Expand All @@ -650,8 +648,7 @@ gitlab_check_error() {

if [[ $hasErrorMessage == 'true' ]]; then
echo_error "\nGitlab error:\n ${result}"

echo "ko"
return "$ERR_GITLAB"
fi
}

Expand Down Expand Up @@ -704,9 +701,11 @@ gitlab_merge_request_summary() {
local source_branch=${1:-$(git_current_branch)}

local result
result=$(gitlab_current_project_request "merge_requests?state=opened&view=simple&source_branch=${source_branch}") || exit $?
result=$(gitlab_current_project_request "merge_requests?state=opened&view=simple&source_branch=${source_branch}") || return $?

[[ -n $result && $result != "[]" ]] || return "$ERR_GITLAB"
[[ -z $result ]] && return "$ERR_GITLAB"

[[ $result == "[]" ]] && return 0

echo "$result" | jq '.[0]'
}
Expand Down Expand Up @@ -1098,10 +1097,9 @@ mr_description() {

if [[ "${GIT_MR_NO_COMMITS}" == "1" ]]; then
mr_print_description "$title"
else
else
mr_print_description "$title" "$(mr_commit_list "$source_branch" "$target_branch")"
fi

}

mr_print_description() {
Expand Down Expand Up @@ -1745,19 +1743,32 @@ mr_menu_status() {
gitlab_load_approvals "$mr_url"
gitlab_load_threads "$mr_url"

mr_title=$(gitlab_title_undraft "$mr_title")
# Read MR (to get source branch)
local merge_request
gitlab_read_mr merge_request
local source_branch
eval "$(echo "$merge_request" | jq -r '
"source_branch=" + (.source_branch | @sh) + ";\n"
')"

# Print MR title line
echo -n "* $(colorize "$project_name" "bold"): "
if has_links; then
echolink "$mr_url" "$mr_title"
else
echo "$mr_title"
echolor " ⇒ $mr_url" "midgray"

local show="$GIT_MR_MENU_STATUS_SHOW"
if [[ $show == "title" || $show == "both" ]]; then
terminal_link "$mr_url" "$(gitlab_title_undraft "$mr_title")"
fi
[[ $show == "both" ]] && echo -en "$GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR"
if [[ $show == "branch" || $show == "both" ]]; then
echo -n "$(colorize "( " "gray")"
echo -n "$(terminal_link "$mr_url" "$(colorize "$source_branch" "lightpurple")")"
echo -n "$(colorize ")" "gray")"
fi
echo
has_links || echolor " ⇒ $mr_url" "midgray"

# Read MR & threads
local merge_request mr_approvals mr_threads
gitlab_read_mr merge_request
local mr_approvals mr_threads
gitlab_read_approvals mr_approvals
gitlab_read_threads mr_threads

Expand Down Expand Up @@ -2032,7 +2043,15 @@ $(mr_description "$source_branch" "$target_branch" "$issue_code")

EOF

local mr_summary; mr_summary=$(gitlab_merge_request_summary "$source_branch")
local mr_summary
if [[ -z $_gitlab_error ]]; then
mr_summary=$(gitlab_merge_request_summary "$source_branch") || _gitlab_error=$?
fi
if [[ $_gitlab_error -gt 0 ]]; then
echo
return $_gitlab_error
fi

if [[ -n $mr_summary ]]; then
# Print merge request status summary
mr_status_block "$mr_summary"
Expand Down Expand Up @@ -2102,13 +2121,17 @@ mr_status() {
local source_branch=${1:-$(git_current_branch)}

# Search existing merge request
local mr_summary; mr_summary=$(gitlab_merge_request_summary "$source_branch")
if [[ -z $mr_summary ]]; then
local mr_summary; mr_summary=$(gitlab_merge_request_summary "$source_branch") || _gitlab_error=$?

if [[ -z $_gitlab_error && -z $mr_summary ]]; then
_gitlab_error=0
echo_error "Merge request not found"
fi

if [[ -n $_gitlab_error ]]; then
git_check_branches "$source_branch" "-"
mr_print "$source_branch"
return "$ERR_GITLAB"
return $_gitlab_error
fi

# Print merge request title & status
Expand Down Expand Up @@ -2204,13 +2227,13 @@ mr_update() {

# Iterate over commit lists, compare sha-1 and update description
for i in ${!current_commits_long_array[*]}; do

local oldSha=${old_commits_array[$i]}

local currSha=${current_commits_short_array[$i]}
local currShaaaa=${current_commits_long_array[$i]}
local currMsg=${current_commits_messages_array[$i]}

local oldCommitUrl="$mr_url/diffs?commit_id=$oldSha"
local currCommitUrl="$mr_url/diffs?commit_id=$currShaaaa"

Expand Down Expand Up @@ -2864,6 +2887,8 @@ _git_mr_commit_ref='[0-9a-fA-F]{7,}'
_git_mr_commit_href="\[${_git_mr_commit_ref}\]\(http[^)]+\)"
_git_mr_commit_regex="^${_git_mr_commit_prefix}(${_git_mr_commit_ref}|${_git_mr_commit_href})\s"

_gitlab_error=

# Custom file descriptors
git_mr_fd_mr=${GIT_MR_FD_MR=}
git_mr_fd_ap=${GIT_MR_FD_AP=}
Expand Down Expand Up @@ -2909,6 +2934,11 @@ GIT_MR_REQUIRED_UPVOTES=${GIT_MR_REQUIRED_UPVOTES:-2}
GITLAB_REMOVE_SOURCE_BRANCH_ON_MERGE=${GITLAB_REMOVE_SOURCE_BRANCH_ON_MERGE:-1}
GIT_MR_TIMEOUT=${GIT_MR_TIMEOUT:-10}

GIT_MR_MENU_STATUS_SHOW=both
#GIT_MR_MENU_STATUS_SHOW=title
#GIT_MR_MENU_STATUS_SHOW=branch
GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR=" "
#GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR="\n "

################################################################################
# Run
Expand Down
3 changes: 3 additions & 0 deletions test/.gitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
jira-qa-id = "130"
jira-ok-id = "140"

gitlab-mr-limit-group = ""
gitlab-projects-limit-member = ""

gitlab-ip-labels = "WIP"
gitlab-cr-labels = "Review"
gitlab-qa-labels = "Testing"
Expand Down
50 changes: 46 additions & 4 deletions test/git-mr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -651,22 +651,26 @@ sha_link() {

@test "Warns for Gitlab API request errors" {
run gitlab_check_error '{"error":"failed"}'
assert_failure
assert_output "$(cat <<- EOF

Gitlab error:
{"error":"failed"}
ko
EOF
)"

run gitlab_check_error '{"message":"failed"}'
assert_failure
assert_output "$(cat <<- EOF

Gitlab error:
{"message":"failed"}
ko
EOF
)"

run gitlab_check_error '{"ok":"ok"}'
assert_success
assert_output ""
}

@test "Determines new merge request URL" {
Expand Down Expand Up @@ -705,7 +709,7 @@ sha_link() {
"merge_requests?state=opened&view=simple&source_branch=feature/nope")
echo '[]'
;;
*) return ;;
*) return $ERR_GITLAB;;
esac
}

Expand All @@ -716,7 +720,7 @@ sha_link() {
run gitlab_extract_title "$mr_summary"; assert_output "Draft: Feature/XY-1234 Lorem Ipsum"

run gitlab_merge_request_summary "feature/nope"
assert_failure "$ERR_GITLAB"
assert_success
assert_output ""

run gitlab_merge_request_summary "nope"
Expand Down Expand Up @@ -2137,6 +2141,8 @@ End"
load "test_helper/gitlab-mock-menu.bash"
load "test_helper/jira-mock.bash"

GIT_MR_MENU_STATUS_SHOW=title

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

Expand Down Expand Up @@ -2169,6 +2175,42 @@ End"
☑️ 1/2 👍 0 👎 1 CI: ❌ Can be merged: ❌
EOF
)"

GIT_MR_MENU_STATUS_SHOW=both
GIT_MR_MENU_STATUS_TITLE_BRANCH_SEPARATOR=" "

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 ( feature/branch-31)
⇒ https://gitlab.example.net/proj-C/-/merge_requests/31

🏷 [Accepted] (↣ main)

👍 3 👎 0 Threads: 1/2 CI: ⏰ Can be merged: ✔


* Project A: MR 11 title ( feature/branch-11)
⇒ https://gitlab.example.net/proj-A/-/merge_requests/11

🏷 [QA] (↣ main)

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


* Project B: MR 21 title ( feature/branch-21)
⇒ https://gitlab.example.net/proj-B/-/merge_requests/21

🏷 [Review] (↣ main)

☑️ 1/2 👍 0 👎 1 CI: ❌ Can be merged: ❌
EOF
)"
}

@test "Replaces menu in MR descriptions" {
Expand Down
Loading