Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.idea
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
FROM node:10.15.1-alpine
FROM node:14-alpine

# from https://github.com/spritsail/alpine-cmake/blob/master/Dockerfile
# needed to build @hanazuki/node-jsonnet
RUN apk --no-cache add cmake clang clang-dev make gcc g++ libc-dev linux-headers

WORKDIR /app
COPY . .

COPY package.json yarn.lock ./
RUN yarn

CMD /usr/local/bin/node /app/plugin.js
COPY plugin.js ./
COPY lib lib/

CMD /usr/local/bin/node /app/plugin.js
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ See [the official docs](https://docs.drone.io/extend/config) for extra informati
## Pattern Matching

This uses the [Glob](https://www.npmjs.com/package/glob) module under the hood, so supports all pattern matching syntaxes of this module.

## Deployment
To deploy to AWS ECR, pull down a temp mfa token for AWS Platinum, and run the following:
1. `aws ecr get-login-password --region us-east-2 --profile mfa | docker login --username AWS --password-stdin 650007492008.dkr.ecr.us-east-2.amazonaws.com`
2. `docker build -t 650007492008.dkr.ecr.us-east-2.amazonaws.com/drone-config-plugin-changeset-conditional:1.0.0 .`
3. `docker image push 650007492008.dkr.ecr.us-east-2.amazonaws.com/drone-config-plugin-changeset-conditional:1.0.0`
16 changes: 12 additions & 4 deletions lib/parsed-yaml-retriever.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const Jsonnet = require('@hanazuki/node-jsonnet')
const { promisify } = require('util')
const yaml = require('yaml')
const getParsedYaml = gh => async data => {

const jsonnet = new Jsonnet.Jsonnet()

const getParsedYaml = (gh) => async (data) => {
let contents = data.yaml
if (!contents) {
const options = {
user: data.repo.namespace,
repo: data.repo.name,
ref: data.build.ref,
path: data.repo.config_path
path: data.repo.config_path,
}
const file = await promisify(gh.repos.getContent)(options)
if (!file.content) {
Expand All @@ -16,11 +20,15 @@ const getParsedYaml = gh => async data => {
}
contents = Buffer.from(file.content, 'base64').toString()
}
if (data.repo.config_path.endsWith('.jsonnet')) {
// this returns formatted JSON that we can then parse to YAML as normal
contents = await jsonnet.evaluateSnippet(contents)
}
const docs = contents.split('\n---\n')
const parsedDocs = docs.map(d => {
const parsedDocs = docs.map((d) => {
return yaml.parse(d)
})
return parsedDocs
}

module.exports = getParsedYaml
module.exports = getParsedYaml
19 changes: 12 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drone-config-changeset-conditional",
"version": "1.0.0",
"version": "5.0.0",
"description": "",
"main": "plugin.js",
"scripts": {
Expand All @@ -9,11 +9,16 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.4",
"github4": "^1.1.1",
"globule": "^1.2.1",
"http-signature": "^1.2.0",
"yaml": "^1.7.1"
"@hanazuki/node-jsonnet": "0.4.2",
"body-parser": "1.18.3",
"express": "4.16.4",
"github4": "1.1.1",
"globule": "1.2.1",
"http-signature": "1.2.0",
"yaml": "1.7.1"
},
"prettier": {
"semi": false,
"singleQuote": true
}
}
20 changes: 16 additions & 4 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,27 @@ app.post('/', bodyParser.json({limit: '50mb'}), async (req, res) => {
}
}

const trimmedSteps = py.steps.filter(s => {
if (!s.when || !s.when.changeset || !s.when.changeset.includes) return true
const transformedSteps = py.steps.map(s => {
if (!s.when || !s.when.changeset || !s.when.changeset.includes) {
return s;
}

const requiredFiles = s.when.changeset.includes
const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true })
console.log('Matched files for step:', matchedFiles.length, 'Allowed matches:', requiredFiles)
return matchedFiles.length

if (!matchedFiles.length) {
// Add an impossible conditional which guarantees the step gets skipped
s.when = {
...s.when,
event: { exclude: ['*'] },
}
}

return s;
})

return trimmedSteps.length ? yaml.stringify({ ...py, steps: trimmedSteps }) : nullYaml(index)
return transformedSteps.length ? yaml.stringify({ ...py, steps: transformedSteps }) : nullYaml(index)
})

res.json({ Data: finalYamlDocs.join('\n---\n') })
Expand Down
Loading