Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Makeshift committed Nov 20, 2022
0 parents commit 32be525
Show file tree
Hide file tree
Showing 16 changed files with 720 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/dependabot.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2

updates:
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'daily'

- package-ecosystem: 'docker'
directory: '/test/docker/*/Dockerfile*'
schedule:
interval: 'weekly'

- package-ecosystem: 'npm'
directory: '/test/npm/*/{package-lock.json,yarn.lock}'
schedule:
interval: 'daily'
19 changes: 19 additions & 0 deletions .github/workflows/generate_dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Generate dependabot.yml

on:
push:
repository_dispatch:
workflow_dispatch:

jobs:
generate:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3

- name: Generate dependabot.yml
uses: ./

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
100 changes: 100 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Generate Dependabot Glob Action

This action creates a `dependabot.yml` file from a user-provided template by replacing instances of directory globs with an array of objects matching that glob, with all the other keys copied.

For example, the following template:

```yaml
- package-ecosystem: 'docker'
directory: '/test/docker/*/Dockerfile*'
schedule:
interval: 'daily'
```
Will result in:
```yaml
- package-ecosystem: 'docker'
directory: '/test/docker/container_1/'
schedule:
interval: 'daily'
- package-ecosystem: 'docker'
directory: '/test/docker/container_2/'
schedule:
interval: 'daily'
- package-ecosystem: 'docker'
directory: '/test/docker/weird_dockerfile/'
schedule:
interval: 'daily'
```
Note that the basename of any matching directory is used as the value.
This action uses the [glob](https://www.npmjs.com/package/glob) node module. Refer to its documentation for more information on the glob syntax.
The default configuration for `glob` is as follows:

```js
const globOpts = {
root: process.cwd(),
mark: true,
matchBase: true,
nomount: true,
follow: core.getInput('follow-symbolic-links') === 'true' || true
}
```

If these options are not sufficient, please open an issue and let me know.

## Quickstart

### Create a `.github/dependabot.template.yml` file

```yaml
version: 2
updates:
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'daily'
- package-ecosystem: 'docker'
directory: '/test/docker/*/Dockerfile*'
schedule:
interval: 'weekly'
- package-ecosystem: 'npm'
directory: '/test/npm/*/{package-lock.json,yarn.lock}'
schedule:
interval: 'daily'
```

### Create a `.github/workflows/generate_dependabot.yml` file

Note that this action does not create a PR or otherwise commit the generated file. You will need to do that yourself.

```yaml
name: Generate dependabot.yml
on:
push:
repository_dispatch:
workflow_dispatch:
jobs:
generate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Generate dependabot.yml
uses: Makeshift/generate-dependabot-glob-action@master
- name: Create Pull Request
uses: peter-evans/create-pull-request@v4
```

Done.
17 changes: 17 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'Generate Dependabot Glob'

description:
'Creates a `dependabot.yml` file from a user-provided template by replacing instances of directory globs with an array of objects matching
that glob, with all the other keys copied.'
inputs:
template-file:
description: 'Location of the file to use as template'
default: .github/dependabot.template.yml

follow-symbolic-links:
description: 'Indicates whether to follow symbolic links'
default: true

runs:
using: 'node16'
main: 'src/index.js'
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "generate-dependabot-glob-action",
"version": "1.0.0",
"description": "This action creates a `dependabot.yml` file from a user-provided template by replacing instances of `directory: **/x` with an array of objects matching that glob, with the keys copied.",
"main": "index.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Makeshift/generate-dependabot-action.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Makeshift/generate-dependabot-action/issues"
},
"homepage": "https://github.com/Makeshift/generate-dependabot-action#readme",
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"glob": "^8.0.3",
"js-yaml": "^4.1.0"
}
}
50 changes: 50 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const core = require('@actions/core');
const yaml = require('js-yaml');
const fs = require('fs').promises;
const util = require('util');
const glob = util.promisify(require('glob'))
const path = require('path')
const github = require('@actions/github')

const globOpts = {
root: process.cwd(),
mark: true,
matchBase: true,
nomount: true,
follow: core.getInput('follow-symbolic-links') === 'true' || true
}

const clone = obj => JSON.parse(JSON.stringify(obj))

async function run() {
const templateFile = core.getInput('template-file') || '.github/dependabot.template.yml';
const template = yaml.load(await fs.readFile(templateFile, 'utf8'));

const newUpdates = []

for (let entry of template.updates) {
core.info(`Processing entry ${entry.directory} for ecosystem ${entry["package-ecosystem"]}`)
const baseUpdate = clone(entry)
const matchingFiles = await glob(entry.directory, globOpts)
core.info(`Found ${matchingFiles.length} files matching ${entry.directory}`)
const matchingDirs = new Set(matchingFiles.map(file => path.dirname(file)))
core.info(`Found ${matchingDirs.length} directories matching ${entry.directory}`)

for (let dir of matchingDirs) {
core.info(`Creating entry for ${dir} with ecosystem ${entry["package-ecosystem"]}`)
const newUpdate = clone(baseUpdate)
newUpdate.directory = dir
newUpdates.push(newUpdate)
}
}

core.info(`Here's the final config: ${JSON.stringify(newUpdates)}`)
template.updates = newUpdates
core.info("Writing config to .github/dependabot.yml")
await fs.writeFile('.github/dependabot.yml', yaml.dump(template))
}

run().catch(error => {
console.log(error)
core.setFailed(error.message)
});
1 change: 1 addition & 0 deletions test/docker/container_1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine
1 change: 1 addition & 0 deletions test/docker/container_2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine
1 change: 1 addition & 0 deletions test/docker/no_dockerfile/intentionally_left_empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi :)
1 change: 1 addition & 0 deletions test/docker/weird_dockerfile/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM alpine
Loading

0 comments on commit 32be525

Please sign in to comment.