Commit dc443064 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Adding support to detect conflicts

parent 91e4ce29
Loading
Loading
Loading
Loading
Loading

checking_conflicts.sh

0 → 100644
+102 −0
Original line number Original line Diff line number Diff line
#!/bin/bash

#Parameters

#${CI_API_V4_URL} -> 1
echo "CI_API_V4_URL:" $1
#${CI_MERGE_REQUEST_PROJECT_ID} -> 2
echo "CI_MERGE_REQUEST_PROJECT_ID:" $2
#${CI_MERGE_REQUEST_MILESTONE} -> 3
echo "CI_MERGE_REQUEST_MILESTONE:" $3
#${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} -> 4
echo "CI_MERGE_REQUEST_TARGET_BRANCH_NAME:" $4
#${CI_MERGE_REQUEST_IID} -> 5
echo "CI_MERGE_REQUEST_IID:" $5
#${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} -> 6
echo "CI_MERGE_REQUEST_SOURCE_BRANCH_NAME:" $6
#${CI_PROJECT_NAME} -> 7
echo "CI_PROJECT_NAME:" $7
#"$GITLAB_USER_NAME" -> 8
echo "GITLAB_USER_NAME:" $8
#$GITLAB_USER_EMAIL -> 9
echo "GITLAB_USER_EMAIL:" $9

#echo "------ Removing previous outputs --------"
#rm **/*.docx
#rm out/*.md

#echo "------ Parsing repo URL --------"

#HOST_URL=$(echo $1 | cut -d'/' -f 1-3)
#PROJECT_NAME=$(echo $1 | cut -d'/' -f 6- | cut -d'.' -f 1)
#echo "HOST URL:" $HOST_URL
#echo "PROJECT NAME:" $PROJECT_NAME
#echo "PROJECT ID:" $2
#echo "MERGE IID:" $3

#echo "\n------ Install necessary packages --------"
#pip install -q mkdocs-material mike rich mkdocs-rss-plugin

echo "------ Config git and get the previous GitLab Pages content --------"
#git clone "https://oauth2:${10}@git.onem2m.org/reinaortega/${7}.git" ${7}
cd ${7}
git config --global --replace-all user.name "$8"
git config --global --replace-all user.email $9
#git fetch origin $PAGES_BRANCH && git checkout $PAGES_BRANCH || git checkout -b $PAGES_BRANCH origin/$PAGES_BRANCH || echo "Pages branch not deployed yet."
#git checkout $7
echo "Checkout merge request source branch ${6} ..."
git checkout -b "${6}" "origin/${6}" #Git checkout other merge requests SOURCE BRANCHES
git status

echo "\n------ Getting all potential conflicting merge requests --------"
# Get a list of projects in the group using GitLab API
#PROJECT_IDS=$(curl --header "PRIVATE-TOKEN: $1" "$GITLAB_HOST/api/v4/groups/$SPECIFICATIONS_GROUP/projects" | grep -oP '"id":(?!'$SPECIFICATIONS_GROUP')\d+' | cut -d':' -f2)

#curl --header "PRIVATE-TOKEN: ${10}" "${1}/projects/${2}/merge_requests?state=opened&milestone=${3}&target_branch=${4}" |jq -c '.[] | {iid: .iid, source_branch: .source_branch, title: .title}'
#curl --header "PRIVATE-TOKEN: ${10}" "${1}/projects/${2}/merge_requests?state=opened&milestone=${3}&target_branch=${4}" | jq -c '.[]' | while read mr; do
merge_requests=$(curl --header "PRIVATE-TOKEN: ${10}" "${1}/projects/${2}/merge_requests?state=opened&milestone=${3}&target_branch=${4}")
echo "${merge_requests}" | jq -c '.[]' | while read mr; do
  mr_id=$(echo "$mr" | jq '.iid')
  result_id=$?
  if [ ! $result_id == 0 ] ; then
    echo "ERROR: When retrieving merge request id"
    continue
  fi
  mr_title=$(echo "$mr" | jq '.title')
  result_title=$?
  if [ ! $result_title == 0 ] ; then
    echo "ERROR: When retrieving merge request title"
    continue
  fi
  mr_source_branch=$(echo "$mr" | jq -r '.source_branch')
  result_source_branch=$?
  if [ ! $result_source_branch == 0 ] ; then
    echo "ERROR: When retrieving merge request source branch"
    continue
  fi
  if [ ${mr_id} != ${5} ]; then
    echo "Checkout potential merge request source branch ${mr_source_branch} ..."
    git checkout -b "${mr_source_branch}" "origin/${mr_source_branch}" #Git checkout other merge requests SOURCE BRANCHES
    echo "Run dry merge from potential merge request source branch ${mr_source_branch} into actual merge request source branch ${6} ..."
    git merge --no-commit --no-ff "${6}" #SOURCE BRANCH of the merge request
		result=$?
		if [ ! $result == 0 ] ; then
      echo "Merge request title: ${mr_title}, Merge Request ID: ${mr_id}" >> conflicting_merge_requests.txt
    else
      echo "No conflict with Merge request title: ${mr_title}, Merge Request ID: ${mr_id}"
    fi
    git merge --abort
  fi

done

if [ -f "conflicting_merge_requests.txt" ]; then
  echo "List of conflicting merge requests:"
  cat conflicting_merge_requests.txt
  mv conflicting_merge_requests.txt ../conflicting_merge_requests.txt
  exit 1
else
  exit 0
fi

exit 0
+31 −2
Original line number Original line Diff line number Diff line
@@ -16,15 +16,42 @@ workflow:
variables:
variables:


stages:
stages:
  - checking
  - generation
  - generation
  - publication
  - publication
  - web
  - web


Checking conflicts:
  stage: checking
  rules:
    - if: $CI_MERGE_REQUEST_TITLE !~ /v.*_baseline$/ && $CI_PIPELINE_SOURCE == "merge_request_event"
      when: always
  image: python:3.9.18-slim-bullseye
  tags:
    - docker
  before_script:
    # Installation of required software
    - apt-get update -qq && apt-get -qq install -y git curl jq > /dev/null
    - |
     curl "${CI_API_V4_URL}/projects/$TOOLS_SCRIPTS_PROJECT_ID/repository/files/checking_conflicts%2Esh/raw?ref=conflicts" >> checking_conflicts.sh
    - chmod +x checking_conflicts.sh
    - git clone "https://oauth2:$PIPELINE_ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git" ${CI_PROJECT_NAME}
  script:
    - echo 'Checking conflicts'
    - ./checking_conflicts.sh ${CI_API_V4_URL} ${CI_MERGE_REQUEST_PROJECT_ID} ${CI_MERGE_REQUEST_MILESTONE} ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} ${CI_MERGE_REQUEST_IID} ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME} ${CI_PROJECT_NAME} "$GITLAB_USER_NAME" $GITLAB_USER_EMAIL $PIPELINE_ACCESS_TOKEN
  artifacts:
    when: on_failure
    paths:
      - conflicting_merge_requests.txt
    expose_as: 'Checking conflicts log'

Word CR:
Word CR:
  stage: generation
  stage: generation
  rules:
  rules:
    - if: $CI_MERGE_REQUEST_TITLE !~ /v.*_baseline$/ && $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_MERGE_REQUEST_TITLE !~ /v.*_baseline$/ && $CI_PIPELINE_SOURCE == "merge_request_event"
      when: always
      when: on_success
  dependencies: []
  when: on_success
  before_script:
  before_script:
    - |
    - |
     curl "${CI_API_V4_URL}/projects/$TOOLS_SCRIPTS_PROJECT_ID/repository/files/generate_changemarks%2Esh/raw?ref=master" >> generate_changemarks.sh
     curl "${CI_API_V4_URL}/projects/$TOOLS_SCRIPTS_PROJECT_ID/repository/files/generate_changemarks%2Esh/raw?ref=master" >> generate_changemarks.sh
@@ -50,7 +77,9 @@ Baseline contribution:
  stage: generation
  stage: generation
  rules:
  rules:
    - if: $CI_MERGE_REQUEST_TITLE =~ /v.*_baseline$/ && $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_MERGE_REQUEST_TITLE =~ /v.*_baseline$/ && $CI_PIPELINE_SOURCE == "merge_request_event"
      when: always
      when: on_success
  dependencies: []
  when: on_success
  before_script:
  before_script:
    - |
    - |
     curl "${CI_API_V4_URL}/projects/$TOOLS_SCRIPTS_PROJECT_ID/repository/files/publish_spec%2Esh/raw?ref=master" >> publish_spec.sh
     curl "${CI_API_V4_URL}/projects/$TOOLS_SCRIPTS_PROJECT_ID/repository/files/publish_spec%2Esh/raw?ref=master" >> publish_spec.sh