From ba72f4236b77c838d2fc09db7e3c7a89be23939b Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Wed, 27 Apr 2022 00:09:18 +0200 Subject: [PATCH 1/2] Move validations to their own file --- lib/index.js | 78 +++++++------------------------------------- lib/validations.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 67 deletions(-) create mode 100644 lib/validations.js diff --git a/lib/index.js b/lib/index.js index 7799ceb..b7d1144 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,17 @@ const Joi = require('@hapi/joi') const Axios = require('axios') const Querystring = require('querystring') +const { + RetryBuild, + User, + LatestBuild, + TriggerBuild, + RepoSettings, + Secret, + Cron, + SelfRepos, + SyncRepos +} = require('./validations') /** * Drone client @@ -922,73 +933,6 @@ class Client { } } -const RetryBuild = Joi.object().pattern( - /.*/, - Joi.string() -) - -const User = Joi.object({ - login: Joi.string(), - email: Joi.string(), - avatar: Joi.string(), - machine: Joi.boolean(), - admin: Joi.boolean(), - active: Joi.boolean(), - syncing: Joi.boolean(), - synced: Joi.date().timestamp('unix'), - created: Joi.date().timestamp('unix'), - updated: Joi.date().timestamp('unix'), - last_login: Joi.date().timestamp('unix') -}) - -const LatestBuild = Joi.object({ - ref: Joi.string(), - branch: Joi.string() -}) - -const TriggerBuild = Joi.object({ - branch: Joi.string(), - commit: Joi.string() -}) - -const RepoSettings = Joi.object({ - visibility: Joi.string(), - config_path: Joi.string(), - trusted: Joi.boolean(), - protected: Joi.boolean(), - ignore_forks: Joi.boolean(), - ignore_pull_requests: Joi.boolean(), - auto_cancel_pull_requests: Joi.boolean(), - auto_cancel_pushes: Joi.boolean(), - auto_cancel_running: Joi.boolean(), - timeout: Joi.number(), - throttle: Joi.number(), - counter: Joi.number() -}) - -const Secret = Joi.object({ - name: Joi.string(), - data: Joi.string(), - pull_request: Joi.boolean(), - pull_request_push: Joi.boolean() -}) - -const Cron = Joi.object({ - name: Joi.string(), - branch: Joi.string(), - expr: Joi.string(), - target: Joi.string(), - disabled: Joi.boolean() -}) - -const SelfRepos = Joi.object({ - latest: Joi.boolean() -}) - -const SyncRepos = Joi.object({ - async: Joi.boolean() -}) - module.exports = { Client, RetryBuild, diff --git a/lib/validations.js b/lib/validations.js new file mode 100644 index 0000000..191abff --- /dev/null +++ b/lib/validations.js @@ -0,0 +1,80 @@ +const Joi = require('@hapi/joi') + +const RetryBuild = Joi.object().pattern( + /.*/, + Joi.string() +) + +const User = Joi.object({ + login: Joi.string(), + email: Joi.string(), + avatar: Joi.string(), + machine: Joi.boolean(), + admin: Joi.boolean(), + active: Joi.boolean(), + syncing: Joi.boolean(), + synced: Joi.date().timestamp('unix'), + created: Joi.date().timestamp('unix'), + updated: Joi.date().timestamp('unix'), + last_login: Joi.date().timestamp('unix') +}) + +const LatestBuild = Joi.object({ + ref: Joi.string(), + branch: Joi.string() +}) + +const TriggerBuild = Joi.object({ + branch: Joi.string(), + commit: Joi.string() +}) + +const RepoSettings = Joi.object({ + visibility: Joi.string(), + config_path: Joi.string(), + trusted: Joi.boolean(), + protected: Joi.boolean(), + ignore_forks: Joi.boolean(), + ignore_pull_requests: Joi.boolean(), + auto_cancel_pull_requests: Joi.boolean(), + auto_cancel_pushes: Joi.boolean(), + auto_cancel_running: Joi.boolean(), + timeout: Joi.number(), + throttle: Joi.number(), + counter: Joi.number() +}) + +const Secret = Joi.object({ + name: Joi.string(), + data: Joi.string(), + pull_request: Joi.boolean(), + pull_request_push: Joi.boolean() +}) + +const Cron = Joi.object({ + name: Joi.string(), + branch: Joi.string(), + expr: Joi.string(), + target: Joi.string(), + disabled: Joi.boolean() +}) + +const SelfRepos = Joi.object({ + latest: Joi.boolean() +}) + +const SyncRepos = Joi.object({ + async: Joi.boolean() +}) + +module.exports = { + RetryBuild, + User, + LatestBuild, + TriggerBuild, + RepoSettings, + Secret, + Cron, + SelfRepos, + SyncRepos +} From 26727a7e73e2a195486c68ff1d9e6e33b9a288ac Mon Sep 17 00:00:00 2001 From: Austin Flores Date: Sat, 30 Apr 2022 13:54:12 +0200 Subject: [PATCH 2/2] Move resources from index --- lib/index.js | 139 +----------------------------- lib/resources/repositories.js | 156 ++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 136 deletions(-) create mode 100644 lib/resources/repositories.js diff --git a/lib/index.js b/lib/index.js index b7d1144..41c0df1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,7 @@ const Joi = require('@hapi/joi') const Axios = require('axios') const Querystring = require('querystring') +const { Repositories } = require('./resources/repositories') const { RetryBuild, User, @@ -71,17 +72,6 @@ class Client { ) } - /** - * Sync repos - * @param {SyncRepos} params Filter parameters - */ - syncRepos (params) { - return this._axios.post( - '/api/user/repos', - Querystring.stringify(params) - ) - } - /** * Update self * @param {User} self Changes to apply @@ -95,131 +85,6 @@ class Client { ) } - /** - * Self repos - * @param {SelfRepos} params Filter parameters - */ - selfRepos (params) { - Joi.assert(params, SelfRepos, 'Specify valid params') - - return this._axios.get( - '/api/user/repos', { - params: params - }) - } - - /** - * Get repos - * @param {integer} page Page number - * @param {integer} limit Page limit - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/all.go - */ - getRepos (page = 1, limit = 10000) { - return this._axios.get( - '/api/repos', { - params: { - page: page, - per_page: limit - } - }) - } - - /** - * Get repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/find.go - */ - getRepo (owner, repo) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - - return this._axios.get( - `/api/repos/${owner}/${repo}` - ) - } - - /** - * Enable repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/enable.go - */ - enableRepo (owner, repo) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - - return this._axios.post( - `/api/repos/${owner}/${repo}` - ) - } - - /** - * Disable repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/disable.go - */ - disableRepo (owner, repo, remove = false) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - Joi.assert(remove, Joi.boolean().required(), 'Must specify remove') - - return this._axios.delete( - `/api/repos/${owner}/${repo}`, { - params: { - remove: remove - } - }) - } - - /** - * Chown repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - */ - chownRepo (owner, repo) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - - return this._axios.post( - `/api/repos/${owner}/${repo}/chown` - ) - } - - /** - * Repair repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/repair.go - */ - repairRepo (owner, repo) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - - return this._axios.post( - `/api/repos/${owner}/${repo}/repair` - ) - } - - /** - * Update repo - * @param {string} owner Owner of the repo - * @param {string} repo Name of the repo - * @param {RepoSettings} settings Settings to update - * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/update.go - */ - updateRepo (owner, repo, settings) { - Joi.assert(owner, Joi.string().required(), 'Must specify owner') - Joi.assert(repo, Joi.string().required(), 'Must specify repo') - Joi.assert(settings, RepoSettings, 'Must specify settings') - - return this._axios.patch( - `/api/repos/${owner}/${repo}`, - settings - ) - } - /** * Incomplete builds */ @@ -933,6 +798,8 @@ class Client { } } +Object.setPrototypeOf(Client.prototype, Repositories.prototype) + module.exports = { Client, RetryBuild, diff --git a/lib/resources/repositories.js b/lib/resources/repositories.js new file mode 100644 index 0000000..9e4ddfc --- /dev/null +++ b/lib/resources/repositories.js @@ -0,0 +1,156 @@ +const Joi = require('@hapi/joi') +const Querystring = require('querystring') +const { + RepoSettings, + SelfRepos +} = require('../validations') + +class Repositories { + /** + * @constructor + * @param {AxiosInstance} webClient webclient + */ + constructor (webClient) { + this._axios = webClient + } + + /** + * Sync repos + * @param {SyncRepos} params Filter parameters + */ + syncRepos (params) { + return this._axios.post( + '/api/user/repos', + Querystring.stringify(params) + ) + } + + /** + * Self repos + * @param {SelfRepos} params Filter parameters + */ + selfRepos (params) { + Joi.assert(params, SelfRepos, 'Specify valid params') + + return this._axios.get( + '/api/user/repos', { + params: params + }) + } + + /** + * Get repos + * @param {integer} page Page number + * @param {integer} limit Page limit + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/all.go + */ + getRepos (page = 1, limit = 10000) { + return this._axios.get( + '/api/repos', { + params: { + page: page, + per_page: limit + } + }) + } + + /** + * Get repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/find.go + */ + getRepo (owner, repo) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + + return this._axios.get( + `/api/repos/${owner}/${repo}` + ) + } + + /** + * Enable repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/enable.go + */ + enableRepo (owner, repo) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + + return this._axios.post( + `/api/repos/${owner}/${repo}` + ) + } + + /** + * Disable repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/disable.go + */ + disableRepo (owner, repo, remove = false) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + Joi.assert(remove, Joi.boolean().required(), 'Must specify remove') + + return this._axios.delete( + `/api/repos/${owner}/${repo}`, { + params: { + remove: remove + } + }) + } + + /** + * Chown repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + */ + chownRepo (owner, repo) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + + return this._axios.post( + `/api/repos/${owner}/${repo}/chown` + ) + } + + /** + * Repair repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/repair.go + */ + repairRepo (owner, repo) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + + return this._axios.post( + `/api/repos/${owner}/${repo}/repair` + ) + } + + /** + * Update repo + * @param {string} owner Owner of the repo + * @param {string} repo Name of the repo + * @param {RepoSettings} settings Settings to update + * Ref: https://github.com/harness/drone/blob/v2.11.1/handler/api/repos/update.go + */ + updateRepo (owner, repo, settings) { + Joi.assert(owner, Joi.string().required(), 'Must specify owner') + Joi.assert(repo, Joi.string().required(), 'Must specify repo') + Joi.assert(settings, RepoSettings, 'Must specify settings') + + return this._axios.patch( + `/api/repos/${owner}/${repo}`, + settings + ) + } +} + +module.exports = { + Repositories +}