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

Merge branch 'master' into 'master'

Replacing Jenkins wtih Gitlab CI/CD for SA5 and 5G_APIs

See merge request !8
parents 8a9843bb 09244db2
Loading
Loading
Loading
Loading

all/.gitlab-ci.yml

0 → 100644
+76 −0
Original line number Diff line number Diff line
# CI/CD:
#
# VALIDATION triggered by:
#  - any (branch commit)
#  
# GENERATION triggered by:
#  - any (branch commit) for Generate config
#  - merge request for CR contribution
#   

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
      when: never
    - if: $CI_COMMIT_BRANCH
    
variables:
  LOGFILE_OPENAPI: "openapi-validation.log"
  GIT_CLEAN_FLAGS: -ffdxq

stages:
  - validation
  - generation
        
Validate OpenAPI:
  stage: validation
  before_script:
    - |
     curl "${CI_API_V4_URL}/projects/57/repository/files/all%2Fvalidate-all-in-docker%2Esh/raw?ref=master" >> validate-all-in-docker.sh
    - chmod +x validate-all-in-docker.sh
  script:
    - echo 'Validate OpenAPI'
    - docker run --rm -v $(pwd):"/specs" etsiforge/swagger-cli:4.0.3 /bin/sh -c "sh specs/validate-all-in-docker.sh"
  artifacts:
    name: "$CI_JOB_NAME"
    paths:
      - ${LOGFILE_OPENAPI}
    
Generate config:
  stage: generation
  when: always
  before_script:
    - |
     curl "${CI_API_V4_URL}/projects/57/repository/files/all%2Fgenerate-config%2Esh/raw?ref=master" >> generate-config.sh
    - chmod +x generate-config.sh
  script:
    - echo 'Generate config'
    - ./generate-config.sh $
  artifacts:
    name: "$CI_JOB_NAME"
    paths:
      - api-versions.txt
      
CR contribution:
  stage: generation
  when: on_success
  only:
    - merge_requests
  before_script:
    - |
     curl "${CI_API_V4_URL}/projects/57/repository/files/all%2Fgenerate_changemarks%2Esh/raw?ref=master" >> generate_changemarks.sh
    - chmod +x generate_changemarks.sh
    # - |
     # curl "${CI_API_V4_URL}/projects/57/repository/files/all%2Fall_delimiter_start%2Edocx/raw?ref=master" >> all_delimiter_start.docx
    # - |
     # curl "${CI_API_V4_URL}/projects/57/repository/files/all%2Fall_delimiter_end%2Edocx/raw?ref=master" >> all_delimiter_end.docx
  script:
    - echo 'CR contribution'
    - mkdir docs
    - ./generate_changemarks.sh ${CI_PROJECT_URL} ${CI_MERGE_REQUEST_PROJECT_ID} ${CI_MERGE_REQUEST_IID}
    #- ./generate_changemarks.sh ${CI_PROJECT_URL} ${CI_MERGE_REQUEST_PROJECT_ID} ${CI_MERGE_REQUEST_IID} all_delimiter_start.docx all_delimiter_end.docx
  artifacts:
    paths:
      - docs/
    expose_as: 'Word document contribution'
 No newline at end of file

all/generate-config.sh

0 → 100644
+58 −0
Original line number Diff line number Diff line
#!/bin/bash
# Copyright ETSI 2021
# See: https://forge.etsi.org/etsi-forge-copyright-statement.txt
# If there are no YAML file, simply exit
ls | grep -q yaml
found_yaml=$?
if [ ! $found_yaml ] ; then 
    echo "-- No YAML files."
    exit 0
fi

echo "------ Installing necessary node modules -----"

#npm install js-yaml
#npm install json

echo "------ Generating CONFIG file from all YAML files ------"
fres=0

for i in *.yaml ;  do
	echo "[$i]"
	echo "[$i]" | grep -Po "TS[\w-]+\.yaml" | xargs -I % sh -c 'echo [%]' >> api-versions.txt ;
    # node ./node_modules/js-yaml/bin/js-yaml $i > "$i.json" ;
    # cat "$i.json" | node ./node_modules/json/lib/json info.version >> api-versions.txt

    # input=$(cat "$i.json" | node ./node_modules/json/lib/json externalDocs.description)
    # pattern="[0-9]+[.][0-9]+[.][0-9]+"
    # if echo $input | grep -qPo $pattern
    # then
      # echo $input | grep -Po $pattern >> api-versions.txt
    # else
      # echo "-" >> api-versions.txt
    # fi

    # # cat "$i.json" | node ./scripts/node_modules/json/lib/json externalDocs.description | grep -Po "[0-9]+[.][0-9]+[.][0-9]+" >> api-versions.txt

    # cat "$i.json" | node ./node_modules/json/lib/json info.description | grep -Po "[0-9]{4}" >> api-versions.txt
	
	#node ./node_modules/js-yaml/bin/js-yaml $i|node ./node_modules/json/lib/json  info.version externalDocs.description info.description |grep -Po "[0-9]+[.][0-9]+[.][0-9]+|[0-9]{4}" >>api-versions.txt
    FILE="${i}"
	INFO_VERSION=`sed -n '/^info:/,/^[^[:space:]]/{/^  version:/p;/^[^([:space:]|info:)]/q}' ${FILE} | grep -Po '([0-9]+\.){2}[0-9]+'` #>> api-versions.txt #Info version
	if [ "$INFO_VERSION" == "" ]; then
		echo "-" >>api-versions.txt
	else
		echo $INFO_VERSION >> api-versions.txt
	fi
	EXT_DOC_VERSION=`sed -n '/^externalDocs:/,/^[^[:space:]]/{/^  description:/,/^[^[:space:]]/p;/^[^([:space:]|externalDocs:)]/q}' ${FILE} | grep -Po '([0-9]+\.){2}[0-9]+'` # >> api-versions.txt # EXT_DOC_VERSION
	if [ "$EXT_DOC_VERSION" == "" ]; then
		echo "-" >>api-versions.txt
	else
		echo $EXT_DOC_VERSION >> api-versions.txt
	fi
	sed -n '/^info:/,/^[^[:space:]]/{/^  description:/,/^[^[:space:]]/p;/^[^([:space:]|info:)]/q}' ${FILE} | grep -Po '[0-9]{4}' >> api-versions.txt
	
	res=$?
    fres=$(($fres||$res))
done
exit $fres
+25 −0
Original line number Diff line number Diff line
#!/bin/bash

DOCKER_IMAGE=forge.3gpp.org:5050/tools/3gpp-scripts/forgelib:v2.0.0

echo "\n------ Checking for docker image --------"
docker pull "$DOCKER_IMAGE"

echo "------ Removing previous outputs --------"
rm **/*.docx

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

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

echo "\n------ Generating change marks --------"
docker container run --rm -v $(pwd):/tmp/ "$DOCKER_IMAGE" forgelib-changedocs -vv --outPath=/tmp/docs "$HOST_URL" "$2" "$PROJECT_NAME" "$3"
#docker container run --rm -v $(pwd):/tmp/ "$DOCKER_IMAGE" forgelib-changedocs -vv --combine --outPath=/tmp/docs --startdelimiter "/tmp/$4" --enddelimiter "/tmp/$5" "$HOST_URL" "$2" "$PROJECT_NAME" "$3"

exit 0
+52 −0
Original line number Diff line number Diff line
#!/bin/sh
# Copyright ETSI 2019-2021
# See: https://forge.etsi.org/etsi-forge-copyright-statement.txt
LOGFILE=openapi-validation.log
echo -e "\n------ Validating all YAML files (may takes several minutes) ------"
# If there are no YAML file, simply exit
ls | grep -q yaml
found_yaml=$?
if [ ! $found_yaml ]
then
    echo "-- No YAML files."
    exit 0
fi
fres=0
LOG=""
ERR=""
for i in specs/*.yaml
do
    #echo -e "----  Validating $i ...  "
	MSG=$(swagger-cli validate "$i" 2>&1)
    #MSG=$(validate "$i" 2>&1)
	echo "$MSG"
    res=$?
    if [ ! $res == 0 ] ; then 
        ERR="$ERR$MSG\n"
    fi 
    LOG="$LOG$MSG\n"
    fres=$(($fres||$res))
done
echo -e "\n-- Final validator returns $fres." | tee -a $LOGFILE
if [ $fres == 0 ]
then
   echo "No errors found, all files validate the OpenAPI definition. Below the complete log." | tee -a $LOGFILE
else
   echo "Some errors found, see below the details." | tee -a $LOGFILE
fi
if [ ! "$ERR" == "" ]
then
    echo -e "\n---- Errors detected ----" | tee -a $LOGFILE
    echo -e $ERR  | tee -a $LOGFILE
fi

echo -e "\n---- Complete log of validation ----" >> $LOGFILE
echo -e $LOG >> $LOGFILE

# echo -e "\n---- Complete log of validation ----" | tee -a $LOGFILE
# echo -e $LOG | tee -a $LOGFILE
#echo -e "\n------ Content of the folder ------" | tee -a $LOGFILE
#ls | tee -a $LOGFILE
mv $LOGFILE specs/
# Exit code needed for jenkins to know the verdict of the build
exit $fres
 No newline at end of file

all/validate-all.sh

0 → 100644
+85 −0
Original line number Diff line number Diff line
#!/bin/bash
# Copyright ETSI 2019-2021
# See: https://forge.etsi.org/etsi-forge-copyright-statement.txt

LOGFILE=openapi-validation.log
DOCKER_IMAGE=etsiforge/swagger-cli:4.0.3

echo -e "\n------ Switching to $1 folder --------"
cd $1

echo "------ Checking for previous logs ------"
if [ -f "$LOGFILE" ] ; then
    echo "Found previous log. Removing it"
    rm "$LOGFILE"
fi

if [ -x "$(command -v docker)" ] ; then
    RUN_IN_DOCKER=0
    echo "Using dockerized validator ($DOCKER_IMAGE)"
    docker pull "$DOCKER_IMAGE"
	#docker pull -q "$DOCKER_IMAGE" --> Pull quietly only from Ubuntu 20.04
else
    RUN_IN_DOCKER=1
    if [ ! -x "$(command -v swagger-cli)" ] ; then
        echo "Validator swagger-cli not found. Quitting."
        exit 1
    else
        echo "Using local validator ($(which swagger-cli))"
    fi
fi

function validate {
    if [ $RUN_IN_DOCKER == 0 ] ; then
	   bname=$(basename "$1")
       docker run --rm -v $(dirname $(realpath $1)):"/specs" $DOCKER_IMAGE swagger-cli validate "/specs/$bname"
    else
        swagger-cli validate "$1"
    fi
}

echo -e "\n------ Validating all YAML files (may takes several minutes) ------"
# If there are no YAML file, simply exit
ls | grep -q yaml
found_yaml=$?
if [ ! $found_yaml ] ; then 
    echo "-- No YAML files."
    exit 0
fi

fres=0
LOG=""
ERR=""
for i in *.yaml ;  do
    echo -e "----  Validating $i ...  "    
    MSG=$(validate "$i" 2>&1)
	echo "$MSG"
    res=$?
    if [ ! $res == 0 ] ; then 
        ERR="$ERR$MSG\n"
    fi 
    LOG="$LOG$MSG\n"
    fres=$(($fres||$res))
done

echo -e "\n-- Final validator returns $fres." | tee -a $LOGFILE
if [ $fres == 0 ] ; then
   echo "No errors found, all files validate the OpenAPI definition. Below the complete log." | tee -a $LOGFILE
else
   echo "Some errors found, see below the details." | tee -a $LOGFILE
fi

if [ ! "$ERR" == "" ] ; then 
    echo -e "\n---- Errors detected ----" | tee -a $LOGFILE
    echo -e $ERR  | tee -a $LOGFILE
fi

echo -e "\n---- Complete log of validation ----" | tee -a $LOGFILE
echo -e $LOG | tee -a $LOGFILE

echo -e "\n------ Content of the folder ------" | tee -a $LOGFILE
ls | tee -a $LOGFILE

# Exit code needed for jenkins to know the verdict of the build

exit $fres
Loading