From 3a94781cb102cbd1ff00e043d404d4a0d09da6b8 Mon Sep 17 00:00:00 2001 From: FLYBYME Date: Fri, 2 Feb 2024 14:58:59 -0500 Subject: [PATCH] Add mirror functionality to repositories service --- services/actions.service.js | 109 ++++++++++++++++++++++++++++++ services/repositories.service.js | 110 +++++++++++++++++++++++++++++++ 2 files changed, 219 insertions(+) create mode 100644 services/actions.service.js diff --git a/services/actions.service.js b/services/actions.service.js new file mode 100644 index 0000000..7825da4 --- /dev/null +++ b/services/actions.service.js @@ -0,0 +1,109 @@ +const DbService = require("db-mixin"); +const Membership = require("membership-mixin"); +const ConfigLoader = require("config-mixin"); +const { MoleculerClientError } = require("moleculer").Errors; + + +/** + * this is the git.actions service for tracking actions + */ + +module.exports = { + // name of service + name: "git.actions", + // version of service + version: 1, + + /** + * Service Mixins + * + * @type {Array} + * @property {DbService} DbService - Database mixin + * @property {ConfigLoader} ConfigLoader - Config loader mixin + */ + mixins: [ + DbService({}), + ConfigLoader([ + 'git.**' + ]), + ], + + /** + * Service dependencies + */ + dependencies: [], + + /** + * Service settings + * + * @type {Object} + */ + settings: { + rest: true, + + fields: { + + + // inject dbservice fields + ...DbService.FIELDS,// inject dbservice fields + }, + defaultPopulates: [], + + scopes: { + ...DbService.SCOPE, + }, + + defaultScopes: [ + ...DbService.DSCOPE, + ], + + // default init config settings + config: { + + } + }, + + /** + * service actions + */ + actions: { + + }, + + /** + * service events + */ + events: { + + }, + + /** + * service methods + */ + methods: { + + }, + + + /** + * service created lifecycle event handler + */ + created() { + + }, + + /** + * service started lifecycle event handler + */ + async started() { + + }, + + /** + * service stopped lifecycle event handler + */ + async stopped() { + + } +}; + diff --git a/services/repositories.service.js b/services/repositories.service.js index 183a4b2..dfce513 100644 --- a/services/repositories.service.js +++ b/services/repositories.service.js @@ -170,6 +170,28 @@ module.exports = { }, }, + // push pull mirror array + mirrors: { + type: "array", + items: { + type: "object", + props: { + url: { + type: "string", + required: true, + }, + type: { + type: "string", + enum: ["push", "pull", "mirror"], + required: true, + default: "mirror", + }, + }, + required: false, + }, + required: false, + default: [], + }, // repository tags tags: { @@ -422,6 +444,94 @@ module.exports = { } }, + /** + * add mirror to repository + * + * @actions + * @param {String} id - id of repository + * @param {String} url - mirror url + * @param {String} type - mirror type + * + * @returns {Object} - repository object + */ + addMirror: { + params: { + id: { + type: "string", + required: true, + }, + url: { + type: "string", + required: true, + }, + type: { + type: "string", + enum: ["push", "pull", "mirror"], + required: true, + default: "mirror", + }, + }, + async handler(ctx) { + const params = Object.assign({}, ctx.params); + + // get repository + const repository = await this.getById(ctx, params.id); + + // check repository exists + if (!repository) { + throw new MoleculerClientError("repository not found", 404); + } + + // add mirror to repository + return this.updateEntity(ctx, { + id: repository.id, + mirrors: [...repository.mirrors, { + url: params.url, + type: params.type + }] + }); + } + }, + + /** + * remove mirror from repository + * + * @actions + * @param {String} id - id of repository + * @param {String} url - mirror url + * + * @returns {Object} - repository object + */ + removeMirror: { + params: { + id: { + type: "string", + required: true, + }, + url: { + type: "string", + required: true, + }, + }, + async handler(ctx) { + const params = Object.assign({}, ctx.params); + + // get repository + const repository = await this.getById(ctx, params.id); + + // check repository exists + if (!repository) { + throw new MoleculerClientError("repository not found", 404); + } + + // remove mirror from repository + return this.updateEntity(ctx, { + id: repository.id, + mirrors: repository.mirrors.filter(mirror => mirror.url !== params.url) + }); + } + }, + /** * get repository path *