const axios = require('axios');
const path = require('path');
const {
readPropertiesFile,
replacePlaceholders
} = require('../essential');
const filePath = path.join(__dirname, 'properties', 'api.properties');
const config = readPropertiesFile(filePath);
if (!config.repocollaboratorurl) {
throw new Error(`Collaborator URL is missing in the configuration file: ${filePath}`);
}
/**
* Invite collaborators to group repositories.
* @param {string} groupId - The ID of the group.
* @param {Array} repoIds - Array of repository IDs.
* @param {Array>} collaborators - Array of collaborator IDs per repository.
* @param {string} token - Authentication token.
* @returns {Promise} - Results of invitations.
*/
async function inviteToGroupRepos(groupId, repoIds, collaborators, token) {
if (!groupId || !repoIds.length || !collaborators.length || !token) {
throw new Error('Invalid parameters: groupId, repoIds, collaborators, and token are required.');
}
return inviteCollaborators(groupId, repoIds, collaborators, token);
}
/**
* Invite collaborators to personal repositories.
* @param {Array} repoIds - Array of repository IDs.
* @param {Array>} collaborators - Array of collaborator IDs per repository.
* @param {string} token - Authentication token.
* @returns {Promise} - Results of invitations.
*/
async function inviteToPersonalRepos(repoIds, collaborators, token) {
if (!repoIds.length || !collaborators.length || !token) {
throw new Error('Invalid parameters: repoIds, collaborators, and token are required.');
}
return inviteCollaborators(null, repoIds, collaborators, token);
}
/**
* Helper function to invite collaborators to repositories.
* @param {string|null} groupId - The ID of the group (null for personal repositories).
* @param {Array} repoIds - Array of repository IDs.
* @param {Array>} collaborators - Array of collaborator IDs per repository.
* @param {string} token - Authentication token.
* @returns {Promise} - Results of invitations.
*/
async function inviteCollaborators(groupId, repoIds, collaborators, token) {
if (repoIds.length !== collaborators.length) {
throw new Error('Mismatch: repoIds and collaborators arrays must be of equal length.');
}
return Promise.all(repoIds.map(async (repoId, index) => {
const repoResults = await Promise.all(collaborators[index].map(async (collaboratorId) => {
if (!collaboratorId) return { collaborator: collaboratorId, success: false, error: "Invalid collaborator ID" };
const url = replacePlaceholders(config.repocollaboratorurl, {
project_id: repoId
});
try {
const response = await axios.post(url, {
user_id: collaboratorId,
access_level: 30
}, {
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }
});
return { collaborator: collaboratorId, success: true, response: response.data };
} catch (error) {
return {
collaborator: collaboratorId,
success: false,
error: error.response?.data || error.message
};
}
}));
return { repoId, results: repoResults };
}));
}
module.exports = { inviteToGroupRepos, inviteToPersonalRepos };
/*
@param groupId = String (for group repos)
@param repoIds = Array (array of repository IDs)
@param collaborators = Array (array of arrays, each containing collaborator IDs for each repo)
@param token = String (authentication token)
*/
const { inviteToGroupRepos, inviteToPersonalRepos } = require('jwz/gitlab'); // Adjust the import path as needed
const groupId = 'your-group-id'; // Replace with your GitLab group ID
const repoIds = ['repo-id-1', 'repo-id-2']; // List of repository IDs
const collaborators = [
['collaborator1', 'collaborator2'], // Collaborators for repo 1
['collaborator3', 'collaborator4'] // Collaborators for repo 2
];
const token = 'your-token'; // Your GitLab API token
// Inviting collaborators to group repositories
const groupResults = await inviteToGroupRepos(groupId, repoIds, collaborators, token);
console.log(groupResults);
// Inviting collaborators to personal repositories (if needed)
const personalResults = await inviteToPersonalRepos(repoIds, collaborators, token);
console.log(personalResults);
The inviteToGroupRepos
and inviteToPersonalRepos
functions facilitate adding collaborators to GitLab repositories. These functions handle API requests efficiently.
groupId
: The ID of the GitLab group.repoIds
: An array of repository IDs.collaborators
: A nested array, each containing collaborator IDs for a corresponding repository.token
: An authentication token for GitLab API.