From af4eb6955f583b5be4a4032d2d532d81bb2cf54d Mon Sep 17 00:00:00 2001 From: Justin Dalrymple Date: Thu, 20 Jul 2017 15:29:57 -0400 Subject: [PATCH] See Changelog: --- .eslintrc | 24 ++-- README.md | 8 ++ package.json | 2 +- src/API.js | 42 +++---- src/BaseModel.js | 10 +- src/Models/Groups.js | 181 +++++++++++++++-------------- src/Models/Issues.js | 7 +- src/Models/Labels.js | 33 +++--- src/Models/ProjectDeployKeys.js | 18 ++- src/Models/ProjectHooks.js | 34 +++--- src/Models/ProjectIssueNotes.js | 41 +++---- src/Models/ProjectIssues.js | 34 +++--- src/Models/ProjectLabels.js | 11 +- src/Models/ProjectMembers.js | 32 +++-- src/Models/ProjectMergeRequests.js | 46 ++++---- src/Models/ProjectMilestones.js | 26 +++-- src/Models/ProjectPipelines.js | 10 +- src/Models/ProjectRepository.js | 88 +++++++++----- src/Models/ProjectRunners.js | 22 ++-- src/Models/ProjectServices.js | 18 +-- src/Models/ProjectTriggers.js | 24 ++-- src/Models/Projects.js | 56 +++++---- src/Models/Runners.js | 18 ++- src/Models/UserKeys.js | 15 +-- src/Models/Users.js | 19 +-- src/Models/index.js | 2 +- src/Utils.js | 21 ++-- 27 files changed, 446 insertions(+), 396 deletions(-) diff --git a/.eslintrc b/.eslintrc index 4ec973f3..b3167f9f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,11 +1,17 @@ { - "parser": "babel-eslint", - "env": { - "browser": true, - "node": true - }, - "extends": "airbnb", - rules:{ - arrow-body-style: [2, "as-needed"] - } + "parser": "babel-eslint", + "env": + { + "browser": true, + "node": true + }, + "extends": "airbnb", + "rules": { + "arrow-body-style": [2, "as-needed"], + "no-param-reassign": [ + "error", { + "props": false + } + ] + } } \ No newline at end of file diff --git a/README.md b/README.md index d57307f8..d4e526d2 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,14 @@ MIT Changelog ========= +[1.0.11](https://github.com/jdalrymple/node-gitlab-api/commit/) (2017-07-20) +------------------ +- Fixing the problem where Id was used instead of IId's for Project issues +- Fixing the naming convention for Project Issues +- Standadized the use of parseInt in the codebase +- Removed instances of duplicate code found by code climate + + [1.0.10](https://github.com/jdalrymple/node-gitlab-api/commit/c4a55aba89d83fda1552b3d5688b090b0c2b60aa) (2017-07-13) ------------------ - Fixing Issues #1, #2, and #3 diff --git a/package.json b/package.json index a956e798..35fee00b 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "devDependencies": { "babel-eslint": "^7.1.1", "babel-preset-latest": "^6.16.0", - "eslint": "^3.14.0", + "eslint": "^3.19.0", "eslint-config-airbnb": "^14.0.0", "eslint-plugin-import": "^2.2.0", "eslint-plugin-jsx-a11y": "^3.0.2", diff --git a/src/API.js b/src/API.js index 3669085b..30b14431 100644 --- a/src/API.js +++ b/src/API.js @@ -1,17 +1,35 @@ const Request = require('request-promise'); const { Groups, Projects, Issues, Runners, Users, Labels } = require('./Models'); +function defaultRequestWithQS(url, endpoint, headers, options) { + return { + url: url + endpoint, + headers, + json: true, + qs: options, + }; +} + +function defaultRequestWithBody(url, endpoint, headers, options) { + return { + url: url + endpoint, + headers, + json: true, + body: options, + }; +} + class API { constructor({ url = 'https://gitlab.com', token, oauthToken }) { this.url = `${url}/api/v4/`; - this.headers = {} + this.headers = {}; if (oauthToken) { this.headers.Authorization = `Bearer ${oauthToken}`; } else if (token) { this.headers['private-token'] = token; } else { - throw "`token` (private-token) or `oauth_token` is mandatory" + throw new Error('`token` (private-token) or `oauth_token` is mandatory'); } this.groups = new Groups(this); @@ -39,22 +57,4 @@ class API { } } -function defaultRequestWithQS(url, endpoint, headers, options) { - return { - url: url + endpoint, - headers: headers, - json: true, - qs: options - } -} - -function defaultRequestWithBody(url, endpoint, headers, options) { - return { - url: url + endpoint, - headers: headers, - json: true, - body: options - } -} - -module.exports = API; \ No newline at end of file +module.exports = API; diff --git a/src/BaseModel.js b/src/BaseModel.js index 1c975a5e..69a739ea 100644 --- a/src/BaseModel.js +++ b/src/BaseModel.js @@ -1,21 +1,21 @@ class BaseModel { - constructor(APIClient){ + constructor(APIClient) { this.client = APIClient; } - get(endpoint, options){ + get(endpoint, options) { return this.client.get(endpoint, options); } - post(endpoint, options){ + post(endpoint, options) { return this.client.post(endpoint, options); } - put(endpoint, options){ + put(endpoint, options) { return this.client.put(endpoint, options); } - delete(endpoint, options){ + delete(endpoint, options) { return this.client.delete(endpoint, options); } } diff --git a/src/Models/Groups.js b/src/Models/Groups.js index 4dd3d035..c0251c6a 100644 --- a/src/Models/Groups.js +++ b/src/Models/Groups.js @@ -1,104 +1,105 @@ const BaseModel = require('../BaseModel'); -const Utils = require('utils'); +const Utils = require('../Utils'); -class Groups extends BaseModel { - constructor(...args){ - super(...args); - this.access_levels = { - GUEST: 10, - REPORTER: 20, - DEVELOPER: 30, - MASTER: 40, - OWNER: 50 - } - } - - all(options = {}) { - Utils.defaultPaging(options); - - return this.get("groups", options); - } - - show(groupId) { - groupId = Utils.parse(groupId); - - return this.get(`groups/${groupId}`); - } - - listMembers(groupId) { - groupId = Utils.parse(groupId); - - return this.get(`groups/${groupId}/members`); - } - - addMember(groupId, userId, accessLevel) { - hasAccess.call(this); - - [groupId,userId] = [groupId,userId].map(Utils.parse); - - return this.post(`groups/${groupId}/members`, { - user_id: userId, - access_level: accessLevel - }); - } - - editMember(groupId, userId, accessLevel) { - hasAccess.call(this); - - [groupId,userId] = [groupId,userId].map(Utils.parse); - - return this.put(`groups/${groupId}/members/${userId}`, { - access_level: accessLevel - }); - } - - removeMember(groupId, userId) { - [groupId,userId] = Array.from(arguments).map(Utils.parse); - - return this.delete(`groups/${groupId}/members/${userId}`); - } - - create(options = {}) { - return this.post("groups", options); - } - - listProjects(groupId) { - groupId = Utils.parse(groupId); - - return this.get(`groups/${groupId}/projects`); - } - - addProject(groupId, projectId) { - [groupId,projectId] = Array.from(arguments).map(Utils.parse); - - return this.post(`groups/${groupId}/projects/${projectId}`); - } - - deleteGroup(groupId) { - groupId = Utils.parse(groupId); - - return this.delete(`groups/${groupId}`); - } - - search(nameOrPath) { - return this.get("groups", options, { - search: nameOrPath - }); - } -} - -function hasAccess(){ +function hasAccess() { let access_level; let hasAccess; for (let k in this.access_levels) { access_level = this.access_levels[k]; - if (accessLevel === access_level) { - hasAccess = true; + if (accessLevel === access_level) { + hasAccess = true; } } if (!hasAccess) throw `\`accessLevel\` must be one of ${JSON.stringify(this.access_levels)}`; } +class Groups extends BaseModel { + constructor(...args) { + super(...args); + + this.access_levels = { + GUEST: 10, + REPORTER: 20, + DEVELOPER: 30, + MASTER: 40, + OWNER: 50, + }; + } + + all(options = {}) { + Utils.defaultPaging(options); + + return this.get('groups', options); + } + + show(groupId) { + const gId = Utils.parse(groupId); + + return this.get(`groups/${gId}`); + } + + listMembers(groupId) { + const gId = Utils.parse(groupId); + + return this.get(`groups/${gId}/members`); + } + + addMember(groupId, userId, accessLevel) { + const [gId, uId] = [groupId, userId].map(Utils.parse); + + hasAccess.call(this); + + return this.post(`groups/${gId}/members`, { + user_id: uId, + access_level: accessLevel, + }); + } + + editMember(groupId, userId, accessLevel) { + const [gId, uId] = [groupId, userId].map(Utils.parse); + + hasAccess.call(this); + + return this.put(`groups/${gId}/members/${uId}`, { + access_level: accessLevel, + }); + } + + removeMember(groupId, userId) { + const [gId, uId] = [groupId, userId].map(Utils.parse); + + return this.delete(`groups/${gId}/members/${uId}`); + } + + create(options = {}) { + return this.post('groups', options); + } + + listProjects(groupId) { + const gId = Utils.parse(groupId); + + return this.get(`groups/${gId}/projects`); + } + + addProject(groupId, projectId) { + const [gId, pId] = [groupId, projectId].map(Utils.parse); + + return this.post(`groups/${gId}/projects/${pId}`); + } + + deleteGroup(groupId) { + const gId = Utils.parse(groupId); + + return this.delete(`groups/${gId}`); + } + + search(nameOrPath) { + return this.get('groups', { + search: nameOrPath, + }); + } +} + module.exports = Groups; diff --git a/src/Models/Issues.js b/src/Models/Issues.js index 44c328cf..8254013b 100644 --- a/src/Models/Issues.js +++ b/src/Models/Issues.js @@ -2,16 +2,11 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class Issues extends BaseModel { - constructor(...args) { - super(...args); - } - all(options = {}) { Utils.defaultPaging(options); - return this.get("issues", options); + return this.get('issues', options); } } - module.exports = Issues; diff --git a/src/Models/Labels.js b/src/Models/Labels.js index 36929775..63dde70a 100644 --- a/src/Models/Labels.js +++ b/src/Models/Labels.js @@ -2,47 +2,44 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class Labels extends BaseModel { - constructor(...args) { - super(...args); - } - all(projectId, options = {}) { + const pId = Utils.parse(projectId); + Utils.defaultPaging(options); - projectId = Utils.parse(projectId); - - return this.get(`projects/${projectId}/labels`, options); + return this.get(`projects/${pId}/labels`, options); } create(projectId, options = {}) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.post(`projects/${projectId}/labels`, options); + return this.post(`projects/${pId}/labels`, options); } edit(projectId, labelName, options = {}) { - projectId = Utils.parse(projectId); - options.name = labelName + const pId = Utils.parse(projectId); - return this.put(`projects/${projectId}/labels`, options); + options.name = labelName; + + return this.put(`projects/${pId}/labels`, options); } remove(projectId, labelName) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.delete(`projects/${projectId}/labels`, { name: labelName }); + return this.delete(`projects/${pId}/labels`, { name: labelName }); } subscribe(projectId, labelId, options = {}) { - [projectId, labelId] = [projectId, labelId].map(Utils.parse); + const [pId, lId] = [projectId, labelId].map(Utils.parse); - return this.post(`projects/${projectId}/issues/${labelId}/subscribe`, options); + return this.post(`projects/${pId}/issues/${lId}/subscribe`, options); } unsubscribe(projectId, labelId) { - [projectId, labelId] = [projectId, labelId].map(Utils.parse); + const [pId, lId] = [projectId, labelId].map(Utils.parse); - return this.delete(`projects/${projectId}/issues/${labelId}/unsubscribe`); + return this.delete(`projects/${pId}/issues/${lId}/unsubscribe`); } } diff --git a/src/Models/ProjectDeployKeys.js b/src/Models/ProjectDeployKeys.js index 95724ab5..ebd0527d 100644 --- a/src/Models/ProjectDeployKeys.js +++ b/src/Models/ProjectDeployKeys.js @@ -2,27 +2,23 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectKeys extends BaseModel { - constructor(...args) { - super(...args); - } - listKeys(projectId) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.get(`projects/${projectId}/deploy_keys`); + return this.get(`projects/${pId}/deploy_keys`); } getKey(projectId, keyId) { - [groupId, keyId] = Array.from(arguments).map(Utils.parse); + const [pId, kId] = [projectId, keyId].map(Utils.parse); - return this.get(`projects/${projectId}/deploy_keys/${keyId}`); + return this.get(`projects/${pId}/deploy_keys/${kId}`); } addKey(projectId, options = {}) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.post(`projects/${projectId}/deploy_keys`, options); + return this.post(`projects/${pId}/deploy_keys`, options); } } -module.exports = ProjectKeys; \ No newline at end of file +module.exports = ProjectKeys; diff --git a/src/Models/ProjectHooks.js b/src/Models/ProjectHooks.js index 8db79afc..6c775543 100644 --- a/src/Models/ProjectHooks.js +++ b/src/Models/ProjectHooks.js @@ -2,42 +2,36 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectHooks extends BaseModel { - constructor(...args) { - super(...args); - } - list(projectId) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.get(`projects/${projectId}/hooks`); + return this.get(`projects/${pId}/hooks`); } show(projectId, hookId) { - [projectId, hookId] = Array.from(arguments).map(Utils.parse); + const [pId, hId] = [projectId, hookId].map(Utils.parse); - return this.get(`projects/${projectId}/hooks/${hookId}`); + return this.get(`projects/${pId}/hooks/${hId}`); } - add(projectId, options) { - if (typeof options === 'string') options = { url: options }; - projectId = Utils.parse(projectId); + add(projectId, url, options = {}) { + options.url = url; + const pId = Utils.parse(projectId); - return this.post(`projects/${projectId}/hooks`, options); + return this.post(`projects/${pId}/hooks`, options); } - edit(projectId, hookId, url) { - [projectId, hookId] = [projectId, hookId].map(Utils.parse); + edit(projectId, hookId, url, options) { + const [pId, hId] = [projectId, hookId].map(Utils.parse); - return this.put(`projects/${projectId}/hooks/${hookId}`, { - access_level: parseInt(accessLevel) - }); + return this.put(`projects/${pId}/hooks/${hId}`, options); } remove(projectId, hookId) { - [projectId, hookId] = Array.from(arguments).map(Utils.parse); + const [pId, hId] = [projectId, hookId].map(Utils.parse); - return this.delete(`projects/${projectId}/hooks/${hookId}`); + return this.delete(`projects/${pId}/hooks/${hId}`); } } -module.exports = ProjectHooks; \ No newline at end of file +module.exports = ProjectHooks; diff --git a/src/Models/ProjectIssueNotes.js b/src/Models/ProjectIssueNotes.js index fbf7dd92..0eae5a5d 100644 --- a/src/Models/ProjectIssueNotes.js +++ b/src/Models/ProjectIssueNotes.js @@ -1,45 +1,42 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); -class ProjectNotes extends BaseModel { - constructor(...args) { - super(...args); - } +class ProjectIssueNotes extends BaseModel { + all(projectId, issueIId, options = {}) { + const [pId, iIId] = [projectId, issueIId].map(Utils.parse); - all(projectId, issueId, options = {}) { Utils.defaultPaging(options); - [projectId, issueId] = [projectId, issueId].map(Utils.parse); - return this.get(`projects/${projectId}/issues/${issueId}/notes`, options); + return this.get(`projects/${pId}/issues/${iIId}/notes`, options); } - create(projectId, issueId, options = {}) { - if(!options.body) throw new Error('Missing required property: body'); + create(projectId, issueIId, options = {}) { + if (!options.body) throw new Error('Missing required property: body'); - [projectId, issueId] = [projectId, issueId].map(Utils.parse); + const [pId, iIId] = [projectId, issueIId].map(Utils.parse); - return this.post(`projects/${projectId}/issues/${issueId}/notes`, options); + return this.post(`projects/${pId}/issues/${iIId}/notes`, options); } - edit(projectId, issueId, noteId, options = {}) { - if(!options.body) throw new Error('Missing required property: body'); + edit(projectId, issueIId, noteId, options = {}) { + if (!options.body) throw new Error('Missing required property: body'); - [projectId, issueId, noteId] = [projectId, issueId, noteId].map(Utils.parse) + const [pId, iIId, nId] = [projectId, issueIId, noteId].map(Utils.parse); - return this.put(`projects/${projectId}/issues/${issueId}/notes/${noteId}`, options); + return this.put(`projects/${pId}/issues/${iIId}/notes/${nId}`, options); } - remove(projectId, issueId, nodeId) { - [projectId, issueId, noteId] = Array.from(arguments).map(Utils.parse) + remove(projectId, issueIId, noteId) { + const [pId, iIId, nId] = [projectId, issueIId, noteId].map(Utils.parse); - return this.delete(`projects/${projectId}/issues/${issueId}/notes/${noteId}`); + return this.delete(`projects/${pId}/issues/${iIId}/notes/${nId}`); } - show(projectId, issueId, noteId) { - [projectId, issueId, noteId] = Array.from(arguments).map(Utils.parse) + show(projectId, issueIId, noteId) { + const [pId, iIId, nId] = [projectId, issueIId, noteId].map(Utils.parse); - return this.get(`projects/${projectId}/issues/${issueId}/notes/${noteId}`); + return this.get(`projects/${pId}/issues/${iIId}/notes/${nId}`); } } -module.exports = ProjectNotes; +module.exports = ProjectIssueNotes; diff --git a/src/Models/ProjectIssues.js b/src/Models/ProjectIssues.js index 785b2844..2a69847e 100644 --- a/src/Models/ProjectIssues.js +++ b/src/Models/ProjectIssues.js @@ -6,53 +6,51 @@ class ProjectIssues extends BaseModel { constructor(...args) { super(...args); - this.notes = ProjectIssueNotes; + this.notes = new ProjectIssueNotes(...args); } all(projectId, options = {}) { + const pId = Utils.parse(projectId); + Utils.defaultPaging(options); - projectId = Utils.parse(projectId); - - return this.get(`projects/${projectId}/issues`, options); + return this.get(`projects/${pId}/issues`, options); } create(projectId, options = {}) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.post(`projects/${projectId}/issues`, options); + return this.post(`projects/${pId}/issues`, options); } edit(projectId, issueId, options = {}) { - [projectId, issueId] = [projectId, issueId].map(Utils.parse) + const [pId, iId] = [projectId, issueId].map(Utils.parse); - return this.put(`projects/${projectId}/issues/${issueId}`, options); + return this.put(`projects/${pId}/issues/${iId}`, options); } remove(projectId, issueId) { - [projectId, issueId] = Array.from(arguments).map(Utils.parse) + const [pId, iId] = [projectId, issueId].map(Utils.parse); - - return this.delete(`projects/${projectId}/issues/${issueId}`); + return this.delete(`projects/${pId}/issues/${iId}`); } show(projectId, issueId) { - [projectId, issueId] = Array.from(arguments).map(Utils.parse) + const [pId, iId] = [projectId, issueId].map(Utils.parse); - - return this.get(`projects/${projectId}/issues/${issueId}`); + return this.get(`projects/${pId}/issues/${iId}`); } subscribe(projectId, issueId, options = {}) { - [projectId, issueId] = Array.from(arguments).map(Utils.parse) + const [pId, iId] = [projectId, issueId].map(Utils.parse); - return this.post(`projects/${projectId}/issues/${issueId}/subscribe`, options); + return this.post(`projects/${pId}/issues/${iId}/subscribe`, options); } unsubscribe(projectId, issueId) { - [projectId, issueId] = Array.from(arguments).map(Utils.parse) + const [pId, iId] = [projectId, issueId].map(Utils.parse); - return this.delete(`projects/${projectId}/issues/${issueId}/unsubscribe`); + return this.delete(`projects/${pId}/issues/${iId}/unsubscribe`); } } diff --git a/src/Models/ProjectLabels.js b/src/Models/ProjectLabels.js index 5c483bbe..67debe2a 100644 --- a/src/Models/ProjectLabels.js +++ b/src/Models/ProjectLabels.js @@ -2,15 +2,12 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectLabels extends BaseModel { - constructor(...args) { - super(...args); - } - all(projectId, options = {}) { - Utils.defaultPaging(options); - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.get(`projects/${projectId}/labels`, options); + Utils.defaultPaging(options); + + return this.get(`projects/${pId}/labels`, options); } } diff --git a/src/Models/ProjectMembers.js b/src/Models/ProjectMembers.js index 3840b787..d3c00ca3 100644 --- a/src/Models/ProjectMembers.js +++ b/src/Models/ProjectMembers.js @@ -2,44 +2,40 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectMembers extends BaseModel { - constructor(...args) { - super(...args); - } - list(projectId) { - projectId = Utils.parse(projectId); + const pId = Utils.parse(projectId); - return this.get(`projects/${projectId}/members`); + return this.get(`projects/${pId}/members`); } show(projectId, userId) { - [projectId, userId] = Array.from(arguments).map(Utils.parse) + const [pId, uId] = [projectId, userId].map(Utils.parse); - return this.get(`projects/${projectId}/members/${userId}`); + return this.get(`projects/${pId}/members/${uId}`); } add(projectId, userId, accessLevel = 30) { - [projectId, userId] = [projectId, userId].map(Utils.parse) + const [pId, uId] = [projectId, userId].map(Utils.parse); - return this.post(`projects/${projectId}/members`, { - user_id: userId, - access_level: parseInt(accessLevel) + return this.post(`projects/${pId}/members`, { + user_id: uId, + access_level: parseInt(accessLevel, 10), }); } edit(projectId, userId, accessLevel = 30) { - [projectId, userId] = [projectId, userId].map(Utils.parse) + const [pId, uId] = [projectId, userId].map(Utils.parse); - return this.put(`projects/${projectId}/members/${userId}`, { - access_level: parseInt(accessLevel) + return this.put(`projects/${pId}/members/${uId}`, { + access_level: parseInt(accessLevel, 10), }); } remove(projectId, userId) { - [projectId, issueId] = Array.from(arguments).map(Utils.parse) + const [pId, uId] = [projectId, userId].map(Utils.parse); - return this.delete(`projects/${projectId}/members/${userId}`); + return this.delete(`projects/${pId}/members/${uId}`); } } -module.exports = ProjectMembers; \ No newline at end of file +module.exports = ProjectMembers; diff --git a/src/Models/ProjectMergeRequests.js b/src/Models/ProjectMergeRequests.js index 0a8aa495..43bd44c5 100644 --- a/src/Models/ProjectMergeRequests.js +++ b/src/Models/ProjectMergeRequests.js @@ -2,57 +2,51 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectMergeRequests extends BaseModel { - constructor(...args) { - super(...args); - } - list(projectId, options = {}) { + const pId = Utils.parse(projectId); + Utils.defaultPaging(options); - projectId = Utils.parse(projectId); - - return this.get(`projects/${Utils.parse(projectId)}/merge_requests`, options); + return this.get(`projects/${pId}/merge_requests`, options); } - show(projectId, mergeRequestId) { - [projectId, hookId] = Array.from(arguments).map(Utils.parse); + show(projectId, mergerequestId) { + const [pId, mId] = [projectId, mergerequestId].map(Utils.parse); - return this.get(`projects/${projectId}/merge_requests/${mergerequestId}`); + return this.get(`projects/${pId}/merge_requests/${mId}`); } add(projectId, sourceBranch, targetBranch, assigneeId, title) { - [projectId, assigneeId] = [projectId, assigneeId].map(Utils.parse); + const [pId, aId] = [projectId, assigneeId].map(Utils.parse); - let options = { - id: projectId, + const options = { + id: pId, source_branch: sourceBranch, target_branch: targetBranch, - title + title, }; - if (assigneeId !== undefined) options.assigneeId = assigneeId; + if (assigneeId !== undefined) options.assigneeId = aId; - return this.post(`projects/${projectId}/merge_requests`, options); + return this.post(`projects/${pId}/merge_requests`, options); } update(projectId, mergerequestId, options = {}) { - [projectId, mergerequestId] = [projectId, mergerequestId].map(Utils.parse); + const [pId, mId] = [projectId, mergerequestId].map(Utils.parse); - options.id = projectId; - options.merge_request_id = mergerequestId; + options.id = pId; + options.merge_request_id = mId; - return this.put(`projects/${projectId}/merge_requests/${mergerequestId}`, options); + return this.put(`projects/${pId}/merge_requests/${mId}`, options); } comment(projectId, mergerequestId, note) { - [projectId, mergerequestId] = [projectId, mergerequestId].map(Utils.parse); + const [pId, mId] = [projectId, mergerequestId].map(Utils.parse); - return this.post(`projects/${projectId}/merge_requests/${mergerequestId}/comments`, { - id: projectId, - merge_request_id: mergerequestId, - note + return this.post(`projects/${pId}/merge_requests/${mId}/comments`, { + body: note, }); } } -module.exports = ProjectMergeRequests; \ No newline at end of file +module.exports = ProjectMergeRequests; diff --git a/src/Models/ProjectMilestones.js b/src/Models/ProjectMilestones.js index e6a935c8..3ebb2154 100644 --- a/src/Models/ProjectMilestones.js +++ b/src/Models/ProjectMilestones.js @@ -2,36 +2,38 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectMilestones extends BaseModel { - constructor(...args) { - super(...args); - } - all(projectId, options = {}) { + const pId = Utils.parse(projectId); + Utils.defaultPaging(options); - return this.get(`projects/${Utils.parse(projectId)}/milestones`, options); + return this.get(`projects/${pId}/milestones`, options); } show(projectId, milestoneId) { - return this.get(`projects/${Utils.parse(projectId)}/milestones/${parseInt(milestoneId)}`); + const [pId, mId] = [projectId, milestoneId].map(Utils.parse); + + return this.get(`projects/${pId}/milestones/${mId}`); } add(projectId, title, { description, due_date }) { - return this.post(`projects/${Utils.parse(projectId)}/milestones`, { - id: Utils.parse(projectId), + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/milestones`, { title, description, - due_date + due_date, }); } update(projectId, milestoneId, { title, description, due_date, state_event }) { - return this.put(`projects/${Utils.parse(projectId)}/milestones/${parseInt(milestoneId)}`, { - id: Utils.parse(projectId), + const [pId, mId] = [projectId, milestoneId].map(Utils.parse); + + return this.put(`projects/${pId}/milestones/${mId}`, { title, description, due_date, - state_event + state_event, }); } } diff --git a/src/Models/ProjectPipelines.js b/src/Models/ProjectPipelines.js index cda1e94c..b22893ac 100644 --- a/src/Models/ProjectPipelines.js +++ b/src/Models/ProjectPipelines.js @@ -2,13 +2,11 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class Pipelines extends BaseModel { - constructor(...args) { - super(...args); - } - all(projectId) { - return this.get(`projects/${Utils.parse(projectId)}/pipelines`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/pipelines`); } } -module.exports = Pipelines +module.exports = Pipelines; diff --git a/src/Models/ProjectRepository.js b/src/Models/ProjectRepository.js index 3f51c5fd..7a16289b 100644 --- a/src/Models/ProjectRepository.js +++ b/src/Models/ProjectRepository.js @@ -2,84 +2,118 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectRepository extends BaseModel { - constructor(...args) { - super(...args); - } - listBranches(projectId) { - return this.get(`projects/${Utils.parse(projectId)}/repository/branches`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/branches`); } showBranch(projectId, branchId) { - return this.get(`projects/${Utils.parse(projectId)}/repository/branches/${encodeURI(branchId)}`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/branches/${encodeURI(branchId)}`); } protectBranch(projectId, branchId) { - return this.put(`projects/${Utils.parse(projectId)}/repository/branches/${encodeURI(branchId)}/protect`); + const pId = Utils.parse(projectId); + + return this.put(`projects/${pId}/repository/branches/${encodeURI(branchId)}/protect`); } unprotectBranch(projectId, branchId) { - return this.put(`projects/${Utils.parse(projectId)}/repository/branches/${encodeURI(branchId)}/unprotect`); + const pId = Utils.parse(projectId); + + return this.put(`projects/${pId}/repository/branches/${encodeURI(branchId)}/unprotect`); } - createBranch(options = {}) { - return this.post(`projects/${Utils.parse(options.projectId)}/repository/branches`, options); + createBranch(projectId, options = {}) { + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/repository/branches`, options); } deleteBranch(projectId, branchId) { - return this.delete(`projects/${Utils.parse(projectId)}/repository/branches/${encodeURI(branchId)}`); - } + const pId = Utils.parse(projectId); - addTag(options = {}) { - return this.post(`projects/${Utils.parse(options.id)}/repository/tags`, options); + return this.delete(`projects/${pId}/repository/branches/${encodeURI(branchId)}`); + } + + addTag(projectId, options = {}) { + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/repository/tags`, options); } deleteTag(projectId, tagName) { - return this.delete(`projects/${Utils.parse(projectId)}/repository/tags/${encodeURI(tagName)}`); + const pId = Utils.parse(projectId); + + return this.delete(`projects/${pId}/repository/tags/${encodeURI(tagName)}`); } showTag(projectId, tagName) { - return this.get(`projects/${Utils.parse(projectId)}/repository/tags/${encodeURI(tagName)}`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/tags/${encodeURI(tagName)}`); } listTags(projectId) { - return this.get(`projects/${Utils.parse(projectId)}/repository/tags`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/tags`); } listCommits(projectId) { - return this.get(`projects/${Utils.parse(projectId)}/repository/commits`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/commits`); } showCommit(projectId, sha) { - return this.get(`projects/${Utils.parse(projectId)}/repository/commits/${sha}`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/commits/${sha}`); } diffCommit(projectId, sha) { - return this.get(`projects/${Utils.parse(projectId)}/repository/commits/${sha}/diff`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/commits/${sha}/diff`); } - listTree(projectId, options = {}) { - return this.get(`projects/${Utils.parse(projectId)}/repository/tree`, options); + listTree(projectId, options = {}) { + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/tree`, options); } showFile(projectId, options = {}) { + const pId = Utils.parse(projectId); + if (options.file_path && options.ref) { - return this.get(`projects/${Utils.parse(projectId)}/repository/files`, options); + return this.get(`projects/${pId}/repository/files`, options); } else if (options.file_path && options.file_id) { - return this.get(`projects/${Utils.parse(projectId)}/repository/raw_blobs/` + options.file_id, options); + return this.get(`projects/${pId}/repository/raw_blobs/{options.file_id}`, options); } + + return null; } createFile(projectId, options = {}) { - return this.post(`projects/${Utils.parse(projectId)}/repository/files`, options); + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/repository/files`, options); } updateFile(projectId, options = {}) { - return this.put(`projects/${Utils.parse(projectId)}/repository/files`, options); + const pId = Utils.parse(projectId); + + return this.put(`projects/${pId}/repository/files`, options); } compare(projectId, options = {}) { - return this.get(`projects/${Utils.parse(projectId)}/repository/compare`, options); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/repository/compare`, options); } } diff --git a/src/Models/ProjectRunners.js b/src/Models/ProjectRunners.js index 043cb830..2682ee2d 100644 --- a/src/Models/ProjectRunners.js +++ b/src/Models/ProjectRunners.js @@ -2,26 +2,28 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectRunners extends BaseModel { - constructor(...args) { - super(...args); - } - all(projectId, options = {}) { + const pId = Utils.parse(projectId); + if (projectId != null) { - return this.get(`projects/${Utils.parse(projectId)}/runners`, options); - } else { - return this.get("runners", options); + return this.get(`projects/${pId}/runners`, options); } + + return this.get('runners', options); } enable(projectId, runnerId) { - return this.post(`projects/${Utils.parse(projectId)}/runners`, { - runner_id: parseInt(runnerId) + const [pId, rId] = [projectId, runnerId].map(Utils.parse); + + return this.post(`projects/${pId}/runners`, { + runner_id: rId, }); } disable(projectId, runnerId) { - return this.delete(`projects/${Utils.parse(projectId)}/runners/${parseInt(runnerId)}`); + const [pId, rId] = [projectId, runnerId].map(Utils.parse); + + return this.delete(`projects/${pId}/runners/${rId}`); } } diff --git a/src/Models/ProjectServices.js b/src/Models/ProjectServices.js index b6c19719..ce2a7f13 100644 --- a/src/Models/ProjectServices.js +++ b/src/Models/ProjectServices.js @@ -2,20 +2,22 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectServices extends BaseModel { - constructor(...args) { - super(...args); - } - show(projectId, serviceName) { - return this.get(`projects/${Utils.parse(projectId)}/services/${serviceName}`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/services/${serviceName}`); } - update(projectId, serviceName, params = {}) { - return this.put(`projects/${Utils.parse(projectId)}/services/${serviceName}`, params); + update(projectId, serviceName, options = {}) { + const pId = Utils.parse(projectId); + + return this.put(`projects/${pId}/services/${serviceName}`, options); } remove(projectId, serviceName) { - return this.delete(`projects/${Utils.parse(projectId)}/services/${serviceName}`); + const pId = Utils.parse(projectId); + + return this.delete(`projects/${pId}/services/${serviceName}`); } } diff --git a/src/Models/ProjectTriggers.js b/src/Models/ProjectTriggers.js index 0fca90ed..9d521441 100644 --- a/src/Models/ProjectTriggers.js +++ b/src/Models/ProjectTriggers.js @@ -2,28 +2,34 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class ProjectTriggers extends BaseModel { - constructor(...args) { - super(...args); - } - add(projectId, options = {}) { - return this.post(`projects/${Utils.parse(projectId)}/triggers`, options); + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/triggers`, options); } edit(projectId, triggerId, options = {}) { - return this.put(`projects/${Utils.parse(projectId)}/triggers/${Utils.parse(triggerId)}`, options); + const [pId, tId] = [projectId, triggerId].map(Utils.parse); + + return this.put(`projects/${pId}/triggers/${tId}`, options); } list(projectId) { - return this.get(`projects/${Utils.parse(projectId)}/triggers`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}/triggers`); } remove(projectId, triggerId) { - return this.delete(`projects/${Utils.parse(projectId)}/triggers/${Utils.parse(triggerId)}`); + const [pId, tId] = [projectId, triggerId].map(Utils.parse); + + return this.delete(`projects/${pId}/triggers/${tId}`); } show(projectId, triggerId) { - return this.get(`projects/${Utils.parse(projectId)}/triggers/${Utils.parse(triggerId)}`); + const [pId, tId] = [projectId, triggerId].map(Utils.parse); + + return this.get(`projects/${pId}/triggers/${tId}`); } } diff --git a/src/Models/Projects.js b/src/Models/Projects.js index 9b15e94f..0c0c59ea 100644 --- a/src/Models/Projects.js +++ b/src/Models/Projects.js @@ -12,8 +12,6 @@ const ProjectServices = require('./ProjectServices'); const ProjectTriggers = require('./ProjectTriggers'); const ProjectRunners = require('./ProjectRunners'); const ProjectPipelines = require('./ProjectPipelines'); -const Runners = require('./Runners'); - class Projects extends BaseModel { constructor(...args) { @@ -36,63 +34,81 @@ class Projects extends BaseModel { all(options = {}) { Utils.defaultPaging(options); - return this.get("projects", options); + return this.get('projects', options); } allAdmin(options = {}) { Utils.defaultPaging(options); - return this.get("projects/all", options); + return this.get('projects/all', options); } create(options = {}) { - if(options.userId){ - return this.post(`projects/user/${userId}`, options); - } else { - return this.post("projects", options); + if (options.userId) { + const uId = Utils.parse(options.userId); + + return this.post(`projects/user/${uId}`, options); } + + return this.post('projects', options); } edit(projectId, options = {}) { - return this.put(`projects/${Utils.parse(projectId)}`, options); + const pId = Utils.parse(projectId); + + return this.put(`projects/${pId}`, options); } fork(projectId, options = {}) { - return this.post(`projects/${projectId}/fork`, options); + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/fork`, options); } remove(projectId) { - return this.delete(`projects/${Utils.parse(projectId)}`); + const pId = Utils.parse(projectId); + + return this.delete(`projects/${pId}`); } search(projectName) { - return this.get(`projects`, { search: projectName }); + return this.get('projects', { search: projectName }); } share(projectId, groupId, groupAccess, options) { - if(!groupId || !groupAccess) throw new Error("Missing required arguments"); + const pId = Utils.parse(projectId); + + if (!groupId || !groupAccess) throw new Error('Missing required arguments'); options.group_id = groupId; options.groupAccess = groupAccess; - return this.post(`projects/${Utils.parse(projectId)}/share`, options); + return this.post(`projects/${pId}/share`, options); } show(projectId) { - return this.get(`projects/${Utils.parse(projectId)}`); + const pId = Utils.parse(projectId); + + return this.get(`projects/${pId}`); } - + star(projectId) { - return this.post(`projects/${Utils.parse(projectId)}/star`); + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/star`); } unstar(projectId) { - return this.post(`projects/${Utils.parse(projectId)}/unstar`); + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/unstar`); } upload(projectId, filePath) { - return this.post(`projects/${Utils.parse(projectId)}/uploads`, { - file: filePath + const pId = Utils.parse(projectId); + + return this.post(`projects/${pId}/uploads`, { + file: filePath, }); } } diff --git a/src/Models/Runners.js b/src/Models/Runners.js index c6ea6e84..a67aee78 100644 --- a/src/Models/Runners.js +++ b/src/Models/Runners.js @@ -2,20 +2,26 @@ const BaseModel = require('../BaseModel'); const Utils = require('../Utils'); class Runners extends BaseModel { - constructor(...args) { - super(...args); + all(options = {}) { + return this.get('runners/all', options); } show(runnerId) { - return this.get(`runners/${parseInt(runnerId)}`); + const rId = Utils.parse(runnerId); + + return this.get(`runners/${rId}`); } update(runnerId, attributes) { - return this.put(`runners/${parseInt(runnerId)}`, attributes); + const rId = Utils.parse(runnerId); + + return this.put(`runners/${rId}`, attributes); } - remove(runnerId, projectId, enable) { - return this.delete(`runners/${parseInt(runnerId)}`); + remove(runnerId) { + const rId = Utils.parse(runnerId); + + return this.delete(`runners/${rId}`); } } diff --git a/src/Models/UserKeys.js b/src/Models/UserKeys.js index 06a90959..94a8006a 100644 --- a/src/Models/UserKeys.js +++ b/src/Models/UserKeys.js @@ -1,18 +1,19 @@ const BaseModel = require('../BaseModel'); +const Utils = require('../Utils'); class UserKeys extends BaseModel { - constructor(...args) { - super(...args); - } - all(userId) { - return this.get(`users/${parseInt(userId)}/keys`); + const uId = Utils.parse(userId); + + return this.get(`users/${uId}/keys`); } addKey(userId, title, key) { - return this.post(`users/${userId}/keys`, { + const uId = Utils.parse(userId); + + return this.post(`users/${uId}/keys`, { title, - key + key, }); } } diff --git a/src/Models/Users.js b/src/Models/Users.js index c4da7aee..7be7862f 100644 --- a/src/Models/Users.js +++ b/src/Models/Users.js @@ -1,5 +1,6 @@ const BaseModel = require('../BaseModel'); const UserKeys = require('./UserKeys'); +const Utils = require('../Utils'); class Users extends BaseModel { constructor(...args) { @@ -11,31 +12,33 @@ class Users extends BaseModel { all(options = {}) { Utils.defaultPaging(options); - return this.get("users", options); + return this.get('users', options); } current() { - return this.get("user"); + return this.get('user'); } show(userId) { - return this.get(`users/${parseInt(userId)}`); + const uId = Utils.parse(userId); + + return this.get(`users/${uId}`); } create(options = {}) { - return this.post("users", options); + return this.post('users', options); } session(email, password) { - return this.post("session", { + return this.post('session', { email, - password + password, }); } search(emailOrUsername) { - return this.get("users", { - search: emailOrUsername + return this.get('users', { + search: emailOrUsername, }); } } diff --git a/src/Models/index.js b/src/Models/index.js index b9f2c53e..e5ce643b 100644 --- a/src/Models/index.js +++ b/src/Models/index.js @@ -5,4 +5,4 @@ const Users = require('./Users'); const Labels = require('./Labels'); const Runners = require('./Runners'); -module.exports = {Groups, Projects, Runners, Issues, Users, Labels} \ No newline at end of file +module.exports = { Groups, Projects, Runners, Issues, Users, Labels }; diff --git a/src/Utils.js b/src/Utils.js index 6587d4ac..2041af45 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -1,15 +1,16 @@ -function parse(value){ - if (typeof value === "number") return value; - else if (value.toString().includes("/")) return encodeURIComponent(value); - else return parseInt(value,10); +function parse(value) { + if (typeof value === 'number') return value; + else if (value.toString().includes('/')) return encodeURIComponent(value); + + return parseInt(value, 10); } -function defaultPaging(options){ - options.page = options.page || 1; - options.per_page = options.per_page || 100; +function defaultPaging(options) { + options.page = options.page || 1; + options.per_page = options.per_page || 100; } module.exports = { - parse, - defaultPaging -} \ No newline at end of file + parse, + defaultPaging, +};