Skip to content

Commit

Permalink
feat: add filter by branch toggle (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Aug 9, 2022
1 parent b3e7979 commit a22ee23
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 17 deletions.
10 changes: 5 additions & 5 deletions .circleci/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ jobs:
shell: "/bin/bash"
command: |
HEAD_SHA=$(git rev-parse HEAD)
if [[ -z "$CIRCLE_PULL_REQUEST" ]]; then
BASE_SHA=$(git merge-base origin/main HEAD)
else
if [[ $CIRCLE_BRANCH == "main" ]]; then
BASE_SHA=$(git rev-parse origin/main~1)
else
BASE_SHA=$(git merge-base origin/main HEAD)
fi
echo "Comparing head: $NX_HEAD to $HEAD_SHA"
echo "Comparing base: $NX_BASE to $BASE_SHA"
if [[ $NX_HEAD == $HEAD_SHA && $NX_BASE == $BASE_SHA ]]; then
echo "Test conditions met"
else
echo "Exiting Job"
circleci-agent step halt
echo "Test conditions NOT met"
exit 5
fi
workflows:
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.6.0] - 2022-08-09
- **[fix]**: Use explicitly main branch's previous commit when last successful sha was not found
- **[fix]**: Use CircleCI internal env var to check if run is pull request
- **[feat]**: Optionally disable filtering the past workflow runs by branch
- **[feat]**: Optionally disable filtering the past workflow runs by branch

## [1.5.1] - 2022-06-27
- **[fix]**: Build url regex broken for pipeline ids < 10
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ width="100%" alt="Nx - Smart, Extensible Build Framework"></p>
version: 2.1

orbs:
nx: nrwl/nx@1.5.1
nx: nrwl/nx@1.6.0

jobs:
checks:
Expand Down
7 changes: 7 additions & 0 deletions src/commands/set-shas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ parameters:
description: |
By default, only workflows with CircleCI status of "success" will be detected for the main branch.
Enable this option to also detect workflows with CircleCI status of "on_hold" in case your workflow requires manual approvals.
skip-branch-filter:
type: boolean
default: false
description: |
By default, the workflow runs will be filtered by `main` branch. This works fine with standard `push` event. If you
want to use the orb for non-push events (e.g. tag, label etc.) you need to disable branch filtering.
steps:
- run:
Expand All @@ -33,6 +39,7 @@ steps:
PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW: <<parameters.error-on-no-successful-workflow>>
PARAM_WORKFLOW_NAME: <<parameters.workflow-name>>
PARAM_ALLOW_ON_HOLD: <<parameters.allow-on-hold-workflow>>
PARAM_SKIP_BRANCH_FILTER: <<parameters.skip-branch-filter>>
PARAM_SCRIPT: <<include(scripts/find-successful-workflow.js)>>
name: Derives SHAs for base and head for use in `nx affected` commands
shell: "/bin/bash"
Expand Down
2 changes: 1 addition & 1 deletion src/examples/custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: >
usage:
version: 2.1
orbs:
nx: nrwl/nx@1.5.0
nx: nrwl/nx@1.6.0
jobs:
build:
docker:
Expand Down
2 changes: 1 addition & 1 deletion src/examples/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >
usage:
version: 2.1
orbs:
nx: nrwl/nx@1.5.0
nx: nrwl/nx@1.6.0
jobs:
build:
docker:
Expand Down
2 changes: 1 addition & 1 deletion src/examples/private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ description: >
usage:
version: 2.1
orbs:
nx: nrwl/nx@1.5.0
nx: nrwl/nx@1.6.0
32 changes: 32 additions & 0 deletions src/examples/tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
description: >
You can use `set-shas` also for non-push events, but you need to skip the branch check in that case.
usage:
version: 2.1
orbs:
nx: nrwl/[email protected]
jobs:
build:
docker:
- image: cimg/node:14.17-browsers
steps:
- checkout
- run:
name: Install dependencies
command: yarn install --frozen-lockfile
- nx/set-shas:
skip-branch-filter: true
- run:
name: Run Builds
command: yarn nx affected --target=build --base=$NX_BASE --parallel --max-parallel=3
- run:
name: Run Unit Tests
command: yarn nx affected --target=test --base=$NX_BASE --parallel --max-parallel=2
workflows:
my-workflow:
jobs:
- build:
filters:
branches:
ignore: /.*/ # ignore any commit on any branch by default
tags:
only: /^v[0-9]+(\.[0-9]+)*$/ # only act on version tags
14 changes: 8 additions & 6 deletions src/scripts/find-successful-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const branchName = process.argv[3];
const mainBranchName = process.env.MAIN_BRANCH_NAME || process.argv[4];
const errorOnNoSuccessfulWorkflow = process.argv[5] === '1';
const allowOnHoldWorkflow = process.argv[6] === '1';
const workflowName = process.argv[7];
const skipBranchFilter = process.argv[7] === '1';
const workflowName = process.argv[8];
const circleToken = process.env.CIRCLE_API_TOKEN;

const [, host, project] = buildUrl.match(/https?:\/\/([^\/]+)\/(.*)\/\d+/);
Expand All @@ -18,7 +19,7 @@ let BASE_SHA;
BASE_SHA = execSync(`git merge-base origin/${mainBranchName} HEAD`, { encoding: 'utf-8' });
} else {
try {
BASE_SHA = await findSuccessfulCommit(mainBranchName, workflowName);
BASE_SHA = await findSuccessfulCommit(skipBranchFilter ? undefined : mainBranchName, workflowName);
} catch (e) {
process.stderr.write(e.message);
process.exit(1);
Expand Down Expand Up @@ -48,17 +49,18 @@ Found the last successful workflow run on 'origin/${mainBranchName}'.\n\n`);
}
}

process.stdout.write(`Commit: ${BASE_SHA}\n`);
process.stdout.write(`Commit: ${BASE_SHA}\n\n`);
})();

async function findSuccessfulCommit(branch, workflowName) {
const url = `https://${host}/api/v2/project/${project}/pipeline?branch=${branch}`;
const url = `https://${host}/api/v2/project/${project}/pipeline?`;
const params = branch ? [`branch=${branch}`] : [];
let nextPage;
let foundSHA;

do {
const fullUrl = nextPage ? `${url}&page-token=${nextPage}` : url;
const { next_page_token, sha } = await getJson(fullUrl)
const fullParams = params.concat(nextPage ? [`page=${nextPage}`] : []).join('&');
const { next_page_token, sha } = await getJson(`${url}${fullParams}`)
.then(async ({ next_page_token, items }) => {
const pipeline = await findSuccessfulPipeline(items, workflowName);
return {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/set-shas.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
echo "$PARAM_SCRIPT" >>"index.js"
RESPONSE=$(node index.js $CIRCLE_BUILD_URL $CIRCLE_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_WORKFLOW_NAME)
RESPONSE=$(node index.js $CIRCLE_BUILD_URL $CIRCLE_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_SKIP_BRANCH_FILTER $PARAM_WORKFLOW_NAME)
echo "$RESPONSE"
BASE_SHA=$(echo "$RESPONSE" | grep 'Commit:' | sed 's/.*Commit: //')
HEAD_SHA=$(git rev-parse HEAD)
Expand Down

0 comments on commit a22ee23

Please sign in to comment.