Skip to content

rosbitskyy/gitlab

Repository files navigation

Integration with GitLab REST API

os, nodes npm version Downloads/month Vulnerabilities

The module allows you to perform the list of methods described in the GitLab service documentation in the section Jobs , Pipelines ( for now)

And you can Add your own method that is not yet implemented by this api

  • List project jobs
  • List pipeline jobs
  • List pipeline trigger jobs
  • Get job token’s job
  • Get GitLab agent by CI_JOB_TOKEN
  • Get a single job
  • Get a log file
  • Cancel a job
  • Retry a job
  • Erase a job
  • Run a job
  • List project pipelines
  • Get a single pipeline
  • Get variables of a pipeline
  • Get a pipeline’s test report
  • Get a pipeline’s test report summary
  • Get the latest pipeline
  • Create a new pipeline
  • Retry jobs in a pipeline
  • Cancel a pipeline’s jobs
  • Delete a pipeline

read more

And yes, let's start!

☕️ buy me a coffee

Installation

First install Node.js

npm i gitlab-restapi

An example of receiving a list of tasks divided into pages with the specified status

const GitLab = require("gitlab-restapi");
const gitLab = new GitLab.API(new GitLab.Options({
    privateToken: process.env.GIT_TOKEN,
    projectId: process.env.GIT_PID,
}));

const jobs = await gitLab.Jobs.jobs(
    new GitLab.PaginateParams({
        page: 1, per_page: 100, scope: ['success', 'failed']
    }));
// find first(last executed) Job by name
const job = jobs.find({name: 'testofclasses'});
console.log(job.status)

scope

  • string or array of strings - Scope of jobs to show. Either one of or an array of the following: created, pending, running, failed, success, canceled, skipped, waiting_for_resource, or manual. All jobs are returned if scope is not provided.

GitLab.Options can specify your method for fetching data. For nodes 18+ - this is the own node fetch (by default), for younger versions (unless otherwise specified) - the internal mechanism

new GitLab.Options({
    privateToken: process.env.GIT_TOKEN,
    projectId: process.env.GIT_PID,
    fetchMethod: fetch // axios, fetch, node-fetch, GitLab.Request (tested)
})

List of Jobs methods:

const GitLab = require("gitlab-restapi");
const gitLab = new GitLab.API(new GitLab.Options({
    privateToken: process.env.GIT_TOKEN,
    projectId: process.env.GIT_PID,
    fetchMethod: fetch // axios, fetch, node-fetch, GitLab.Request (tested)
}));
console.log(gitLab.Jobs.methods)

// list of registered APIs
console.log(gitLab.getOwnPropertyNames())

Get a single job of a project

const jobs = await gitLab.Jobs.jobs(
    new GitLab.PaginateParams({page: 1, per_page: 1, scope: ['success']}));
// const jobs = await gitLab.Jobs.jobs(new GitLab.PaginateParams({})); // page: 1, per_page: 20, all scopes
// const jobs = await gitLab.Jobs.jobs(); // page: 1, per_page: 20, all scopes
console.log('jobs:', jobs.list)

const _job = jobs.find({status: 'success'});
console.log('found:', _job)

const job = await gitLab.Jobs.job(_job.id);
console.log('Get a single job of a project by id:', job)

Erase a single job of a project (remove job artifacts and a job log)

const jobs = await gitLab.Jobs.jobs(new GitLab.PaginateParams({
    page: 1,
    per_page: 100,
    scope: ['failed', 'canceled']
}));
const erasedJobs = new GitLab.Jobs([])
for (let job of jobs.list) {
    if (job.artifacts && job.artifacts.length) {
        const obj = await gitLab.Jobs.erase(job.id);
        if (obj) erasedJobs.push(obj)
    }
}
console.log(erasedJobs.list)

Pipelines

const pipelinelatest = await gitLab.Pipelines.latest();
console.log(pipelinelatest)

const pipelines = await gitLab.Pipelines.pipelines(new GitLab.PaginateParams({
    page: 1, per_page: 20,  status: 'success', source: 'push',}));
console.log(pipelines.list)

Add your own method that is not yet implemented by this api

gitLab.add('MyGroups').addMethods({
    groups: new GitLab.Method({method: 'get', class: GitLab.Responses, 
        url: () => `groups`})
})
console.log(gitLab.MyGroups.methods)

const groups = await gitLab.MyGroups.groups(
    new GitLab.PaginateParams({page: 2, per_page: 20}));
console.log(groups.list)

Releases

gitLab.add('MyReleases').addMethods({
    releases: new GitLab.Method({method: 'get', class: GitLab.Responses, 
        url: () => `projects/${gitLab.projectId}/releases`})
})
console.log(gitLab.MyReleases.methods)

const releases = await gitLab.MyReleases.releases(new GitLab.PaginateParams({page: 2, per_page: 20}));
console.log(releases.list)

Thanks for your attention - the continuation of the api will come soon