Conditionally Disable Merge Request?

Hi.

I am trying to improve a developer workflow, where we keep forgetting to update our CHANGELOG.md file. With every update, we try to maintain an entry in our changelog, so I wanted to find a way to enforce this. I have already tried using Git hooks, or even parsing commits. Neither of those options will work for us, though.

I wanted to know if it is possible for me to somehow disable the ability to merge, if the file CHANGELOG.md is not modified? If so, how would I approach this? Would I need to set up a CI/CD pipeline (not ideal, as we already use Jenkins), or is there something akin to Github Actions, where I can simply run a script when a merge request is opened, which returns true/false?

Hi @xdega,

You are right – you need a CI/CD pipeline, which is the GitLab equivalent of GitHub Actions.

I guess there are lots of ways to do this, but I have something similar in a repo where we do this:

# .gitlab-ci.yml

stages:
    - lint

workflow:
    rules:
        - if: $CI_MERGE_REQUEST_ID
          when: never
        - if: $CI_COMMIT_TAG
          when: never
        - when: always

.lint: &lint
    stage: lint
    variables:
        PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
    before_script:
        - if [[ ! -e venv ]]; then virtualenv --python=python3 venv; fi
        - source venv/bin/activate
        - pip install python-gitlab junit-xml
    script:
        - python bin/changelog_lint.py --token=${API_TOKEN}
        - deactivate
    rules:
        - if: $SKIP_CHANGELOG_LINT
          when: never
        - when: on_success
    artifacts:
        when: always
        reports:
            junit:
                - $CI_PROJECT_DIR/changelog-lint-output.xml
        expire_in: 2 weeks
    tags:
        - lint

then in bin/changelog_lint.py we use python-gitlab to look through the MR commits, and produce a report in JUnit format which is nicely displayed in the MR overview page, like a unit test.

I set up this repo to try and verify the CHANGELOG is updated when the pipeline runs,

but I seem to be unable to get a diff that I am able to use in my bash scripts. Any idea on how I can compare the update vs master? Does this need to be in the context of a branch, or merge request?

The eventual goal is to run this on merge request, but wanted to hammer out the logic before that tedious testing.

This is how I did something similar:

  1. Get a list of open merge requests in the repo
  2. For each MR:
    1. Verify that the branch is in your own repo (not a fork)
    2. If it is, add it to a dictionary of MR IID → list of changes
  3. Figure out which MR is related to the current pipeline
  4. Get this MR’s changes from the dictionary
  5. Iterate over the changes (and in your case look for a change to the CHANGELOG file)

This is a little clunky (not least because I find the MRs with $CI_OPEN_MERGE_REQUESTS) but it does work.

1 Like