"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBranch = exports.getCombinedCommitStatus = exports.renovateToGiteaStatusMapping = exports.giteaToRenovateStatusMapping = exports.createCommitStatus = exports.getComments = exports.deleteComment = exports.updateComment = exports.createComment = exports.unassignLabel = exports.getOrgLabels = exports.getRepoLabels = exports.getIssue = exports.searchIssues = exports.closeIssue = exports.updateIssueLabels = exports.updateIssue = exports.createIssue = exports.requestPrReviewers = exports.getPR = exports.mergePR = exports.closePR = exports.updatePR = exports.createPR = exports.getRepoContents = exports.getRepo = exports.searchRepos = exports.getVersion = exports.getCurrentUser = exports.giteaHttp = void 0;
const gitea_1 = require("../../../util/http/gitea");
const url_1 = require("../../../util/url");
const utils_1 = require("./utils");
exports.giteaHttp = new gitea_1.GiteaHttp();
const urlEscape = (raw) => encodeURIComponent(raw);
const commitStatusStates = [
    'unknown',
    'success',
    'pending',
    'warning',
    'failure',
    'error',
];
async function getCurrentUser(options) {
    const url = `${utils_1.API_PATH}/user`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getCurrentUser = getCurrentUser;
async function getVersion(options) {
    const url = `${utils_1.API_PATH}/version`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body.version;
}
exports.getVersion = getVersion;
async function searchRepos(params, options) {
    const query = (0, url_1.getQueryString)(params);
    const url = `${utils_1.API_PATH}/repos/search?${query}`;
    const res = await exports.giteaHttp.getJson(url, {
        ...options,
        paginate: true,
    });
    if (!res.body.ok) {
        throw new Error('Unable to search for repositories, ok flag has not been set');
    }
    return res.body.data;
}
exports.searchRepos = searchRepos;
async function getRepo(repoPath, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getRepo = getRepo;
async function getRepoContents(repoPath, filePath, ref, options) {
    const query = (0, url_1.getQueryString)(ref ? { ref } : {});
    const url = `${utils_1.API_PATH}/repos/${repoPath}/contents/${urlEscape(filePath)}?${query}`;
    const res = await exports.giteaHttp.getJson(url, options);
    if (res.body.content) {
        res.body.contentString = Buffer.from(res.body.content, 'base64').toString();
    }
    return res.body;
}
exports.getRepoContents = getRepoContents;
async function createPR(repoPath, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/pulls`;
    const res = await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.createPR = createPR;
async function updatePR(repoPath, idx, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/pulls/${idx}`;
    const res = await exports.giteaHttp.patchJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.updatePR = updatePR;
async function closePR(repoPath, idx, options) {
    await updatePR(repoPath, idx, {
        ...options,
        state: 'closed',
    });
}
exports.closePR = closePR;
async function mergePR(repoPath, idx, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/pulls/${idx}/merge`;
    await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
}
exports.mergePR = mergePR;
async function getPR(repoPath, idx, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/pulls/${idx}`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getPR = getPR;
async function requestPrReviewers(repoPath, idx, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/pulls/${idx}/requested_reviewers`;
    await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
}
exports.requestPrReviewers = requestPrReviewers;
async function createIssue(repoPath, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues`;
    const res = await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.createIssue = createIssue;
async function updateIssue(repoPath, idx, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${idx}`;
    const res = await exports.giteaHttp.patchJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.updateIssue = updateIssue;
async function updateIssueLabels(repoPath, idx, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${idx}/labels`;
    const res = await exports.giteaHttp.putJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.updateIssueLabels = updateIssueLabels;
async function closeIssue(repoPath, idx, options) {
    await updateIssue(repoPath, idx, {
        ...options,
        state: 'closed',
    });
}
exports.closeIssue = closeIssue;
async function searchIssues(repoPath, params, options) {
    const query = (0, url_1.getQueryString)({ ...params, type: 'issues' });
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues?${query}`;
    const res = await exports.giteaHttp.getJson(url, {
        ...options,
        paginate: true,
    });
    return res.body;
}
exports.searchIssues = searchIssues;
async function getIssue(repoPath, idx, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${idx}`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getIssue = getIssue;
async function getRepoLabels(repoPath, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/labels`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getRepoLabels = getRepoLabels;
async function getOrgLabels(orgName, options) {
    const url = `${utils_1.API_PATH}/orgs/${orgName}/labels`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getOrgLabels = getOrgLabels;
async function unassignLabel(repoPath, issue, label, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${issue}/labels/${label}`;
    await exports.giteaHttp.deleteJson(url, options);
}
exports.unassignLabel = unassignLabel;
async function createComment(repoPath, issue, body, options) {
    const params = { body };
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${issue}/comments`;
    const res = await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.createComment = createComment;
async function updateComment(repoPath, idx, body, options) {
    const params = { body };
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/comments/${idx}`;
    const res = await exports.giteaHttp.patchJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.updateComment = updateComment;
async function deleteComment(repoPath, idx, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/comments/${idx}`;
    await exports.giteaHttp.deleteJson(url, options);
}
exports.deleteComment = deleteComment;
async function getComments(repoPath, issue, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/issues/${issue}/comments`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getComments = getComments;
async function createCommitStatus(repoPath, branchCommit, params, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/statuses/${branchCommit}`;
    const res = await exports.giteaHttp.postJson(url, {
        ...options,
        body: params,
    });
    return res.body;
}
exports.createCommitStatus = createCommitStatus;
exports.giteaToRenovateStatusMapping = {
    unknown: 'yellow',
    success: 'green',
    pending: 'yellow',
    warning: 'red',
    failure: 'red',
    error: 'red',
};
exports.renovateToGiteaStatusMapping = {
    green: 'success',
    yellow: 'pending',
    red: 'failure',
};
function filterStatus(data) {
    const ret = {};
    for (const i of data) {
        if (!ret[i.context] ||
            new Date(ret[i.context].created_at) < new Date(i.created_at)) {
            ret[i.context] = i;
        }
    }
    return Object.values(ret);
}
async function getCombinedCommitStatus(repoPath, branchName, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/commits/${urlEscape(branchName)}/statuses`;
    const res = await exports.giteaHttp.getJson(url, {
        ...options,
        paginate: true,
    });
    let worstState = 0;
    for (const cs of filterStatus(res.body)) {
        worstState = Math.max(worstState, commitStatusStates.indexOf(cs.status));
    }
    return {
        worstStatus: commitStatusStates[worstState],
        statuses: res.body,
    };
}
exports.getCombinedCommitStatus = getCombinedCommitStatus;
async function getBranch(repoPath, branchName, options) {
    const url = `${utils_1.API_PATH}/repos/${repoPath}/branches/${urlEscape(branchName)}`;
    const res = await exports.giteaHttp.getJson(url, options);
    return res.body;
}
exports.getBranch = getBranch;
//# sourceMappingURL=gitea-helper.js.map