Skip to content

Instantly share code, notes, and snippets.

@daxmc99
Created November 5, 2024 20:10
Show Gist options
  • Save daxmc99/63200b31352c92e4af12bae15f02e01c to your computer and use it in GitHub Desktop.
Save daxmc99/63200b31352c92e4af12bae15f02e01c to your computer and use it in GitHub Desktop.
Add all eligible reviewers to Gitlab MR
#!/usr/bin/env bash
set -e
# Ensure required tools are installed
if ! command -v glab &> /dev/null; then
echo "Error: glab CLI is not installed. Please install it before running this script."
exit 1
fi
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install it before running this script."
exit 1
fi
# Get the MR details in JSON format
MR_JSON=$(glab mr view -F json)
# Extract the numeric MR ID
MR_ID_NUM=$(echo "$MR_JSON" | jq '.id')
if [ -z "$MR_ID_NUM" ] || [ "$MR_ID_NUM" = "null" ]; then
echo "Could not extract MR ID from glab mr view output."
exit 1
fi
# Construct the global MR ID for GraphQL API
MR_ID="gid://gitlab/MergeRequest/$MR_ID_NUM"
# Extract project path and MR IID
PROJECT_PATH=$(echo "$MR_JSON" | jq -r '.references.full' | sed 's/!.*$//')
MR_IID=$(echo "$MR_JSON" | jq -r '.iid')
if [ -z "$PROJECT_PATH" ] || [ -z "$MR_IID" ]; then
echo "Could not extract project path or MR IID."
exit 1
fi
# GraphQL query to get eligible approvers and current reviewers
QUERY=$(cat <<EOF
query {
mergeRequest(id: "$MR_ID") {
approvalState {
rules {
approvalsRequired
eligibleApprovers {
name
username
}
}
}
reviewers {
nodes {
username
}
}
}
}
EOF
)
# Get the list of eligible approvers
RESPONSE=$(glab api graphql -f query="$QUERY")
if echo "$RESPONSE" | jq -e '.errors' >/dev/null; then
echo "Error fetching merge request data:"
echo "$RESPONSE" | jq -r '.errors[] | .message'
exit 1
fi
REVIEWERS=$(echo "$RESPONSE" | jq -r '.data.mergeRequest.approvalState.rules[].eligibleApprovers[]?.username' | sort -u)
if [ -z "$REVIEWERS" ]; then
echo "No eligible reviewers found."
exit 1
fi
# Convert reviewers to GraphQL array format
REVIEWERS_ARRAY=$(printf '"%s",' $REVIEWERS)
REVIEWERS_ARRAY="[${REVIEWERS_ARRAY%,}]"
# Mutation to set reviewers with operationMode: APPEND
MUTATION=$(cat <<EOF
mutation {
mergeRequestSetReviewers(
input: {
projectPath: "$PROJECT_PATH",
iid: "$MR_IID",
reviewerUsernames: $REVIEWERS_ARRAY,
operationMode: APPEND
}
) {
errors
mergeRequest {
id
title
approvalsRequired
reviewers {
nodes {
name
username
}
}
}
}
}
EOF
)
# Run the mutation
MUTATION_RESPONSE=$(glab api graphql -f query="$MUTATION")
if echo "$MUTATION_RESPONSE" | jq -e '.errors' >/dev/null; then
echo "Error setting reviewers:"
echo "$MUTATION_RESPONSE" | jq -r '.errors[] | .message'
exit 1
fi
# Check for errors in the mutation response
ERROR_COUNT=$(echo "$MUTATION_RESPONSE" | jq '.data.mergeRequestSetReviewers.errors | length')
if [ "$ERROR_COUNT" -gt 0 ]; then
echo "Errors encountered:"
echo "$MUTATION_RESPONSE" | jq -r '.data.mergeRequestSetReviewers.errors[]'
exit 1
else
echo "Reviewers updated successfully."
echo "Current reviewers:"
echo "$MUTATION_RESPONSE" | jq -r '.data.mergeRequestSetReviewers.mergeRequest.reviewers.nodes[] | "Reviewer: \(.name) (\(.username))"'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment