From d6cfd8a452f00b0f1706f3c106844c35b32313fb Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Wed, 27 Apr 2022 23:44:47 +0000 Subject: [PATCH 1/3] Add support for autolinks in safe-settings --- README.md | 8 +++++ lib/plugins/autolinks.js | 68 ++++++++++++++++++++++++++++++++++++++++ lib/plugins/diffable.js | 11 ++++--- lib/settings.js | 1 + 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 lib/plugins/autolinks.js diff --git a/README.md b/README.md index 705c82ca6..c84c51b94 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Using the settings, the following things could be configured: - `Collaborators and permissions` - `Issue labels` - `Branch protections`. If the name of the branch is `default` in the settings, it is applied to the `default` branch of the repo. +- `Autolinks` - `repository name validation` using regex pattern It is possible to provide an `include` or `exclude` settings to restrict the `collaborators`, `teams`, `labels` to a list of repos or exclude a set of repos for a collaborator. @@ -298,6 +299,13 @@ branches: apps: [] users: [] teams: [] + +# See the docs (https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/managing-repository-settings/configuring-autolinks-to-reference-external-resources) for a description of autolinks and replacement values. +autolinks: + - key_prefix: 'JIRA-' + url_template: 'https://jira.github.com/browse/JIRA-' + - key_prefix: 'MYLINK-' + url_template: 'https://mywebsite.com/' validator: #pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*' diff --git a/lib/plugins/autolinks.js b/lib/plugins/autolinks.js new file mode 100644 index 000000000..667ca99a5 --- /dev/null +++ b/lib/plugins/autolinks.js @@ -0,0 +1,68 @@ +const Diffable = require('./diffable'); +const NopCommand = require('../nopcommand'); + +module.exports = class Autolinks extends Diffable { + constructor(...args) { + super(...args); + } + + async find() { + const { data } = await this.github.repos.listAutolinks(this.repo); + return data; + } + + comparator(existing, attr) { + return existing.key_prefix === attr.key_prefix && existing.url_template === attr.url_template; + } + + changed(existing, attr) { + return existing.key_prefix === attr.key_prefix && existing.url_template !== attr.url_template; + } + + async update(existing, attr) { + await this.remove(existing); + return this.add(attr); + } + + async add({ key_prefix, url_template }) { + const attrs = { + ...this.repo, + key_prefix, + url_template, + }; + + if (this.nop) { + return new NopCommand( + this.constructor.name, + this.repo, + this.github.repos.createAutolink.endpoint(attrs), + 'Add autolink', + ); + } + + try { + return this.github.repos.createAutolink(attrs); + } catch (e) { + if (e?.response?.data?.errors?.[0]?.code === 'already_exists') { + this.log.debug(`Did not update ${key}, as it already exists`); + } + throw e; + } + } + + async remove({ id }) { + const attrs = { + ...this.repo, + id, + }; + if (this.nop) { + return new NopCommand( + this.constructor.name, + this.repo, + this.github.repos.deleteAutolink.endpoint(attrs), + 'Remove autolink', + ); + } + return this.github.repos.deleteAutolink(attrs); + } +}; diff --git a/lib/plugins/diffable.js b/lib/plugins/diffable.js index 9426f191b..4deb0036c 100644 --- a/lib/plugins/diffable.js +++ b/lib/plugins/diffable.js @@ -81,6 +81,12 @@ module.exports = class Diffable { return this.find().then(existingRecords => { const changes = [] + existingRecords.forEach(x => { + if (!filteredEntries.find(y => this.comparator(x, y))) { + changes.push(this.remove(x)) + } + }) + filteredEntries.forEach(attrs => { const existing = existingRecords.find(record => { return this.comparator(record, attrs) @@ -93,11 +99,6 @@ module.exports = class Diffable { } }) - existingRecords.forEach(x => { - if (!filteredEntries.find(y => this.comparator(x, y))) { - changes.push(this.remove(x)) - } - }) if (changes.length === 0) { if (this.nop) { return Promise.resolve([ diff --git a/lib/settings.js b/lib/settings.js index 689e0df0c..3b73d0c95 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -637,6 +637,7 @@ Settings.PLUGINS = { teams: require('./plugins/teams'), milestones: require('./plugins/milestones'), branches: require('./plugins/branches'), + autolinks: require('./plugins/autolinks'), validator: require('./plugins/validator') } From a93c44df1a385a30ce6f37ff1bec29671432fc9b Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Thu, 28 Apr 2022 18:33:53 +0000 Subject: [PATCH 2/3] fix error case --- lib/plugins/autolinks.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/plugins/autolinks.js b/lib/plugins/autolinks.js index 667ca99a5..af95f9054 100644 --- a/lib/plugins/autolinks.js +++ b/lib/plugins/autolinks.js @@ -44,7 +44,8 @@ module.exports = class Autolinks extends Diffable { return this.github.repos.createAutolink(attrs); } catch (e) { if (e?.response?.data?.errors?.[0]?.code === 'already_exists') { - this.log.debug(`Did not update ${key}, as it already exists`); + this.log.debug(`Did not update ${key_prefix}, as it already exists`); + return; } throw e; } From 7362a2417fe70e09dccc055ce27372fc7639733a Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Thu, 28 Apr 2022 18:54:59 +0000 Subject: [PATCH 3/3] fix --- lib/plugins/autolinks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/autolinks.js b/lib/plugins/autolinks.js index af95f9054..7cc1b396b 100644 --- a/lib/plugins/autolinks.js +++ b/lib/plugins/autolinks.js @@ -54,7 +54,7 @@ module.exports = class Autolinks extends Diffable { async remove({ id }) { const attrs = { ...this.repo, - id, + autolink_id: id, }; if (this.nop) { return new NopCommand(