Skip to content

Commit

Permalink
Merge pull request #19 from Djuuu/feature/merge-request-group-filtering
Browse files Browse the repository at this point in the history
Filtering by project group & membership to build menu
  • Loading branch information
Djuuu authored Jun 26, 2024
2 parents 97a8298 + f4f348a commit c09fa24
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,14 @@ git config --global mr.jira-ok-id "xx"
# Required upvote count to turn indicator green in `mr status` (defaults to 2)
# git config --global mr.git-mr-required-upvotes 2

# Network timeout (in seconds, defaults to 5)
# git config --global mr.git-mr-timeout 5
# Limit merge request search for menu to given group (empty by default, useful on gitlab.com)
# git config --global mr.gitlab-mr-limit-group "my-company"

# Limit project name search to the ones you are a member of (defaults to 1, useful on gitlab.com)
# git config --global mr.gitlab-projects-limit-member 1

# Network timeout (in seconds, defaults to 10)
# git config --global mr.git-mr-timeout 10
```

**Tip:**
Expand Down Expand Up @@ -261,8 +267,14 @@ export JIRA_OK_ID="xx"
# Required upvote count to turn indicator green in `mr status` (defaults to 2)
#export GIT_MR_REQUIRED_UPVOTES=2

# Network timeout (in seconds, defaults to 5)
#export GIT_MR_TIMEOUT=5
# Limit merge request search for menu to given group (empty by default, useful on gitlab.com)
#GITLAB_MR_LIMIT_GROUP="my-company"

# Limit project name search to the ones you are a member of (defaults to 1, useful on gitlab.com)
#GITLAB_PROJECTS_LIMIT_MEMBER=1

# Network timeout (in seconds, defaults to 10)
#export GIT_MR_TIMEOUT=10
```

#### Jira & Gitlab tokens
Expand Down
21 changes: 18 additions & 3 deletions git-mr
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,13 @@ gitlab_merge_requests_search() {

local search_term=$1; [[ -n $search_term ]] || exit_error "$ERR_MR" "search_term required"

local api_prefix=""
if [[ -n $GITLAB_MR_LIMIT_GROUP ]]; then
api_prefix="groups/${GITLAB_MR_LIMIT_GROUP}/"
fi

local result
result=$(gitlab_request "merge_requests?scope=all&state=all&view=simple&search=$(urlencode "$search_term")&in=title&per_page=50&order_by=created_at&sort=asc") || exit $?
result=$(gitlab_request "${api_prefix}merge_requests?scope=all&state=all&view=simple&search=$(urlencode "$search_term")&in=title&per_page=50&order_by=created_at&sort=asc") || exit $?
[[ -n $result && $result != "[]" ]] || return "$ERR_GITLAB"

result=$(echo "$result" | jq -c 'map(select(.state != "closed") | {iid: .iid, title: .title, web_url: .web_url, state: .state, project_id: .project_id}) | sort_by(.state)')
Expand All @@ -729,7 +734,13 @@ gitlab_merge_requests_search() {
gitlab_projects() {
local result

result=$(gitlab_request "projects?simple=true&archived=false&order_by=last_activity_at&per_page=${GIT_MR_LAST_PROJECTS_LIMIT:-40}") || exit $?
local params=""
if [[ ${GITLAB_PROJECTS_LIMIT_MEMBER} -eq 1 ]]; then
params="&membership=true"
fi
params="${params}&archived=false&order_by=last_activity_at&per_page=${GIT_MR_LAST_PROJECTS_LIMIT:-50}"

result=$(gitlab_request "projects?simple=true${params}") || exit $?
[[ -n $result && $result != "[]" ]] || return "$ERR_GITLAB"

result=$(echo "$result" | jq 'map({id: .id, name: .name, path_with_namespace: .path_with_namespace})')
Expand Down Expand Up @@ -2865,6 +2876,9 @@ JIRA_TOKEN=${JIRA_TOKEN:-$( git config --get mr.jira-token || true)}
GITLAB_DOMAIN=${GITLAB_DOMAIN:-$(git config --get mr.gitlab-domain || true)}
GITLAB_TOKEN=${GITLAB_TOKEN:-$( git config --get mr.gitlab-token || true)}

GITLAB_MR_LIMIT_GROUP=${GITLAB_MR_LIMIT_GROUP:-$(git config --get mr.gitlab-mr-limit-group || true)}
GITLAB_PROJECTS_LIMIT_MEMBER=${GITLAB_PROJECTS_LIMIT_MEMBER:-$(git config --get mr.gitlab-projects-limit-member || true)}

JIRA_CODE_PATTERN=${JIRA_CODE_PATTERN:-$(git config --get mr.jira-code-pattern || true)}

JIRA_IP_ID=${JIRA_IP_ID:-$(git config --get mr.jira-ip-id || true)}
Expand All @@ -2886,9 +2900,10 @@ GITLAB_REMOVE_SOURCE_BRANCH_ON_MERGE=${GITLAB_REMOVE_SOURCE_BRANCH_ON_MERGE:-$(g
GIT_MR_TIMEOUT=${GIT_MR_TIMEOUT:-$(git config --get mr.git-mr-timeout || true)}

# Defaults
GITLAB_PROJECTS_LIMIT_MEMBER=${GITLAB_PROJECTS_LIMIT_MEMBER:-1}
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:-5}
GIT_MR_TIMEOUT=${GIT_MR_TIMEOUT:-10}


################################################################################
Expand Down
53 changes: 52 additions & 1 deletion test/git-mr.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ setup_file() {

# Unset all git-mr environment variables (would take precedence over local configuration)
unset JIRA_INSTANCE JIRA_USER JIRA_TOKEN JIRA_CODE_PATTERN \
GITLAB_DOMAIN GITLAB_TOKEN GITLAB_DEFAULT_LABELS \
GITLAB_DOMAIN GITLAB_TOKEN GITLAB_MR_LIMIT_GROUP GITLAB_DEFAULT_LABELS \
GITLAB_IP_LABELS GITLAB_CR_LABELS GITLAB_QA_LABELS GITLAB_OK_LABELS \
JIRA_IP_ID JIRA_CR_ID JIRA_QA_ID JIRA_OK_ID \
GITLAB_PROJECTS_LIMIT_MEMBER \
GIT_MR_EXTENDED GIT_MR_REQUIRED_UPVOTES GIT_MR_TIMEOUT

export GIT_MR_NO_COLORS=1
Expand Down Expand Up @@ -2028,6 +2029,56 @@ End"
################################################################################
# Merge request menu utility functions

@test "Searches projects" {
load "test_helper/gitlab-mock-search.bash"

run gitlab_projects
assert_success
refute_output --partial '"path_with_namespace": "public-group/project-a"' # not a member
refute_output --partial '"path_with_namespace": "public-group/project-b"' # not a member
assert_output --partial '"path_with_namespace": "private-group/project-c"'
assert_output --partial '"path_with_namespace": "private-group/project-d"'

GITLAB_PROJECTS_LIMIT_MEMBER=0

run gitlab_projects
assert_success
assert_output --partial '"path_with_namespace": "public-group/project-a"' # no membership filtering
assert_output --partial '"path_with_namespace": "public-group/project-b"' # no membership filtering
assert_output --partial '"path_with_namespace": "private-group/project-c"'
assert_output --partial '"path_with_namespace": "private-group/project-d"'

GITLAB_PROJECTS_LIMIT_MEMBER=1
}

@test "Searches MRs across projects filtering by group when configured" {
load "test_helper/gitlab-mock-search.bash"

run gitlab_merge_requests_search
assert_failure "search_term required"


run gitlab_merge_requests_search AB-123
assert_success
assert_output --partial "public-group/proj-C/-/merge_requests/31"
assert_output --partial "public-group/proj-A/-/merge_requests/11"
assert_output --partial "private-group/proj-B/-/merge_requests/21"
refute_output --partial "private-group/proj-D/-/merge_requests/41" # closed


GITLAB_MR_LIMIT_GROUP=private-group

run gitlab_merge_requests_search AB-123
assert_success

refute_output --partial "public-group/proj-C/-/merge_requests/31" # not in private group
refute_output --partial "public-group/proj-A/-/merge_requests/11" # not in private group
assert_output --partial "private-group/proj-B/-/merge_requests/21"
refute_output --partial "private-group/proj-D/-/merge_requests/41" # closed

unset GITLAB_MR_LIMIT_GROUP
}

@test "Searches MRs across projects to build menu" {
load "test_helper/gitlab-mock-menu.bash"

Expand Down
46 changes: 46 additions & 0 deletions test/test_helper/gitlab-mock-search.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

gitlab_request() {
local f="gitlab_request "
case "$1" in
"merge_requests?scope=all&state=all&view=simple&search=AB-123"*)
# echo "$f✔️ $1" >> gitlab-mock-search.log
echo '[
{"iid": 31,"title":"MR 31 title","web_url":"https://'${GITLAB_DOMAIN}'/public-group/proj-C/-/merge_requests/31","state":"opened", "project_id": 3},
{"iid": 11,"title":"MR 11 title","web_url":"https://'${GITLAB_DOMAIN}'/public-group/proj-A/-/merge_requests/11","state":"opened", "project_id": 1},
{"iid": 21,"title":"MR 21 title","web_url":"https://'${GITLAB_DOMAIN}'/private-group/proj-B/-/merge_requests/21","state":"opened", "project_id": 2},
{"iid": 41,"title":"MR 41 title","web_url":"https://'${GITLAB_DOMAIN}'/private-group/proj-D/-/merge_requests/41","state":"closed", "project_id": 4}
]'
;;

"groups/private-group/merge_requests?scope=all&state=all&view=simple&search=AB-123"*)
# echo "$f✔️ $1" >> gitlab-mock-search.log
echo '[
{"iid": 21,"title":"MR 21 title","web_url":"https://'${GITLAB_DOMAIN}'/private-group/proj-B/-/merge_requests/21","state":"opened", "project_id": 2},
{"iid": 41,"title":"MR 41 title","web_url":"https://'${GITLAB_DOMAIN}'/private-group/proj-D/-/merge_requests/41","state":"closed", "project_id": 4}
]'
;;

"projects?simple=true&archived=false&order_by=last_activity_at&per_page="*)
# echo "$f✔️ $1" >> gitlab-mock-search.log
echo '[
{"id": 1, "name": "Project A", "path_with_namespace": "public-group/project-a"},
{"id": 2, "name": "Project B", "path_with_namespace": "public-group/project-b"},
{"id": 3, "name": "Project C", "path_with_namespace": "private-group/project-c"},
{"id": 4, "name": "Project D", "path_with_namespace": "private-group/project-d"}
]'
;;

"projects?simple=true&membership=true&archived=false&order_by=last_activity_at&per_page="*)
# echo "$f✔️ $1" >> gitlab-mock-search.log
echo '[
{"id": 3, "name": "Project C", "path_with_namespace": "private-group/project-c"},
{"id": 4, "name": "Project D", "path_with_namespace": "private-group/project-d"}
]'
;;

*)
echo "$f$1" >> gitlab-mock-search.log
return 1
;;
esac
}

0 comments on commit c09fa24

Please sign in to comment.