Skip to content

Commit

Permalink
Merge pull request #3 from kulovan/fix_syntax_errors_for_commands
Browse files Browse the repository at this point in the history
Fix command script
  • Loading branch information
whyayen authored Mar 26, 2022
2 parents 47e50f4 + 157aca0 commit d32239f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 64 deletions.
10 changes: 10 additions & 0 deletions src/@orb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2.1

description: >
The orb helps you that cancel running CI with your GitHub draft pull request
automatically to save your credits.
# This information will be displayed in the orb registry and is not mandatory.
display:
home_url: "https://github.com/whyayen/bye-github-draft"
source_url: "https://github.com/whyayen/bye-github-draft"
64 changes: 0 additions & 64 deletions src/bye-github-draft.yml

This file was deleted.

6 changes: 6 additions & 0 deletions src/commands/check-skippable-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
steps:
- run: apk add --no-cache bash curl jq
- run:
shell: /bin/bash
name: Check skippable PR !!!!
command: <<include(scripts/check-skippable-pr.sh)>>
4 changes: 4 additions & 0 deletions src/jobs/check-skippable-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docker:
- image: alpine:3.7
steps:
- check-skippable-pr
51 changes: 51 additions & 0 deletions src/scripts/check-skippable-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

required_env_vars=(
"GITHUB_TOKEN"
"CIRCLE_TOKEN"
)

for required_env_var in ${required_env_vars[@]}; do
if [[ -z "${!required_env_var}" ]]; then
printf "${required_env_var} not provided, but that doesn't mean we should skip CI.\n"
exit 0
fi
done

# Since we're piggybacking off of an existing OAuth var, tweak the var for our uses
token=$(printf "${GITHUB_TOKEN}" | cut -d':' -f1)

headers="Authorization: token $token"
api_endpoint="https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PULL_REQUEST##*/}"

# Fetch PR metadata from Github's API and parse fields from json
github_res=$(curl --silent --header "${headers}" "${api_endpoint}" | jq '{draft: .draft, title: .title}')
draft=$(printf "${github_res}" | jq '.draft')
title=$(printf "${github_res}" | jq '.title' | tr '[:upper:]' '[:lower:]')

if [[ "${title}" == "null" && "${draft}" == "null" ]]; then
printf "Couldn't fetch info on PR, but that doesn't mean we should skip CI.\n"
exit 0
fi

cancel_running_jobs=0

if [[ "${draft}" == true ]]; then
printf "PR is a draft, skipping CI!\n"
cancel_running_jobs=1
fi

for skip_token in '[skip ci]' '[ci skip]' '[wip]'; do
if [[ ${title} == *"${skip_token}"* ]]; then
printf "Found \"${skip_token}\" in PR title, skipping CI!\n"
cancel_running_jobs=1
fi
done

if [[ "${cancel_running_jobs}" == 1 ]]; then
curl -X POST https://circleci.com/api/v2/workflow/${CIRCLE_WORKFLOW_ID}/cancel -H 'Accept: application/json' -u "${CIRCLE_TOKEN}:"
echo 'Cancel'
else
printf "No reason to skip CI, let's go!"
fi
exit 0

0 comments on commit d32239f

Please sign in to comment.