How to integrate Code Beamer Ticket ID and its status with GitLab?

Whenever developer commits a code to GitLab, it should validate with the Ticket ID of the codebeamer and check the ticket status (open,close,inprogress). Gitlab should allow the push operation based on the Ticket status of the CodeBeamer. Push operation has to be allowed only for the OPEN ticket status Hence, how to interlink the Ticket ID status of the codebeamer with GitLab also validate status of it

To implement a mechanism where GitLab validates a commit with a CodeBeamer Ticket ID and allows the push operation only if the ticket status is OPEN, you need to create a GitLab pre-receive hook or use a custom CI/CD pipeline with a script that validates the ticket status using CodeBeamer’s API. Here’s how to achieve this:


Steps to Interlink CodeBeamer and GitLab

1. Enable CodeBeamer API Access

  • Confirm that CodeBeamer’s REST API is enabled for your instance.
  • Obtain the API documentation for querying ticket details.
  • Generate an API token or credentials for authentication with CodeBeamer.

2. Configure a GitLab Pre-Receive Hook

A pre-receive hook is a server-side Git hook that executes before Git accepts the push operation. You can configure it to validate the commit message for a Ticket ID and query CodeBeamer for its status.

a. Example Hook Logic
  1. Extract the Ticket ID from the commit message.
  2. Query the CodeBeamer API for the ticket status.
  3. Reject the push if the status is not OPEN.
b. Example Pre-Receive Hook Script

Save this script as a pre-receive hook on the GitLab repository server.

#!/bin/bash

# Set your CodeBeamer API URL and credentials
CODEBEAMER_API_URL="https://codebeamer.example.com/api/v3"
CODEBEAMER_USERNAME="your_username"
CODEBEAMER_TOKEN="your_api_token"

# Loop through the commits in the push
while read oldrev newrev refname; do
  # Get commit messages from pushed commits
  for commit in $(git rev-list $oldrev..$newrev); do
    commit_message=$(git log -1 --pretty=%B $commit)

    # Extract the Ticket ID (assuming a format like CB-1234)
    TICKET_ID=$(echo "$commit_message" | grep -oE "CB-[0-9]+")

    if [ -z "$TICKET_ID" ]; then
      echo "ERROR: No Ticket ID (e.g., CB-1234) found in commit message."
      exit 1
    fi

    # Query the CodeBeamer API for ticket status
    response=$(curl -s -u "$CODEBEAMER_USERNAME:$CODEBEAMER_TOKEN" \
      "$CODEBEAMER_API_URL/issues/$TICKET_ID")

    # Parse the status (assuming status is a field in the JSON response)
    status=$(echo "$response" | jq -r '.status')

    if [ "$status" != "OPEN" ]; then
      echo "ERROR: Ticket $TICKET_ID has status '$status'. Only OPEN tickets are allowed."
      exit 1
    fi
  done
done

# If all checks pass, allow the push
exit 0
  • Prerequisites:
    • Ensure jq is installed for JSON parsing.
    • Replace the API URL, username, and token with valid values for your CodeBeamer instance.
c. Deploy the Hook
  • Place the pre-receive script in the repository’s hooks directory on the GitLab server.
  • Ensure it has executable permissions:
chmod +x pre-receive

3. Alternative: Use GitLab CI/CD Pipeline

If you don’t have access to GitLab’s server to configure hooks, you can enforce validation in a CI pipeline that fails builds when the ticket status is invalid.

a. Example .gitlab-ci.yml
stages:
  - validate

validate_ticket:
  stage: validate
  image: curlimages/curl:latest
  script:
    - echo "Checking Ticket ID in commit messages..."
    - TICKET_ID=$(git log -1 --pretty=%B | grep -oE "CB-[0-9]+")
    - if [ -z "$TICKET_ID" ]; then
        echo "ERROR: No Ticket ID (e.g., CB-1234) found in commit message.";
        exit 1;
      fi
    - echo "Found Ticket ID: $TICKET_ID"
    - response=$(curl -s -u "$CODEBEAMER_USERNAME:$CODEBEAMER_TOKEN" "$CODEBEAMER_API_URL/issues/$TICKET_ID")
    - status=$(echo "$response" | jq -r '.status')
    - if [ "$status" != "OPEN" ]; then
        echo "ERROR: Ticket $TICKET_ID has status '$status'. Only OPEN tickets are allowed.";
        exit 1;
      fi
  • Pipeline Behavior:
    • Extracts the ticket ID from the latest commit.
    • Queries CodeBeamer’s API to fetch the status.
    • Fails the job if the ticket status is not OPEN.
  • Limitations: This approach won’t block the push but will fail the pipeline. You can enforce mandatory pipeline success before merging to protected branches.

4. Add Ticket ID Validation in Commit Message

Ensure developers include the Ticket ID in commit messages. You can:

  1. Define a commit message template in Git.
  2. Use a client-side pre-commit hook to validate locally.

Example Pre-Commit Hook for Developers:

#!/bin/bash

# Validate commit message for a valid Ticket ID
TICKET_ID=$(git log -1 --pretty=%B | grep -oE "CB-[0-9]+")

if [ -z "$TICKET_ID" ]; then
  echo "ERROR: No Ticket ID (e.g., CB-1234) found in commit message."
  exit 1
fi

Place this script in .git/hooks/pre-commit and make it executable:

chmod +x .git/hooks/pre-commit

5. Enforce Policies

  • Use GitLab branch protection to require:
    • Pre-receive hooks.
    • Successful CI pipelines.

Summary Workflow

  1. Developer commits code with a ticket ID (e.g., CB-1234) in the message.
  2. GitLab pre-receive hook or CI/CD pipeline:
  • Extracts the ticket ID.
  • Queries CodeBeamer’s API.
  • Checks the ticket status.
  • Rejects the push or fails the pipeline if the ticket is not OPEN.
  1. Push operation is allowed only for OPEN tickets.

Let me know if you need help with setup or debugging!