Create MergeRequest when Pipeline runs for branch

I’ve created a Pipeline similar to the Blog Post from here (How to automatically create a new MR on GitLab with GitLab CI | GitLab)

It works fine and creates the MRs automatically as suggested. But all in the name of the “Bot User” for which the “Private-Token” is created. So every merged MR, produces a “Git Blame” of the “Bot User”. We do squash the commits.

If I try to use $CI_JOB_TOKEN, I got a 401-unauthorized since the token is not authorized for MR Api Endpoints. Just for API Docs | GitLab

How can I create during a running CI a new MR, in the name of the User who runs/triggers the job?

I’ve created a little gitlab-ci.yml which should create for a ‘/feature/foobar’ Branch a Merge Request

api-access-demo:
  stage: build
  image: alpine:3.6
  variables:
    GIT_STRATEGY: none
    HOST: https://gitlab.SOMEHOST.com/api/v4/projects/
    TARGET_BRANCH: "develop"
    REMOVE_SOURCE_BRANCH: "true"
    DESCRIPTION: "Funny way of life"
  only:
    refs:
      - /^feature\/*/
  script:
    - |
      apk add --no-cache \
      bash \
      curl \
      grep \
      jq

    - |
      echo "{
      \"id\": ${CI_PROJECT_ID},
      \"source_branch\": \"${CI_COMMIT_REF_NAME}\",
      \"target_branch\": \"${TARGET_BRANCH}\",
      \"remove_source_branch\": ${REMOVE_SOURCE_BRANCH},
      \"title\": \"DRAFT: ${CI_COMMIT_REF_NAME}\",
      \"assignee_id\":\"${GITLAB_USER_ID}\",
      \"description\":\""${DESCRIPTION}"\",
      \"squash\": true
      }" > ./data.json
    - cat ./data.json
    - echo ${HOST}${CI_PROJECT_ID}
    - curl -vvv -X POST "${HOST}${CI_PROJECT_ID}/merge_requests" --header "PRIVATE-TOKEN:${CI_JOB_TOKEN}" --header "Content-Type:application/json" --data-binary "@data.json"

Which results in a

{"message":"401 Unauthorized"} 

We use Gitlab 14.2 as self-hosted version.

Thanks a lot,

Tom

Hey Tom,
are you still facing this issue ?

Yes, still. But I’ve not done any further investigations. I think I will write some python script which will hold some kind of user owned access tokens.