Skip to content

Commit

Permalink
Move resources from index
Browse files Browse the repository at this point in the history
  • Loading branch information
unflores committed Apr 30, 2022
1 parent ba72f42 commit 74ea7e8
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 136 deletions.
139 changes: 3 additions & 136 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -933,6 +798,8 @@ class Client {
}
}

Object.setPrototypeOf(Client.prototype, Repositories.prototype)

module.exports = {
Client,
RetryBuild,
Expand Down
156 changes: 156 additions & 0 deletions lib/resources/repositories.js
Original file line number Diff line number Diff line change
@@ -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.webClient = 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
}

0 comments on commit 74ea7e8

Please sign in to comment.