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

Generate automatically openapi files for uploaded document

parent 768ca75d
Loading
Loading
Loading
Loading
+100 −0
Original line number Diff line number Diff line
# CI/CD:
#
# BUILD triggered by:
#  - any (branch commit)
#
# VALIDATION triggered by:
#  - any (branch commit)
#  
# GENERATION triggered by:
#  - any (branch commit) for Generate config
#  - merge request for CR contribution
#    
# OPENAPI
#
#  - 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
  #GIT_SSL_NO_VERIFY: "true"

stages:
  - validation
  - generation
  - openapi

Validate OpenAPI:
  stage: validation
  before_script:
    - |
     curl --header "PRIVATE-TOKEN: $PIPELINE_PROJECT_TOKEN" "${CI_API_V4_URL}/projects/694/repository/files/common-openapi%2Fcli-validate-openapis-mec%2Esh/raw?ref=autogeneration" >> cli-validate-openapis-mec.sh
    - chmod +x cli-validate-openapis-mec.sh
  script:
    - echo 'Validate OpenAPI'
    - ./cli-validate-openapis-mec.sh .
  artifacts:
    name: "$CI_JOB_NAME"
    when: always
    paths:
      - ${LOGFILE_OPENAPI}
    
CR contribution:
  stage: generation
  when: on_success
  only:
    - merge_requests
  before_script:
    - |
     curl --header "PRIVATE-TOKEN: $PIPELINE_PROJECT_TOKEN" "${CI_API_V4_URL}/projects/694/repository/files/common-openapi%2Fgenerate_changemarks%2Esh/raw?ref=autogeneration" >> 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'

Generation of OpenAPI:
  stage: openapi
  before_script:
    - |
     curl --header "PRIVATE-TOKEN: $PIPELINE_PROJECT_TOKEN" "${CI_API_V4_URL}/projects/694/repository/files/common-openapi%2FgenerateOpenapi%2Esh/raw?ref=autogeneration" >> generateOpenapi.sh
    - chmod +x generateOpenapi.sh
    - git clone "https://oauth2:$PIPELINE_ACCESS_TOKEN@$CI_SERVER_HOST/rep/$CI_PROJECT_PATH.git"
  script: 
    - echo 'Generate OpenAPI'
    - ./generateOpenapi.sh .
  after_script:
    - echo $CI_COMMIT_BRANCH
    - echo $CI_PROJECT_PATH
    - echo $CI_SERVER_HOST
    - echo $(pwd)
    - cd gs012-rnis-api
    - |
     git push --push-option="ci.skip" "https://gitlab-ci-token:$PIPELINE_ACCESS_TOKEN@$CI_SERVER_HOST/rep/$CI_PROJECT_PATH.git"
    - |
     git push --push-option="ci.skip" --tags "https://gitlab-ci-token:$PIPELINE_ACCESS_TOKEN@$CI_SERVER_HOST/rep/$CI_PROJECT_PATH.git" 
    #- |
    # git remote add origin "https://gitlab-ci-token:glpat-y3Uu8UsLTzXsHFyPcoH6@$CI_SERVER_HOST/rep/$CI_PROJECT_PATH.git"
    #- |
    #git push --push-option="ci.skip" origin HEAD:$($CI_COMMIT_BRANCH)
  rules:
    - changes:
        - doc/*.docx
+94 −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
DOCKER_IMAGE_LINT=wework/speccy:0.8
DOCKER_IMAGE_REDOC=redocly/openapi-cli:v1.0.0-beta.93

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_REDOC)"
    docker pull "$DOCKER_IMAGE_REDOC"
	#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 {
    echo -e "----  Validating $i:  "    
    if [ $RUN_IN_DOCKER == 0 ] ; then
	   bname=$(basename "$1")
       #docker run -v "$(pwd)":"/specs" $DOCKER_IMAGE swagger-cli validate "/specs/$bname"
	   #docker run -v "$(pwd)":"/specs" $DOCKER_IMAGE_LINT lint "/specs/$bname"
       docker run -v "$(pwd)":"/specs" $DOCKER_IMAGE_REDOC lint "/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=""

#specfiles command used if we want to exclude definitions file from MEC-10.2 for instance
#specfiles=$(ls -I*definitions.*| egrep "^[^.]*.(json|yaml)")
# for i in $specfiles ; do

for i in ./*.json ./*.yaml ;  do
    MSG=$(validate "$i" 2>&1)
    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
+62 −0
Original line number Diff line number Diff line
#!/bin/bash

DOC2OAS_DOCKER_IMAGE=doc2oas-ie-sample:master

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

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 "TAG NAME:" $2

echo "------ Getting spec and config files--------"
# If there is no .docx file or more than one, then simply exit
cd doc

spec=$(ls | grep 'docx')
if [ -z $spec ] ; then 
    echo "-- No spec file"
    exit 0
#Check if there are more than one
fi

config="config.yaml"

echo "\n------ Preparaing spec --------"
echo -e "import sys" | tee -a "command.py"
echo -e "\nsys.path.append(\"/app\")" | tee -a "command.py"
echo -e "\nimport main" | tee -a "command.py"
echo -e "\nif __name__ == '__main__':" | tee -a "command.py"
echo -e "\n    sys.exit(main.maindoc2oas(\"/app/tmp/$spec\", \"/app/tmp/$config\"))" | tee -a "command.py"
#docker run --rm -v $(pwd):/app/tmp $DOC2OAS_DOCKER_IMAGE "-c 'import main; main.maindoc2oas("/app/doc/$spec", "/app/doc/$config")'"
docker run --rm -v $(pwd):/app/tmp doc2oas-ie-sample:master tmp/command.py

echo "------ Move generated openapi to the correct folder --------"

openapi=$(ls | grep gs_*.yaml)
if [ -z $openapi ] ; then 
    echo "-- No openapi file"
    exit 0
else 
    #mv $openapi ../autogenerated/$openapi
	mv $openapi ../gs012-rnis-api/autogenerated/$openapi
fi

cd .. 
cd gs012-rnis-api

echo "\n------ Prepare to push -------"
git add autogenerated/$openapi
version=$(ls doc | grep -e v[0-9]* -o)
git rm doc/$spec
git commit -m "Autogenerated openapi version $version"

echo "\n------ Tag version -----"
echo $version
git tag $version

exit 0
 No newline at end of file
+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