validate-in-docker.sh 2.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# Copyright ETSI 2017
# See: https://forge.etsi.org/etsi-forge-copyright-statement.txt

# Merges OpenAPIs interfaces in the working directory
# and validates them individually.
# Exit status is 0 if all validation passed, 1 otherwise

# Usage:
# $0 <working-directory> <storage-dir>

#set -x

function store_api () {
	f="$1"
	cp -v "$1" "${storage_dir}/"
17
  rm "$1"
18
19
20
21
22
23
24
}

function validate_api () {
  file="$1"
  api="$2"
  deliverable="$3"
  echo "--- Merging file."
25
26
  merged_file="../build/${deliverable}-${api}-API.yaml"
  json_file="../build/${deliverable}-${api}-API.json"
27
  json-refs resolve "${file}" > "${merged_file}"
28
  yaml2json "${merged_file}" > "${json_file}"
29
30
31
32

  echo "--- Validating ${merged_file}"
  swagger-tools validate "${merged_file}"
  vres=$?
33
  echo "--- Validation done ($vres)."
34
35

  # If validation succedes, store the generated file
36
37
  [ $vres -a -d "/storage" ] && store_api "${merged_file}"
  [ $vres -a -d "/storage" ] && store_api "${json_file}"
38
39
40
41
  
  return $vres
}

42
43
44
45
46
47
48
49
50
51
52
# usage get_api_from_fn <file_name>
#  e.g. get_api_from_fn /path/to/SOL003/Api1/Api1.yaml returns Api1
function get_api_from_fn () {
  echo "$(basename $(dirname $1))"
}

# usage get_api_from_fn <file_name>
#  e.g. get_api_from_fn /path/to/SOL003/Api1/Api1.yaml returns Api1
function get_deliverable_from_fn () {
  echo "$(basename $(dirname $(dirname $1 )))"
}
53
54
55
56
57
58

## Main ##

wd="${1?"Usage: $0 <working-directory> <storage-directory>"}"
storage_dir="${2?"Usage: $0 <working-directory> <storage-directory>"}"

59
60
61
echo "Entering dir $wd/src"
cd "$wd/src"
mkdir -p "$wd/build"
62
63
64
65
66

# Stores the overall validation result
# (single results in OR)
fres=0

67
68
69
70
71
72
73
74
75
76
77
for f in $(find -name "*.yaml") ; do
      echo "Found yaml file: $f"
      file=$(basename "$f")
      api=$(get_api_from_fn $f)
      deliverable=$(get_deliverable_from_fn $f)
      if [ "$file" = "$api.yaml" ]; then
          echo "-- Will validate: $f (api: $api) (deliverable:$deliverable)"         
          validate_api "$f" "$api" "$deliverable"
          res=$?
          fres=$(($fres||$res))
      fi
78
79
80
81
82
done

# Exit code needed for jenkins to know the verdict of the build
echo "-- Final validator returns $fres."
exit $fres