0.0.1-packtest16 • Published 2 months ago

@sungv/gitlab-node-client v0.0.1-packtest16

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@sungv/gitlab-node-client

Overview

@sungv/gitlab-node-client provides a convenient interface to interact with the GitLab API from Node.js. It allows you to manage projects, issues, merge requests, users, groups, and more.

Features

  • Compatible with both Node.js and browser environments.
  • Supports ES modules and CommonJS.
  • Includes full development setup with linting, testing, and building.
  • Automated CI/CD pipeline using GitLab CI and GitHub Actions.
  • Prettier and ESLint for consistent code style.

Badges

GitHub Actions Status npm version License: MIT

Installation

npm install @sungv/gitlab-node-client

Usage

Basic Example

const GitLabAPI = require("@sungv/gitlab-node-client");

const gitlab = new GitLabAPI({
  host: "https://gitlab.example.com",
  token: "YOUR_PERSONAL_ACCESS_TOKEN",
});

// Get all projects (paginated)
(async () => {
  try {
    const projects = await gitlab.projects.all({ per_page: 5 });
    console.log("Projects:", projects.items);
  } catch (error) {
    console.error("Error:", error.message);
  }
})();

Available APIs:

The following table lists all currently available API categories and methods within them. You can find the detailed documentation for the GitLab API at https://docs.gitlab.com/ee/api/rest/.

CategoryMethodSignatureDescriptionExample Usage
Projectsall(options = {})PromiseList all projects (paginated).gitlab.projects.all({ query: { simple: true } })
show(projectId)PromiseGet a specific project.gitlab.projects.show(123)
create(data)PromiseCreate a new project.gitlab.projects.create({ name: 'My Project' })
update(projectId, data)PromiseUpdate a project.gitlab.projects.update(123, { description: 'Updated description' })
remove(projectId)PromiseRemove a project.gitlab.projects.remove(123)
fork(projectId, data)PromiseFork a project.gitlab.projects.fork(123, { namespace_id: 456 })
search(searchString, options)PromiseSearch for projects (paginated).gitlab.projects.search("my project", { per_page: 20 })
Repositoriestree(projectId, options)PromiseList repository files and directories (paginated).gitlab.repositories.tree(123, { path: 'src', ref: 'main' })
getFile(projectId, path, ref)PromiseGet the raw content of a file.gitlab.repositories.getFile(123, 'README.md', 'main')
createFile(projectId, path, data)PromiseCreate a file.gitlab.repositories.createFile(123, 'new_file.txt', { branch: 'main', content: 'file content', commit_message: 'create new file' })
updateFile(projectId, path, data)PromiseUpdate a file.gitlab.repositories.updateFile(123, 'new_file.txt', { branch: 'main', content: 'updated file content', commit_message: 'update file' })
deleteFile(projectId, path, data)PromiseDelete a file.gitlab.repositories.deleteFile(123, 'new_file.txt', { branch: 'main', commit_message: 'delete file' })
branches.all(projectId, options)PromiseList project branches (paginated).gitlab.repositories.branches.all(123)
branches.show(projectId, branchName)PromiseGet a specific branch.gitlab.repositories.branches.show(123, 'main')
branches.create(projectId, data)PromiseCreate a branchgitlab.repositories.branches.create(123, {branch: 'new-branch', ref: 'main'})
branches.delete(projectId, branchName)PromiseDelete a branchgitlab.repositories.branches.delete(123, 'new-branch')
Merge Requestsall(projectId, options)PromiseList project merge requests (paginated).gitlab.mergeRequests.all(123, { state: 'opened' })
show(projectId, mergeRequestId)PromiseGet a specific merge request.gitlab.mergeRequests.show(123, 456)
create(projectId, data)PromiseCreate a new merge request.gitlab.mergeRequests.create(123, { source_branch: 'feature', target_branch: 'main', title: 'My MR' })
update(projectId, mergeRequestId, data)PromiseUpdate a merge request.gitlab.mergeRequests.update(123, 456, { description: 'Updated MR description' })
accept(projectId, mergeRequestId, options)PromiseAccept a merge request.gitlab.mergeRequests.accept(123, 456)
cancel(projectId, mergeRequestId)PromiseCancel a merge request.gitlab.mergeRequests.cancel(123, 456)
Usersall(options = {})PromiseList all users (paginated).gitlab.users.all()
show(userId)PromiseGet a specific user.gitlab.users.show(123)
current()PromiseGet the current authenticated user.gitlab.users.current()
Groupsall(options = {})PromiseList all groups (paginated).gitlab.groups.all()
show(groupId)PromiseGet a specific group.gitlab.groups.show(123)
create(data)PromiseCreate a new group.gitlab.groups.create({ name: 'My Group', path: 'my-group' })
update(groupId, data)PromiseUpdate a group.gitlab.groups.update(123, { description: 'Updated group description' })
delete(groupId)PromiseDelete a group.gitlab.groups.delete(123)
projects(groupId, options)PromiseList group projects (paginated).gitlab.groups.projects(123)
Issuesall(projectId, options = {})PromiseList project issues (paginated).gitlab.issues.all(123, { state: 'opened' })
show(projectId, issueIid)PromiseGet a specific issue.gitlab.issues.show(123, 456)
create(projectId, data)PromiseCreate a new issue.gitlab.issues.create(123, { title: 'My Issue' })
update(projectId, issueIid, data)PromiseUpdate an issue.gitlab.issues.update(123, 456, { description: 'Updated description' })
delete(projectId, issueIid)PromiseDelete an issue.gitlab.issues.delete(123, 456)
notes.all(projectId, issueIid, options)PromiseList issue notes (paginated).gitlab.issues.notes.all(123, 456)
notes.create(projectId, issueIid, data)PromiseCreate an issue note.gitlab.issues.notes.create(123, 456, { body: 'My comment' })
notes.update(projectId, issueIid, noteId, data)PromiseUpdate an issue note.gitlab.issues.notes.update(123, 456, 789, { body: 'Updated comment' })
notes.delete(projectId, issueIid, noteId)PromiseDelete an issue note.gitlab.issues.notes.delete(123, 456, 789)
Pipelinesall(projectId, options = {})PromiseList project pipelines (paginated).gitlab.pipelines.all(123)
show(projectId, pipelineId)PromiseGet a specific pipeline.gitlab.pipelines.show(123, 456)
create(projectId, ref)PromiseCreate a new pipeline.gitlab.pipelines.create(123, 'main')
retry(projectId, pipelineId)PromiseRetry a pipeline.gitlab.pipelines.retry(123, 456)
cancel(projectId, pipelineId)PromiseCancel a pipeline.gitlab.pipelines.cancel(123, 456)
jobs(projectId, pipelineId, options)PromiseList pipeline jobs (paginated).gitlab.pipelines.jobs(123, 456)
Jobsshow(projectId, jobId)PromiseGet a specific job.gitlab.jobs.show(123, 456)
artifacts(projectId, jobId)PromiseGet job artifacts.gitlab.jobs.artifacts(123, 456)
trace(projectId, jobId)PromiseGet job trace.gitlab.jobs.trace(123, 456)
retry(projectId, jobId)PromiseRetry a job.gitlab.jobs.retry(123, 456)
cancel(projectId, jobId)PromiseCancel a job.gitlab.jobs.cancel(123, 456)
Commitsshow(projectId, commitSha)PromiseGet a specific commit.gitlab.commits.show(123, 'abcdef123456')
commits(projectId, options = {})PromiseList project commits (paginated).gitlab.commits.commits(123)
diff(projectId, commitSha)PromiseGet commit diff.gitlab.commits.diff(123, 'abcdef123456')
comments(projectId, commitSha)PromiseGet commit comments.gitlab.commits.comments(123, 'abcdef123456')
Project Variablesall(projectId, options = {})PromiseList project variables (paginated).gitlab.projectsVariables.all(123)
show(projectId, key)PromiseGet a specific project variable.gitlab.projectsVariables.show(123, 'MY_VARIABLE')
create(projectId, data)PromiseCreate a new project variable.gitlab.projectsVariables.create(123, { key: 'NEW_VARIABLE', value: 'my value' })
update(projectId, key, data)PromiseUpdate a project variable.gitlab.projectsVariables.update(123, 'MY_VARIABLE', { value: 'updated value' })
remove(projectId, key)PromiseRemove a project variable.gitlab.projectsVariables.remove(123, 'MY_VARIABLE')
Deploymentsall(projectId, options = {})PromiseList project deployments (paginated).gitlab.deployments.all(123)
show(projectId, deploymentId)PromiseGet a specific deployment.gitlab.deployments.show(123, 456)
create(projectId, data)PromiseCreate a new deployment.gitlab.deployments.create(123, { environment: 'production', tag: 'v1.0.0' })
update(projectId, deploymentId, data)PromiseUpdate a deployment.gitlab.deployments.update(123, 456, { status: 'success' })
delete(projectId, deploymentId)PromiseDelete a deployment.gitlab.deployments.delete(123, 456)
Environmentsall(projectId, options = {})PromiseList project environments (paginated).gitlab.environments.all(123)
show(projectId, environmentId)PromiseGet a specific environment.gitlab.environments.show(123, 456)
create(projectId, data)PromiseCreate a new environment.gitlab.environments.create(123, { name: 'staging' })
update(projectId, environmentId, data)PromiseUpdate an environment.gitlab.environments.update(123, 456, { name: 'production' })
delete(projectId, environmentId)PromiseDelete an environment.gitlab.environments.delete(123, 456)
Membersall(projectId, options = {})PromiseList project members (paginated).gitlab.members.all(123)
show(projectId, userId)PromiseGet a specific project member.gitlab.members.show(123, 456)
create(projectId, data)PromiseAdd a new project member.gitlab.members.create(123, { user_id: 456, access_level: 30 })
update(projectId, userId, data)PromiseUpdate a project member.gitlab.members.update(123, 456, { access_level: 40 })
delete(projectId, userId)PromiseRemove a project member.gitlab.members.delete(123, 456)
Groups Membersall(groupId, options = {})PromiseList group members (paginated).gitlab.groupsMembers.all(123)
show(groupId, userId)PromiseGet a specific group member.gitlab.groupsMembers.show(123, 456)
create(groupId, data)PromiseAdd a new group member.gitlab.groupsMembers.create(123, { user_id: 456, access_level: 30 })
update(groupId, userId, data)PromiseUpdate a group member.gitlab.groupsMembers.update(123, 456, { access_level: 40 })
delete(groupId, userId)PromiseRemove a group member.gitlab.groupsMembers.delete(123, 456)
Searchprojects(query, options = {})PromiseSearch for projects (paginated).gitlab.search.projects("my project", { per_page: 20, query: { order_by: "name", sort: "asc" } })
groups(query, options = {})PromiseSearch for groups (paginated).gitlab.search.groups("my group", { per_page: 20 })
users(query, options = {})PromiseSearch for users (paginated).gitlab.search.users("john", { per_page: 20, query: { active: true } })

Features

Pagination:

Most API methods that retrieve collections (e.g., projects, issues) support pagination. You can control the page size using the per_page option in the request options object. The response object will include information about the total pages, total items, and the current page.

Rate Limiting:

The GitLab API enforces rate limits to prevent abuse. This module captures the remaining rate limit and reset time from the response headers and stores them in the rateLimitRemaining and rateLimitReset properties of the GitLabAPI instance. You can use this information to implement your own retry mechanism for handling rate limit errors.

Error Handling:

The module uses custom error classes (GitLabAPIError, GitLabNotFoundError, GitLabAuthenticationError, GitLabRateLimitError) to provide more context about API errors. These errors include the HTTP status code, response body, and the URL of the request that failed.

Development

Setup

  1. Clone the repository:

    git clone https://gitlab.com/your-repo/gitlab-node-client.git
    cd gitlab-node-client
  2. Install dependencies:

    npm install

Scripts

  • Build: npm run build
  • Test: npm run test
  • Lint: npm run lint
  • Format: npm run format
  • Prepare (Husky hooks): npm run prepare

Code Style Guide

This project enforces consistent code style using Prettier and ESLint:

  • Prettier handles code formatting.
  • ESLint ensures adherence to the Airbnb base style guide.

Run formatting with:

npm run format

Generate Documentation

This project uses TypeDoc for generating documentation.

Generate documentation with:

npm run typedoc

Contributing

Contributions are welcome! Please open an issue or submit a pull request with your changes.

Issue Templates

Include the following types:

  • Bug Reports: Template for submitting bug reports.
  • Feature Requests: Template for suggesting new features.

License

This project is licensed under the MIT License.

Changelog

1.0.0 - YYYY-MM-DD

Added

  • Initial release with basic functionality.
  • Prettier and ESLint setup.
  • CI/CD pipeline using GitLab CI and GitHub Actions.
  • Tree-shaking and optimized build.
  • Issue templates and badges.
0.0.1-packtest16

2 months ago

0.0.1-packtest14

2 months ago

0.0.1-packtest15

2 months ago

0.0.1-packtest12

2 months ago

0.0.1-packtest13

2 months ago

0.0.1-packtest10

2 months ago

0.0.1-packtest11

2 months ago

0.0.1-packtest2

2 months ago

0.0.1-packtest4

2 months ago

0.0.1-packtest3

2 months ago

0.0.1-packtest6

2 months ago

0.0.1-packtest5

2 months ago

0.0.2-packtest1

2 months ago

0.0.1-packtest8

2 months ago

0.0.1-packtest7

2 months ago

0.0.1-packtest9

2 months ago

0.0.1-packtest1

7 months ago