All files / backend/helpers/gitcommit utils.mjs

97.29% Statements 36/37
100% Branches 10/10
75% Functions 3/4
100% Lines 36/36

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                                          45x   45x 8x 8x 14x   8x       45x 6x   3x 3x 3x   3x           3x   2x               1x                 45x 9x       9x 9x 9x 9x 9x 9x 9x 7x   9x 9x   9x         9x                 9x 9x           3x   6x 6x 6x     9x      
/*
  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 {strToU8, zipSync} from 'fflate';
import FormData from 'form-data';
import axios from 'axios';
import {getLoggerWithTag} from '@global/logger/v2/logger.mjs';
 
const logger = getLoggerWithTag('b/h/gitcommit/utils');
 
export const buildZipArchive = (contentObject) => {
    const files = {};
    for (const path in contentObject) {
        files[path] = strToU8(contentObject[path]);
    }
    return Buffer.from(zipSync(files));
};
 
 
const parseAdapterError = (error) => {
    if (axios.isAxiosError(error)) {
        // Если это ошибка axios
        const status = error.response?.status || 0;
        const statusText = error.response?.data?.errorCode || 'Unknown code';
        const message = `id = ${error.response?.data?.errorId}: ${error.response?.data?.errorMessage}`;
 
        return {
            status,
            statusText,
            message,
            isAxiosError: true
        };
    } else if (error instanceof Error) {
        // Если это обычная ошибка JS
        return {
            status: 400,
            statusText: 'Client Error',
            message: error.message,
            isAxiosError: false
        };
    } else {
        // Если это что-то еще
        return {
            status: 400,
            statusText: 'Unknown Error',
            message: String(error),
            isAxiosError: false
        };
    }
};
 
export const sendArchiveToAdapter = async({adapterUrl, archiveBuffer, checksum, repoUrl, branch, processId, sub, name, surname, email}) => {
    const result = {
        success: false
    };
 
    const body = new FormData();
    body.append('process_id', processId);
    body.append('repo_url', repoUrl);
    body.append('sub', sub);
    body.append('name', name);
    body.append('surname', surname);
    if (email) {
        body.append('email', email);
    }
    body.append('branch_name', branch);
    body.append('file', archiveBuffer, {filename: 'archive.zip', contentType: 'application/zip'});
 
    const headers = {
        ...body.getHeaders(),
        'X-Checksum': checksum
    };
 
    logger.debug(() => [
        `send request to gas [POST] ${adapterUrl}`,
        {title: 'process_id', obj: processId},
        {title: 'repo_url', obj: repoUrl},
        {title: 'sub', obj: sub},
        {title: 'branch_name', obj: branch},
        {title: 'X-Checksum', obj: checksum}
    ]);
 
    try {
        await axios({
            method: 'POST',
            url: adapterUrl,
            headers: headers,
            data: body
        });
        result.success = true;
    } catch (err) {
        const axiosError = parseAdapterError(err);
        result.message = 'Ошибка при отправке файлов в gas';
        result.error = `${axiosError.status}: ${axiosError.statusText}: ${axiosError.message}`;
    }
 
    return result;
};