Skip to content

Commit

Permalink
feat: add allow not run workflow option
Browse files Browse the repository at this point in the history
  • Loading branch information
eladhaim committed Feb 7, 2024
1 parent 1b5230d commit 1085df5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
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.
allow-not-run-workflow:
type: boolean
default: false
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 "not_run" in case your workflow requires manual approvals.
skip-branch-filter:
type: boolean
default: false
Expand All @@ -39,6 +45,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>>
PARMA_ALLOW_NOT_RUN: <<parameters.allow-not-run-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
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.6.2
nx: nrwl/nx@1.7.2
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.6.2
nx: nrwl/nx@1.7.2
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.6.2
nx: nrwl/nx@1.7.2
2 changes: 1 addition & 1 deletion src/examples/tags.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.6.1
nx: nrwl/nx@1.7.2
jobs:
build:
docker:
Expand Down
34 changes: 27 additions & 7 deletions src/scripts/find-successful-workflow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const errorOnNoSuccessfulWorkflow = process.argv[5] === '1';
const allowOnHoldWorkflow = process.argv[6] === '1';
const skipBranchFilter = process.argv[7] === '1';
const workflowName = process.argv[8];
const allowNotRunWorkflow = process.argv[9] == '1';
const circleToken = process.env.CIRCLE_API_TOKEN;

const [, host, project] = buildUrl.match(/https?:\/\/([^\/]+)\/(.*)\/\d+/);
Expand Down Expand Up @@ -51,7 +52,9 @@ WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}'.
We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'.
NOTE: You can instead make this a hard error by settting 'error-on-no-successful-workflow' on the step in your workflow.\n\n`);
BASE_SHA = execSync(`git rev-parse origin/${mainBranchName}~1`, { encoding: 'utf-8' });
BASE_SHA = execSync(`git rev-parse origin/${mainBranchName}~1`, {
encoding: "utf-8",
});
}
} else {
process.stdout.write(`
Expand Down Expand Up @@ -108,11 +111,28 @@ function commitExists(commitSha) {

async function isWorkflowSuccessful(pipelineId, workflowName) {
if (!workflowName) {
return getJson(`https://${host}/api/v2/pipeline/${pipelineId}/workflow`)
.then(({ items }) => items.every(item => (item.status === 'success') || (allowOnHoldWorkflow && item.status === 'on_hold')));
return getJson(
`https://${host}/api/v2/pipeline/${pipelineId}/workflow`
).then(({ items }) =>
items.every(
(item) =>
item.status === "success" ||
(allowOnHoldWorkflow && item.status === "on_hold") ||
(allowNotRunWorkflow && item.status === "not_run")
)
);
} else {
return getJson(`https://${host}/api/v2/pipeline/${pipelineId}/workflow`)
.then(({ items }) => items.some(item => ((item.status === 'success') || (allowOnHoldWorkflow && item.status === 'on_hold')) && item.name === workflowName));
return getJson(
`https://${host}/api/v2/pipeline/${pipelineId}/workflow`
).then(({ items }) =>
items.some(
(item) =>
(item.status === "success" ||
(allowOnHoldWorkflow && item.status === "on_hold") ||
(allowNotRunWorkflow && item.status === "not_run")) &&
item.name === workflowName
)
);
}
}

Expand All @@ -122,8 +142,8 @@ async function getJson(url) {

if (circleToken) {
options.headers = {
'Circle-Token': circleToken
}
"Circle-Token": circleToken,
};
}

https.get(url, options, (res) => {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/set-shas.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if [ -z "$CIRCLE_BRANCH" ]; then
else
TARGET_BRANCH=$CIRCLE_BRANCH
fi
RESPONSE=$(node index.js $CIRCLE_BUILD_URL $TARGET_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_SKIP_BRANCH_FILTER $PARAM_WORKFLOW_NAME)
RESPONSE=$(node index.js $CIRCLE_BUILD_URL $TARGET_BRANCH $PARAM_MAIN_BRANCH $PARAM_ERROR_ON_NO_SUCCESSFUL_WORKFLOW $PARAM_ALLOW_ON_HOLD $PARAM_SKIP_BRANCH_FILTER $PARAM_WORKFLOW_NAME $PARMA_ALLOW_NOT_RUN)
echo "$RESPONSE"
BASE_SHA=$(echo "$RESPONSE" | grep 'Commit:' | sed 's/.*Commit: //')
HEAD_SHA=$(git rev-parse HEAD)
Expand Down

0 comments on commit 1085df5

Please sign in to comment.