Skip to content
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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-<num>'
- key_prefix: 'MYLINK-'
url_template: 'https://mywebsite.com/<num>'

validator:
#pattern: '[a-zA-Z0-9_-]+_[a-zA-Z0-9_-]+.*'
Expand Down
69 changes: 69 additions & 0 deletions lib/plugins/autolinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const Diffable = require('./diffable');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a test too to make sure it works in the future ?

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_prefix}, as it already exists`);
return;
}
throw e;
}
}

async remove({ id }) {
const attrs = {
...this.repo,
autolink_id: 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);
}
};
11 changes: 6 additions & 5 deletions lib/plugins/diffable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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([
Expand Down
1 change: 1 addition & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

Expand Down