diff --git a/lib/index.js b/lib/index.js index 7799ceb..41c0df1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,18 @@ const Joi = require('@hapi/joi') const Axios = require('axios') const Querystring = require('querystring') +const { Repositories } = require('./resources/repositories') +const { + RetryBuild, + User, + LatestBuild, + TriggerBuild, + RepoSettings, + Secret, + Cron, + SelfRepos, + SyncRepos +} = require('./validations') /** * Drone client @@ -60,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 @@ -84,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 */ @@ -922,72 +798,7 @@ 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() -}) +Object.setPrototypeOf(Client.prototype, Repositories.prototype) module.exports = { Client, 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 +} 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 +}