From f2ad9b925db612c0ef6f08d21a391f96a0e2ad7d Mon Sep 17 00:00:00 2001 From: minchao Date: Sat, 20 Jul 2019 20:09:50 +0800 Subject: [PATCH] Fix check oas spec --- .travis.yml | 6 ++-- scripts/{check-spec.sh => check-oas-spec.sh} | 35 ++++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) rename scripts/{check-spec.sh => check-oas-spec.sh} (63%) diff --git a/.travis.yml b/.travis.yml index 4b25325d..90e77d72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,7 @@ install: - go mod download script: - - if [[ $TRAVIS_BRANCH == "check-spec" ]]; then + - if [[ $TRAVIS_BRANCH == "check-oas-spec" ]]; then make spec; make generate; fi @@ -45,7 +45,7 @@ script: deploy: - provider: script skip_cleanup: true - script: bash ${TRAVIS_BUILD_DIR}/scripts/check-spec.sh + script: bash ${TRAVIS_BUILD_DIR}/scripts/check-oas-spec.sh on: - branch: check-spec + branch: check-oas-spec condition: $DEPLOY = true diff --git a/scripts/check-spec.sh b/scripts/check-oas-spec.sh similarity index 63% rename from scripts/check-spec.sh rename to scripts/check-oas-spec.sh index ce4aecab..cc2cdf38 100755 --- a/scripts/check-spec.sh +++ b/scripts/check-oas-spec.sh @@ -3,6 +3,7 @@ set -eo pipefail owner="minchao" repo="go-ptx" +isSpecChanged=false function setup() { if [[ "${TRAVIS}" == "true" ]]; then @@ -17,30 +18,38 @@ function setup() { fi } -function git_diff() { - local specs - specs=$(find . -maxdepth 1 -type f -name "oas.*") - for spec in ${specs} +function check_oas_spec() { + for spec in ./oas.*.*.json do IFS='.' read -r -a substrings <<< "${spec}" target_folders="${target_folders} ${substrings[2]}" done + target_folders="$(echo -e "${target_folders}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" export target_folders - output=$(git diff origin/master -- ${target_folders} README.md) - export output + local output + output=$(git diff origin/master -- ${target_folders}) + if [[ -n "${output}" ]]; then + isSpecChanged=true + fi } function git_push() { - new_branch="spec-changes-on-$(date +"%Y%m%d")" - message="OAS spec changes on the $(date +"%Y%m%d")" + local version + version=$(printf '%(%Y-%m-%d)T\n' -1) + + new_branch="spec-changes-on-${version}" + export new_branch + message="OAS spec changes on the ${version}" + export message + + git add ${target_folders} oas.*.*.json git checkout -b "${new_branch}" - git add ${target_folders} README.md git commit -m "${message}" git push --quiet origin "${new_branch}" } -function github_draft_pr() { +function create_github_pull_request() { curl -X POST \ -H "authorization: Bearer ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ @@ -51,9 +60,9 @@ function github_draft_pr() { setup git fetch origin master -git_diff +check_oas_spec -if [[ -n "${output}" ]]; then +if [[ ${isSpecChanged} == true ]]; then git_push - github_draft_pr + create_github_pull_request fi