Comparing version 0.1.10 to 0.1.11
@@ -21,6 +21,5 @@ { | ||
"get", | ||
"project", | ||
"raw", | ||
"/vehicle/node/description", | ||
"id", | ||
"name" | ||
"package.json" | ||
] | ||
@@ -27,0 +26,0 @@ } |
@@ -7,3 +7,4 @@ import axios from "axios"; | ||
export class GitlabApiDriver { | ||
constructor(baseUrl, accessToken) { | ||
constructor(baseUrl, accessToken, verbose) { | ||
this.VERBOSE = verbose; | ||
this.BASE_URL = trimTailingSlash(baseUrl); | ||
@@ -23,6 +24,20 @@ | ||
async _getProjectByPath(projectPath) { | ||
const encodedPath = encodeURIComponent(trimSlashes(projectPath)) | ||
const url = `${this.API_URL}/projects/${encodedPath}`; | ||
getProjectUrl(identifier) { | ||
const resolvedIdentifier = resolveProjectIdentifier(this.BASE_URL, identifier); | ||
if(typeof resolvedIdentifier.id !== 'undefined') { | ||
return `${this.API_URL}/projects/${resolvedIdentifier.id}`; | ||
} | ||
else if(typeof resolvedIdentifier.path !== 'undefined') { | ||
const encodedPath = encodeURIComponent(trimSlashes(resolvedIdentifier.path)) | ||
return `${this.API_URL}/projects/${encodedPath}`; | ||
} | ||
else { | ||
throw new Error(`'${identifier}' is an invalid identifier for a project`); | ||
} | ||
} | ||
async getProject(identifier) { | ||
const url = this.getProjectUrl(identifier); | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -32,9 +47,10 @@ return res.data; | ||
catch(e) { | ||
throw new GitlabApiError(`Error requesting project with path '${this.BASE_URL}/${trimSlashes(projectPath)}'\n\nOriginal Error:\n${e}`) | ||
throw new GitlabApiError(`Error requesting project identified by '${identifier}'\n\nOriginal Error:\n${e}`) | ||
} | ||
} | ||
async _getProjectById(projectId) { | ||
const url = `${this.API_URL}/projects/${projectId}`; | ||
async getBranches(projectIdentifier) { | ||
const url = `${this.getProjectUrl(projectIdentifier)}/repository/branches`; | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -44,22 +60,10 @@ return res.data; | ||
catch(e) { | ||
throw new GitlabApiError(`Error requesting project with ID '${projectId}'\n\nOriginal Error:\n${e}`) | ||
throw new GitlabApiError(`Error requesting branchs for project identified by '${projectIdentifier}'\n\nOriginal Error:\n${e}`) | ||
} | ||
} | ||
async getProject(identifier) { | ||
const resolvedIdentifier = resolveProjectIdentifier(this.BASE_URL, identifier); | ||
if(typeof resolvedIdentifier.id !== 'undefined') { | ||
return await this._getProjectById(resolvedIdentifier.id); | ||
} | ||
else if(typeof resolvedIdentifier.path !== 'undefined') { | ||
return await this._getProjectByPath(resolvedIdentifier.path); | ||
} | ||
else { | ||
throw new Error(`'${identifier}' is an invalid identifier for a project`); | ||
} | ||
} | ||
async branchExists(projectId, branchName) { | ||
const url = `${this.API_URL}/projects/${projectId}/repository/branches/${branchName}`; | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -82,2 +86,3 @@ return res.status === 200; | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -105,2 +110,3 @@ return res.status === 200; | ||
try { | ||
if(this.VERBOSE) console.log(`POST > ${url}`); | ||
const res = await axios.post(url, commitObject, config); | ||
@@ -123,2 +129,3 @@ return res.status === 201; | ||
try { | ||
if(this.VERBOSE) console.log(`PUT > ${url}`); | ||
const res = await axios.put(url, commitObject, config); | ||
@@ -132,6 +139,10 @@ return res.status === 201; | ||
async getRawFile(projectId, branchName, filePath) { | ||
async getRawFile(projectIdentifier, filePath, branchName) { | ||
if(typeof branchName === 'undefined') { | ||
branchName = (await this.getProject(projectIdentifier)).default_branch; | ||
} | ||
const encodedFilePath = encodeURIComponent(trimSlashes(filePath)) | ||
const url = `${this.API_URL}/projects/${projectId}/repository/files/${encodedFilePath}/raw?ref=${branchName}` | ||
const url = `${this.getProjectUrl(projectIdentifier)}/repository/files/${encodedFilePath}/raw?ref=${branchName}` | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -143,8 +154,3 @@ if(res.status === 200) { | ||
catch(e) { | ||
if(typeof e.response !== 'undefined' && e.response.status === 404) { | ||
return false; | ||
} | ||
else { | ||
throw new GitlabApiError(`Error requesting raw file '${filePath}' from branch '${branchName}' for project ID '${projectId}'\n\nOriginal Error:\n${e}`) | ||
} | ||
throw new GitlabApiError(`Error requesting raw file '${filePath}' from branch '${branchName}' for project identified by '${projectIdentifier}'\n\nOriginal Error:\n${e}`) | ||
} | ||
@@ -156,2 +162,3 @@ } | ||
try { | ||
if(this.VERBOSE) console.log(`GET > ${url}`); | ||
const res = await axios.get(url, this.config); | ||
@@ -158,0 +165,0 @@ if(res.status === 200) { |
34
index.js
@@ -16,3 +16,3 @@ #!/usr/bin/env node | ||
const version = "0.1.10"; | ||
const version = "0.1.11"; | ||
@@ -30,4 +30,4 @@ const parser = new ArgumentParser({ | ||
parser.add_argument('--json', {action: 'store_true', help: 'always print result as json, even if it is a single value'}) | ||
parser.add_argument('--ref', {metavar: 'ref', type: String, help: 'provide a git ref'}) | ||
const args = parser.parse_args() | ||
@@ -88,10 +88,23 @@ | ||
const objectIdentifier = parameters.shift(); | ||
// remaining parameters are object fields | ||
const fields = parameters; | ||
let fields; | ||
switch(objectType) { | ||
case "project": | ||
fields = parameters; | ||
if(args.verbose) console.log(`Doing 'GET' > 'project' with identifier '${objectIdentifier}' and fields '${fields}'`); | ||
await getProject(args, objectIdentifier, fields); | ||
break; | ||
case "branches": | ||
fields = parameters; | ||
if(args.verbose) console.log(`Doing 'GET' > 'branches' with identifier '${objectIdentifier}' and fields '${fields}'`); | ||
await getBranches(args, objectIdentifier, fields); | ||
break; | ||
case "raw": | ||
if(parameters.length != 1) { | ||
throw new Error(`Error: wrong number of parameters for 'get raw' action`) | ||
} | ||
const filePath = parameters.shift(); | ||
if(args.verbose) console.log(`Doing 'GET' > 'raw' with identifier project identifier '${objectIdentifier}' and file path '${fields}'`); | ||
await getRaw(args, objectIdentifier, filePath); | ||
break; | ||
default: | ||
@@ -114,2 +127,15 @@ throw new Error(`Error: object type '${objectType}' is not supported`) | ||
console.dir(result); | ||
} | ||
async function getBranches(args, objectIdentifier, fields) { | ||
const api = getApiDriver(args); | ||
const branches = await api.getBranches(objectIdentifier); | ||
const result = filterFields(args, branches, fields); | ||
console.dir(result); | ||
} | ||
async function getRaw(args, projectIdentifier, filePath) { | ||
const api = getApiDriver(args); | ||
const rawFile = await api.getRawFile(projectIdentifier, filePath, args.ref); | ||
console.log(rawFile); | ||
} |
{ | ||
"name": "gitlab-x", | ||
"version": "0.1.10", | ||
"version": "0.1.11", | ||
"description": "GitLab Executor API Interface", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/mxcd/gitlab-api", |
@@ -64,2 +64,15 @@ ## gitlab-x | ||
$ npx gitlab-x get project 23106572 name web_url | ||
{ name: 'test', web_url: 'https://gitlab.com/mxcd/test' } | ||
{ name: 'test', web_url: 'https://gitlab.com/mxcd/test' } | ||
``` | ||
##### get branches | ||
`$ npx gitlab-x get branches <project_identifier> [fields...]` | ||
##### get branch | ||
##### get issues | ||
##### get issue | ||
##### get raw | ||
`$ npx gitlab-x get raw <project_identifier> <file_path> [--ref <branch>]` |
21
util.js
@@ -42,7 +42,7 @@ import { GitlabApiDriver } from "./api-driver.js"; | ||
export function getApiDriver(args) { | ||
return new GitlabApiDriver(args.url, args.access_token); | ||
return new GitlabApiDriver(args.url, args.access_token, args.verbose); | ||
} | ||
export function filterFields(args, obj, fields) { | ||
if(typeof obj === 'object' && fields.length !== 0) { | ||
const filterSingleObj = (args, obj, fields) => { | ||
if(fields.length === 1 && !args.json) { | ||
@@ -57,4 +57,21 @@ return obj[fields[0]] | ||
return result; | ||
} | ||
} | ||
if(fields.length !== 0) { | ||
if(typeof obj === 'object') { | ||
if(Array.isArray(obj)) { | ||
let result = []; | ||
for(let listObj of obj) { | ||
result.push(filterSingleObj(args, listObj, fields)) | ||
} | ||
return result; | ||
} | ||
else { | ||
return filterSingleObj(args, obj, fields); | ||
} | ||
} | ||
else { | ||
return obj; | ||
} | ||
} | ||
@@ -61,0 +78,0 @@ else { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17089
372
77