# Merge Commit Message Validator for Pull Requests

<figure><img src="https://2170165715-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M8XQUZ_SIrqvz-hF-Qt%2Fuploads%2FHEtUmz5DcYvkgnPyZlfp%2FIMG_20220915_144654.jpg?alt=media&#x26;token=766f627a-e953-420f-8a21-10f38427ba9b" alt=""><figcaption><p>Diskit,Nubra Valley,Ladakh</p></figcaption></figure>

This repository provides a Forge app for validating merge commit messages in Bitbucket pull requests. It ensures that pull requests targeting specific branches meet configurable standards for commit message patterns and merge strategies.

***

### Features

* **Pull Request Validation**:
  * Validates pull requests targeting specific branches (`main`, `master`, `boost_1.80.0`).
  * Ensures the merge strategy is set to `squash`.
  * Checks the merge commit message against a configurable regex pattern.
* **Configurable Regex**:
  * Admins can update the regex pattern used for validating merge commit messages via a user-friendly UI.

***

### Files Overview

#### **`src/merge-checks/index.ts`**

This file contains the backend logic for validating pull requests. Key functionalities include:

1. **Fetching Pull Request Details**:
   * Retrieves the destination branch and checks if it's one of the allowed branches (`main`, `master`, `boost_1.80.0`).
2. **Validation Logic**:
   * Ensures the merge strategy is set to `squash`.
   * Validates the merge commit message against a regex pattern retrieved from Forge storage.
3. **Dynamic Configuration**:
   * The regex pattern for commit messages is fetched from Forge storage, allowing it to be updated without code changes.
4. **Error and Success Handling**:
   * Logs and returns appropriate responses based on validation results.

#### **`frontend/TitleSubstringForm.tsx`**

This file provides the frontend interface for configuring the regex pattern. It is built using Atlassian's UI Kit 2.

**Key Features:**

* A **form** to set the merge commit message regex pattern.
* Validates user input to ensure the field is not empty.
* Displays success messages when changes are applied.
* Automatically saves the regex pattern to Forge storage via an `invoke` resolver.

***

### Installation and Setup

1. **Clone the repository**:

   ```bash
   git clone <repository-url>
   cd <repository-name>
   ```
2. **Install Dependencies**: Make sure you have the Forge CLI installed. Then install dependencies:

   ```bash
   npm install
   ```
3. **Deploy the Forge App**: Deploy the app to your Atlassian account.

   ```bash
   forge deploy
   forge install
   ```
4. **Run the App**: To test the app locally, use:

   ```bash
   forge tunnel
   ```

***

### Usage

#### 1. **Setting Up Commit Message Regex**

Navigate to the app's configuration UI to set a custom regex for validating merge commit messages. For example:

* **Regex**: `^JIRA-\d+: .*`\
  Ensures commit messages start with a JIRA ticket ID.

#### 2. **Merge Request Validation**

When a pull request is created or updated, the app validates:

* The **destination branch** (must be `main`, `master`, or `boost_1.80.0`).
* The **merge strategy** (must be `squash`).
* The **commit message** (must match the configured regex).

***

### Example Scenarios

* **Successful Validation**:
  * A pull request targets `main`.
  * The merge strategy is `squash`.
  * The commit message matches the regex.
* **Failure Cases**:
  * The destination branch is not allowed.
  * The merge strategy is not `squash`.
  * The commit message does not match the regex.

***

### Contribution Guidelines

1. Fork the repository.
2. Make your changes in a new branch.
3. Submit a pull request with a detailed description.
4. Git Repo <https://github.com/ttn-aditya/merge-commit-regex-checker-bitbucket-cloud>
