Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Created October 3, 2022 13:51
Show Gist options
  • Save apaleslimghost/3fc332b3caa8bdc26e0efe22467bc44a to your computer and use it in GitHub Desktop.
Save apaleslimghost/3fc332b3caa8bdc26e0efe22467bc44a to your computer and use it in GitHub Desktop.
script to test circleci config filters
const yaml = require('yaml')
const fs = require('fs')
const argv = require('minimist')(process.argv.slice(2))
const config = fs.readFileSync('./.circleci/config.yml', 'utf-8')
const { workflows } = yaml.parse(config)
const [key, sha] = argv.branch ? ['branches', argv.branch] : ['tags', argv.tag]
const parseCircleJob = (job) => {
if (typeof job === 'string') {
return { name: job, options: {} }
}
return {
name: Object.keys(job)[0],
options: Object.values(job)[0],
}
}
const matchFilter = (filter) =>
Boolean(filter) &&
(Array.isArray(filter)
? filter.some(matchFilter)
: filter.startsWith('/') && filter.endsWith('/')
? new RegExp(filter.slice(1, -1)).test(sha)
: filter === sha)
let filtered = workflows['tool-kit'].jobs
.map(parseCircleJob)
.filter(({ name, options }) => {
if (!options.filters) {
console.log(`${name}: no filters`)
return key === 'branches'
}
if (!options.filters[key]) {
console.log(`${name}: no ${key} filters`)
return key === 'branches'
}
const { only, ignore } = options.filters[key]
const onlyMatches = matchFilter(only)
const ignoreMatches = matchFilter(ignore)
console.log(
`${name}: ${
only
? `only ${only} ${onlyMatches ? 'matches' : `doesn't match`} ${sha}`
: 'no only'
}. ${
ignore
? `ignore ${ignore} ${
ignoreMatches ? 'matches' : `doesn't match`
} ${sha}`
: 'no ignore'
} `
)
return (!only || onlyMatches) && !ignoreMatches
})
console.log()
console.log(filtered.map(({ name }) => name).join('\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment