All files / backend/helpers/gitcommit gitlabCommit.mjs

92.59% Statements 50/54
96.42% Branches 27/28
42.85% Functions 3/7
100% Lines 50/50

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                          28x       28x 10x 10x 2x   10x 10x 10x 5x   5x             28x 19x     19x 19x 2x 2x     17x 17x 17x   2x 2x 2x       15x 15x 5x     10x 5x   5x 5x 5x   5x                       5x 2x 2x     3x   14x 14x 14x 14x 14x     17x                                     28x 24x 24x 24x 3x           21x       21x   19x   2x              
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import crypto from 'crypto';
import {getLoggerWithTag} from '@global/logger/v2/logger.mjs';
import { v4 as uuidv4 } from 'uuid';
import {buildZipArchive, sendArchiveToAdapter} from '@back/helpers/gitcommit/utils.mjs';
 
const logger = getLoggerWithTag('gl-commit');
 
let ADAPTER_GITLAB_URL_SAVE;
 
const getRepositoryOptions = (path) => {
    let rootPath = path;
    if (!rootPath) {
        rootPath = process.env.VUE_APP_DOCHUB_ROOT_MANIFEST;
    }
    const [protocol, projectID, source] = String(rootPath || '').split(':');
    const branch = source?.split('@')[0];
    if (protocol !== 'gitlab' || !projectID || !branch) {
        throw new Error(`Некорректный GitLab URI: ${rootPath}`);
    }
    return {
        protocol,
        projectID,
        branch
    };
};
 
const commitToGitlabApiAdapter = async({content, rootPath, userInfo}) => {
    const result = {
        success: false
    };
    try {
        if (!process.env.VUE_APP_DOCHUB_GITLAB_ADAPTER_URL) {
            logger.error(() => 'env VUE_APP_DOCHUB_GITLAB_ADAPTER_URL not exist or empty, it\'s required for save by adapter');
            throw new Error('Сохранение данных невозможно (1)');
        }
 
        Eif (!ADAPTER_GITLAB_URL_SAVE) {
            try {
                ADAPTER_GITLAB_URL_SAVE = new URL('/api/v1/send-files', process.env.VUE_APP_DOCHUB_GITLAB_ADAPTER_URL).toString();
            } catch (e) {
                logger.error(() => `Incorrect value in env VUE_APP_DOCHUB_GITLAB_ADAPTER_URL, can't create URL to gas ${process.env.VUE_APP_DOCHUB_GITLAB_ADAPTER_URL}`);
                result.success = false;
                throw new Error('Сохранение данных невозможно');
            }
        }
 
        const {sub, name, surname, email} = userInfo || {};
        if (!sub || !name || !surname) {
            throw new Error('Недостаточно данных пользователя из JWT-токена для отправки в gitlab-адаптер (sub/name/surname)');
        }
 
        const {protocol, projectID, branch} = getRepositoryOptions(rootPath);
        const repoUrl = `${protocol}:${projectID}`;
 
        const archiveBuffer = buildZipArchive(content);
        const checksum = crypto.createHash('sha256').update(archiveBuffer).digest('hex');
        const processId = uuidv4();
 
        const sendResult = await sendArchiveToAdapter({
            adapterUrl: ADAPTER_GITLAB_URL_SAVE,
            archiveBuffer: archiveBuffer,
            checksum: checksum,
            repoUrl: repoUrl,
            branch: branch,
            processId: processId,
            sub: sub,
            name: name,
            surname: surname,
            email: email
        });
        if (!sendResult.success) {
            const {message, error} = sendResult;
            return Object.assign(result, {message, error});
        }
 
        result.success = true;
    } catch (err) {
        const traceId = uuidv4();
        logger.error(() => `${traceId}: Error when put-content with gitlab adapter`, err);
        result.success = false;
        result.message = `Непредвиденная ошибка: ${traceId}`;
        result.error = err.message;
    }
 
    return result;
};
 
/**
 * @typedef {{
 *     content: object,
 *     rootPath: string,
 *     userInfo?: {
 *       sub?: string,
 *       name?: string,
 *       surname?: string,
 *       email?: string
 *     }
 * }} CommitData
 */
 
/**
 * @param {CommitData} data
 */
export const commitToGitlab = async(data) => {
    const writeMode = process.env.VUE_APP_DOCHUB_GITLAB_MODE;
    const content = data?.content;
    if (!content || typeof content !== 'object') {
        return {
            success: false,
            message: 'Некорректные данные для коммита в GitLab',
            error: 'content is required'
        };
    }
    logger.debug(() => [
        `Write files to gitlab with write mode ${writeMode}`,
        {title: 'fileNames', obj: Object.keys(content)}
    ]);
    switch (writeMode) {
        case 'adapter':
            return commitToGitlabApiAdapter({...data, content});
        default:
            return {
                success: false,
                message: 'Запись в GitLab доступна только через adapter (VUE_APP_DOCHUB_GITLAB_MODE=adapter)',
                error: `Неподдерживаемый режим записи: ${writeMode}`
            };
    }
};