From b27a87483669472b03aac252a35c0dde48ea5505 Mon Sep 17 00:00:00 2001 From: "Connor Bell (Makeshift)" Date: Thu, 5 Jan 2023 15:16:07 +0000 Subject: [PATCH] feat: Added the ability to customise a header at the top of the generated file --- action.yml | 10 ++++++++-- package.json | 2 +- src/index.js | 30 ++++++++++++++++++++++-------- yarn.lock | 8 ++++---- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/action.yml b/action.yml index 54a67f4..cb838e6 100644 --- a/action.yml +++ b/action.yml @@ -11,8 +11,14 @@ inputs: default: .github/dependabot.template.yml follow-symbolic-links: - description: 'Indicates whether to follow symbolic links' - default: true + description: 'Indicates whether to follow symbolic links (If you want to put your template in a weird place)' + default: 'true' + + file-header: + description: 'Header to add to the generated file. {input-name} will be replaced with the value of the given input.' + default: | + # This file was generated by the "Generate Dependabot Glob" action. Do not edit it directly. + # Make changes to `{template-file}` and a PR will be automatically created. runs: using: 'node16' diff --git a/package.json b/package.json index a011deb..65c4755 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "js-yaml": "^4.1.0" }, "devDependencies": { - "@vercel/ncc": "^0.34.0", + "@vercel/ncc": "^0.36.0", "standard": "^17.0.0" } } diff --git a/src/index.js b/src/index.js index 738400b..e07cf02 100644 --- a/src/index.js +++ b/src/index.js @@ -5,22 +5,35 @@ const util = require('util') const glob = util.promisify(require('glob')) const path = require('path') -const actionName = 'Makeshift/generate-dependabot-glob-action' +const actionOpts = { + 'template-file': core.getInput('template-file') || '.github/dependabot.template.yml', + 'follow-symbolic-links': core.getInput('follow-symbolic-links') === 'true', + // eslint-disable-next-line no-template-curly-in-string + 'file-header': core.getInput('file-header') || '# This file is generated from ${template-file}' +} const globOpts = { root: process.cwd(), mark: true, matchBase: true, nomount: true, - follow: core.getInput('follow-symbolic-links') === 'true' || true + follow: actionOpts['follow-symbolic-links'] +} + +function parseStringTemplate (str, obj) { + const parts = str.split(/\$\{(?!\d)[\wæøåÆØÅ]*\}/) + const args = str.match(/[^{}]+(?=})/g) || [] + const parameters = args.map(argument => obj[argument] || (obj[argument] === undefined ? '' : obj[argument])) + return String.raw({ raw: parts }, ...parameters) } +// Lazy deep clone. Not great, but works for this purpose. 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 { templateFile, warning } = actionOpts + const template = yaml.load(await fs.readFile(templateFile, 'utf8')) const newUpdates = [] for (const entry of template.updates) { @@ -29,7 +42,7 @@ async function run () { 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}`) + core.info(`Found ${matchingDirs.size} directories matching ${entry.directory}`) for (const dir of matchingDirs) { core.info(`Creating entry for ${dir} with ecosystem ${entry['package-ecosystem']}`) @@ -39,11 +52,12 @@ async function run () { } } - core.info(`Here's the final config: ${JSON.stringify(newUpdates)}`) + core.info(`Here's the final config (JSON): ${JSON.stringify(newUpdates)}`) template.updates = newUpdates core.info('Writing config to .github/dependabot.yml') - const warning = `# This file is generated by the ${actionName} action via a GitHub Actions Workflow from the template file '${templateFile}'. Do not edit directly.` - await fs.writeFile('.github/dependabot.yml', warning + '\n' + yaml.dump(template)) + const finalString = parseStringTemplate(warning, actionOpts) + '\n' + yaml.dump(template) + core.info(finalString) + await fs.writeFile('.github/dependabot.yml', finalString) } run().catch(error => { diff --git a/yarn.lock b/yarn.lock index f21446e..8d938ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -173,10 +173,10 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== -"@vercel/ncc@^0.34.0": - version "0.34.0" - resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.34.0.tgz#d0139528320e46670d949c82967044a8f66db054" - integrity sha512-G9h5ZLBJ/V57Ou9vz5hI8pda/YQX5HQszCs3AmIus3XzsmRn/0Ptic5otD3xVST8QLKk7AMk7AqpsyQGN7MZ9A== +"@vercel/ncc@^0.36.0": + version "0.36.0" + resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.36.0.tgz#1f262b86fc4f0770bbc0fc1d331d5aaa1bd47334" + integrity sha512-/ZTUJ/ZkRt694k7KJNimgmHjtQcRuVwsST2Z6XfYveQIuBbHR+EqkTc1jfgPkQmMyk/vtpxo3nVxe8CNuau86A== acorn-jsx@^5.3.2: version "5.3.2"