GitLab

getReleaseVersion

Code

const axios = require('axios');
const path = require('path');
const { readPropertiesFile, requireHttpsUrl, resolveSecureUrl } = require('../essential');

/**
 * Fetches a release version from GitLab.
 * @param {string} groupId - The ID of the group (unused but kept for signature).
 * @param {string} projectId - The ID of the project.
 * @param {string} version - The tag name of the release.
 * @param {string} token - The GitLab access token.
 * @returns {Promise<Object>}
 */
async function getReleaseVersion(groupId, projectId, version, token) {
    const filePath = path.join(__dirname, 'properties', 'api.properties');
    const config = readPropertiesFile(filePath);

    if (!config.reporeleaseurl) {
        throw new Error("Release URL is missing in the configuration.");
    }

    requireHttpsUrl(config.reporeleaseurl, 'reporeleaseurl');

    const replacements = { group_id: groupId, project_id: projectId };

    try {
        const url = resolveSecureUrl(config.reporeleaseurl, replacements, 'reporeleaseurl');
        const response = await axios.get(url, {
            headers: {
                Authorization: `Bearer ${token}`
            }
        });

        const release = response.data.find(r => r.tag_name === version);

        if (release) {
            return {
                success: true,
                releaseName: release.name,
                releaseTag: release.tag_name,
                releaseURL: release._links.self,
            };
        } else {
            return { success: false, message: `Release ${version} not found.` };
        }
    } catch (error) {
        console.error('Error fetching GitLab release:', error.message);
        return { success: false, message: error.message };
    }
}

module.exports = getReleaseVersion;

Usage

/*
    @param groupId = String
    @param projectId = String
    @param version = String
    @param token = String
*/
const getReleaseVersion = require('jwz/gitlab');

const groupId = 'your-group-id'; // Can be null if using project ID directly
const projectId = 'your-project-id';
const version = 'v1.0.0';
const token = 'your-token';

const res = await getReleaseVersion(groupId, projectId, version, token);
console.log(res);

Explanation

The getReleaseVersion function retrieves specific release details from a GitLab repository.

  • Parameters:
    • groupId: The ID of the GitLab group (kept for consistency, though primarily the project ID is used).
    • projectId: The specific repository/project ID.
    • version: The tag name of the release (e.g., 'v1.0.0').
    • token: A GitLab personal access token.
  • Logic:
    • Reads the release URL from api.properties.
    • Checks the endpoint uses https, and throws before the token is sent if it does not.
    • Authenticates using the provided token in the header.
    • Fetches all releases for the project and filters by the requested tag name.
  • Output:
    • Returns an object with success status and release details (name, tag, URL) if found, or an error message if not.