forked from Gitlink/forgeplus-react
210 lines
6.8 KiB
JavaScript
Executable File
210 lines
6.8 KiB
JavaScript
Executable File
import { apiService } from './api.service';
|
|
import { message } from 'antd';
|
|
import { getImageUrl } from "educoder";
|
|
import { calcChangeLineNum } from './utils/calc-change-line-num';
|
|
import { underscoreToCamelcase } from './utils/camelcase-convert';
|
|
import axios from 'axios';
|
|
import sha1 from 'sha1';
|
|
|
|
export const prService = {
|
|
async getPRByIid(projectsId, owner, mergeId) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}.json`);
|
|
let data = underscoreToCamelcase(res.data);
|
|
let newData = {
|
|
...data,
|
|
checkSuites: [],
|
|
iid: data.index,
|
|
sourceBranch: data.head,
|
|
sourceProjectId: projectsId,
|
|
// state: "opened",
|
|
// targetBranch: "master",
|
|
// targetProjectId: 42422,
|
|
diff: {
|
|
baseCommitSha: data.baseCommitSha,
|
|
commitsCount: data.commitNum,
|
|
headCommitSha: data.headCommitSha,
|
|
startCommitSha: data.mergeBase,
|
|
// delLineNum: 379,
|
|
// addLineNum: 753,
|
|
// createdAt: "2021-05-20T14:27:46+0800",
|
|
// filesCount: 22,
|
|
// overflow: false,
|
|
// startCommitSha: "2566c6dec7756e51f7f16267d9a2a63116ac015b",
|
|
// updatedAt: "2021-05-20T14:27:46+0800",
|
|
},
|
|
|
|
};
|
|
console.log('getPRByIid');
|
|
console.log(newData);
|
|
return newData;
|
|
},
|
|
|
|
async getDiffVersions(projectsId, owner, mergeId) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions.json`);
|
|
return Array.isArray(res.data.versions) ? underscoreToCamelcase(res.data.versions) : [];
|
|
},
|
|
|
|
async getDiffs(
|
|
projectId,
|
|
owner,
|
|
from,
|
|
to,
|
|
) {
|
|
const res = await axios.get(`/v1/${owner}/42422/compare.json`, { params: { from, to } })
|
|
console.log(res);
|
|
const changeLineNum = calcChangeLineNum(res.diffs);
|
|
return {
|
|
...res,
|
|
...changeLineNum,
|
|
};
|
|
},
|
|
|
|
async getDiffOverviews(projectsId, owner, mergeId, versionId) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions/${versionId}/diff.json`);
|
|
let files = res.data.files;
|
|
console.log('getDiffOverviews');
|
|
console.log(res.data.files);
|
|
for (const [i, item] of files.entries()) {
|
|
// item.aMode= "100644";
|
|
// item.bMode= "0";
|
|
// item.markAsRead= true;
|
|
// item.tooLarge= false;
|
|
// item.updatedAfterRead= false;
|
|
|
|
item.compareDiffId = versionId;
|
|
item.id = item.name;
|
|
item.addLineNum = item.addition;
|
|
item.binaryFile = item.is_bin;
|
|
item.delLineNum = item.deletion;
|
|
item.deletedFile = item.is_deleted;
|
|
item.newFile = item.is_created;
|
|
item.newPath = item.name;
|
|
item.oldPath = item.oldname;
|
|
item.renamedFile = item.is_renamed;
|
|
}
|
|
return Array.isArray(res.data.files) ? underscoreToCamelcase(res.data.files) : []
|
|
},
|
|
|
|
async getCommentPack(projectsId, owner, mergeId,) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals.json`);
|
|
console.log('getCommentPack');
|
|
console.log(underscoreToCamelcase(res.data.journals));
|
|
let journals=underscoreToCamelcase(res.data.journals);
|
|
for(const item of journals){
|
|
item.author=item.user;
|
|
item.author.avatarUrl=item.user.imageUrl?getImageUrl(item.user.imageUrl):'';
|
|
item.author.username=item.user.name;
|
|
item.type='Common';
|
|
item.stDiff=item.diff;
|
|
}
|
|
return journals;
|
|
},
|
|
|
|
async editPRComment(projectsId, owner, mergeId, noteId, data) {
|
|
const res = await axios.patch(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals/${noteId}.json`, data);
|
|
console.log('editPRComment');
|
|
console.log(res);
|
|
return res.data;
|
|
// return (await apiService.put(
|
|
// `/api/v3/projects/42422/pull_requests/13055/comments/${noteId}`,
|
|
// undefined,
|
|
// payload
|
|
// ))
|
|
},
|
|
|
|
async getDiffById(
|
|
projectsId, owner, mergeId, versionId,
|
|
options = {}
|
|
) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/versions/${versionId}/diff.json`, { params: options });
|
|
|
|
let data = res.data;
|
|
data.compareDiffId = versionId;
|
|
data.id = data.name;
|
|
data.addLineNum = data.addition;
|
|
data.binaryFile = data.is_bin;
|
|
data.delLineNum = data.deletion;
|
|
data.deletedFile = data.is_deleted;
|
|
data.newFile = data.is_created;
|
|
data.newPath = data.name;
|
|
data.oldPath = data.oldname;
|
|
data.renamedFile = data.is_renamed;
|
|
|
|
console.log('getDiffById');
|
|
console.log(res.data);
|
|
|
|
return data;
|
|
},
|
|
|
|
async getFileReadMarks(projectsId, owner, mergeId,) {
|
|
const res = await axios.get(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_files.json`);
|
|
let data=[];
|
|
for (const item of res.data.files) {
|
|
if(item.mark_as_read){
|
|
item.newPath = item.name;
|
|
item.file_path_sha2 = item.file_path_sha;
|
|
item.file_path_sha = sha1(item.newPath);
|
|
data.push(item);
|
|
}
|
|
}
|
|
return underscoreToCamelcase(data);
|
|
},
|
|
|
|
async markFileAsRead(projectsId, owner, mergeId, data) {
|
|
const res = await axios.put(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_file_as_read.json`, data);
|
|
return res.data;
|
|
},
|
|
async markFileAsUnread(projectsId, owner, mergeId, data) {
|
|
const res = await axios.put(`/${owner}/${projectsId}/pulls/${mergeId}/diffs/mark_file_as_unread.json`, data);
|
|
return res.data;
|
|
},
|
|
|
|
async addComment(projectsId, owner, mergeId, data) {
|
|
const res = await axios.post(`/v1/${owner}/${projectsId}/pulls/${mergeId}/journals.json`, data);
|
|
return res.data;
|
|
},
|
|
|
|
async createReview(projectsId, owner, mergeId, data) {
|
|
const res = await axios.post(`/v1/${owner}/${projectsId}/pulls/${mergeId}/reviews.json`, data);
|
|
console.log('createReview--data');
|
|
if (res.data.id) {
|
|
return res.data;
|
|
} else {
|
|
message.error(res.data && res.data.message);
|
|
}
|
|
},
|
|
|
|
async getReviews(projectsId, owner, mergeId,) {
|
|
const res = await axios.get(`/v1/${owner}/${projectsId}/pulls/${mergeId}/reviews.json`,);
|
|
console.log('getReviews');
|
|
console.log(underscoreToCamelcase(res.data.reviews));
|
|
let reviews=underscoreToCamelcase(res.data.reviews);
|
|
for(const item of reviews){
|
|
item.author=item.reviewer;
|
|
item.author.avatarUrl=getImageUrl(item.reviewer.imageUrl);
|
|
item.author.username=item.reviewer.name;
|
|
item.author.webUrl='';
|
|
item.body=item.content;
|
|
item.pending=false;
|
|
item.pullRequestId=mergeId;
|
|
}
|
|
return reviews;
|
|
// for (const item of res.data.files) {
|
|
// item.newPath = item.name;
|
|
// item.file_path_sha2 = item.file_path_sha;
|
|
// item.file_path_sha = sha1(item.newPath)
|
|
// }
|
|
// return underscoreToCamelcase(res.data.files);
|
|
},
|
|
|
|
async commitReview(projectsId, owner, mergeId, body) {
|
|
return await apiService.put(
|
|
`/api/v3/projects/42422/pull_requests/13055/reviews`,
|
|
undefined,
|
|
{
|
|
body,
|
|
}
|
|
);
|
|
},
|
|
};
|