From c53688c67eaf2e2c35672d415e930c314ddd55ba Mon Sep 17 00:00:00 2001 From: Iain Sproat <68657+iainsproat@users.noreply.github.com> Date: Mon, 21 Nov 2022 11:20:36 +0000 Subject: [PATCH] CI(CircleCI): Halt building if a draft PR (#1214) * Halt building if a draft PR --- .circleci/config.yml | 15 ++++++++++++++- .circleci/is_draft.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100755 .circleci/is_draft.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 72b6d5557..a242a7d94 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -30,6 +30,8 @@ workflows: filters: *filters-allow-all - docker-build-server: + context: &build-context + - github-readonly-public-repos filters: &filters-build tags: only: /.*/ @@ -38,35 +40,41 @@ workflows: - get-version - docker-build-frontend: + context: *build-context filters: *filters-build requires: - get-version - docker-build-webhooks: + context: *build-context filters: *filters-build requires: - get-version - test-server - docker-build-file-imports: + context: *build-context filters: *filters-build requires: - get-version - test-server - docker-build-previews: + context: *build-context filters: *filters-build requires: - get-version - test-server - docker-build-test-container: + context: *build-context filters: *filters-build requires: - get-version - test-server - docker-build-monitor-container: + context: *build-context filters: *filters-build requires: - get-version @@ -365,11 +373,16 @@ jobs: - checkout - attach_workspace: at: /tmp/ci/workspace + - run: + name: determine if draft PR + command: | + echo "export IS_DRAFT_PR=$(.circleci/is_draft.sh)" >> workspace/env-vars - run: cat workspace/env-vars >> $BASH_ENV + - run: echo "IS_DRAFT_PR=${IS_DRAFT_PR}" - run: name: 'Check if should proceed' command: | - [[ "${CIRCLE_BRANCH}" != "main" && -z "${CIRCLE_PULL_REQUEST}" ]] && echo "Should not build, stopping" && circleci-agent step halt && exit 1 + [[ "${CIRCLE_BRANCH}" != "main" && ("${IS_DRAFT_PR}" == "TRUE" || -z "${CIRCLE_PULL_REQUEST}") ]] && echo "Should not build, stopping" && circleci-agent step halt && exit 1 echo "proceeding" - setup_remote_docker: # a weird issue with yarn installing packages throwing EPERM errors diff --git a/.circleci/is_draft.sh b/.circleci/is_draft.sh new file mode 100755 index 000000000..991fe9367 --- /dev/null +++ b/.circleci/is_draft.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# acknowledgements: https://github.com/vitalinfo/circleci-cancel-draft + +set -euf -o pipefail + +if [[ -z "$CIRCLE_PULL_REQUEST" ]]; then + echo "FALSE" +fi + +PR_NUMBER="${CIRCLE_PULL_REQUEST//[!0-9]/}" +RESPONSE=$(curl --silent \ + -H "Authorization: token ${GITHUB_TOKEN}" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${PR_NUMBER}" +) + +DRAFT=$(echo "${RESPONSE}" | jq ".draft") +DRAFT_LABEL=$(echo "${RESPONSE}" | jq ".labels | map(select(.name | test(\"Draft\"))) | .[]") + +if [[ ${DRAFT} == 'true' || ${DRAFT_LABEL} ]]; then + echo "TRUE" +else + echo "FALSE" +fi + +exit 0