t-comm
Version:
专业、稳定、纯粹的工具库
128 lines (123 loc) • 4.08 kB
JavaScript
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var tgit_helper = require('./helper.js');
require('axios');
/**
* 获取仓库中指定文件的内容(自动 base64 解码)
*
* 对应工蜂 API:GET /api/v3/projects/:id/repository/files
*
* @param {object} options 输入配置
* @param {string | number} options.projectName 项目名称或项目 ID
* @param {string} options.filePath 文件路径(如 src/utils/helper.ts)
* @param {string} [options.ref='master'] 分支名或 commit SHA
* @param {string} options.privateToken 密钥
* @param {string} [options.baseUrl] baseUrl
* @returns {Promise<RepoFile>} 文件内容(已解码)
* @example
*
* getFileContent({
* projectName: 'group/sub/repo',
* filePath: 'src/utils/helper.ts',
* ref: 'master',
* privateToken: 'xxxxx',
* }).then(file => console.log(file.content));
*/
function getFileContent(_ref) {
var projectName = _ref.projectName,
filePath = _ref.filePath,
_ref$ref = _ref.ref,
ref = _ref$ref === void 0 ? 'master' : _ref$ref,
privateToken = _ref.privateToken,
baseUrl = _ref.baseUrl;
return new Promise(function (resolve, reject) {
if (!projectName || !filePath) {
reject(new Error('projectName, filePath is required'));
return;
}
var url = tgit_helper.resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(tgit_helper.encodeProject(projectName), "/repository/files"));
tgit_helper.instance({
url: url,
method: 'GET',
params: {
file_path: filePath,
ref: ref
},
headers: {
'PRIVATE-TOKEN': privateToken
}
}).then(function (res) {
var raw = res.data;
resolve({
content: (raw === null || raw === void 0 ? void 0 : raw.content) ? tgit_helper.base64DecodeUtf8(raw.content) : '',
encoding: raw === null || raw === void 0 ? void 0 : raw.encoding,
sha: raw === null || raw === void 0 ? void 0 : raw.blob_id
});
})["catch"](function (err) {
reject(err);
});
});
}
/**
* 更新仓库中的文件(通过 API 提交,不需要 clone)
*
* 对应工蜂 API:PUT /api/v3/projects/:id/repository/files
*
* @param {object} options 输入配置
* @param {string | number} options.projectName 项目名称或项目 ID
* @param {string} options.filePath 文件路径
* @param {string} options.content 新的文件内容(纯文本)
* @param {string} options.commitMessage 提交消息
* @param {string} options.branch 目标分支
* @param {string} options.privateToken 密钥
* @param {string} [options.baseUrl] baseUrl
* @returns {Promise<{ file_path: string; branch_name: string } & Record<string, unknown>>}
* @example
*
* updateFile({
* projectName: 'group/sub/repo',
* filePath: 'src/utils/helper.ts',
* content: 'export const x = 1;\n',
* commitMessage: 'chore: update helper',
* branch: 'master',
* privateToken: 'xxxxx',
* }).then((resp) => {
*
* })
*/
function updateFile(_ref2) {
var projectName = _ref2.projectName,
filePath = _ref2.filePath,
content = _ref2.content,
commitMessage = _ref2.commitMessage,
branch = _ref2.branch,
privateToken = _ref2.privateToken,
baseUrl = _ref2.baseUrl;
return new Promise(function (resolve, reject) {
if (!projectName || !filePath || !branch) {
reject(new Error('projectName, filePath, branch is required'));
return;
}
var url = tgit_helper.resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(tgit_helper.encodeProject(projectName), "/repository/files"));
tgit_helper.instance({
url: url,
method: 'PUT',
data: {
file_path: filePath,
branch_name: branch,
content: content,
commit_message: commitMessage,
encoding: 'text'
},
headers: {
'PRIVATE-TOKEN': privateToken
}
}).then(function (res) {
resolve(res.data);
})["catch"](function (err) {
reject(err);
});
});
}
exports.getFileContent = getFileContent;
exports.updateFile = updateFile;