From 7cc49acd7e51c17b89ee3ca255d5d9a3fb1618d0 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 11 Nov 2024 10:50:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9:=20=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=97=A7=E7=89=88=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/convert/convert.go | 28 +++++++++++++++++ modules/structs/pull.go | 21 +++++++++++++ routers/hat/repo/commits.go | 16 +++++++--- routers/hat/repo/pull.go | 61 ++++++++++++++++++++++++++++++++----- routers/hat/repo/repo.go | 20 ++++++++---- 5 files changed, 129 insertions(+), 17 deletions(-) diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 8e45082..873e314 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -2,15 +2,18 @@ package convert import ( "encoding/json" + "fmt" "strings" "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/repo" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/util" gitea_convert "code.gitea.io/gitea/services/convert" + "code.gitea.io/gitea/services/gitdiff" api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" ) @@ -92,3 +95,28 @@ func ToHookTask(t *webhook.HookTask) *api.HookTask { ResponseContent: responseContent, } } + +func ToChangedFile(f *gitdiff.DiffFile, repo *repo_model.Repository, commit string) *api.ChangedFile { + + file := &api.ChangedFile{ + Filename: f.GetDiffFileName(), + OldName: f.OldName, + Index: f.Index, + Type: f.Type, + IsBin: f.IsBin, + IsCreated: f.IsCreated, + IsDeleted: f.IsDeleted, + IsLFSFile: f.IsLFSFile, + IsRenamed: f.IsRenamed, + IsSubmodule: f.IsSubmodule, + Additions: f.Addition, + Deletions: f.Deletion, + Changes: f.Addition + f.Deletion, + Sha: commit, + HTMLURL: fmt.Sprint(repo.HTMLURL(), "/src/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), + ContentsURL: fmt.Sprint(repo.APIURL(), "/contents/", util.PathEscapeSegments(f.GetDiffFileName()), "?ref=", commit), + RawURL: fmt.Sprint(repo.HTMLURL(), "/raw/commit/", commit, "/", util.PathEscapeSegments(f.GetDiffFileName())), + } + + return file +} diff --git a/modules/structs/pull.go b/modules/structs/pull.go index 92eb5f4..3d30804 100644 --- a/modules/structs/pull.go +++ b/modules/structs/pull.go @@ -4,6 +4,7 @@ import ( "time" gitea_api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/services/gitdiff" ) type PullRequest struct { @@ -39,3 +40,23 @@ type PullRequest struct { CommitNum int `json:"commit_num"` ChangedFiles int `json:"changed_files"` } + +type ChangedFile struct { + Filename string `json:"filename"` + OldName string `json:"old_name"` + Index int `json:"index"` + Type gitdiff.DiffFileType `json:"type"` + IsBin bool `json:"is_bin"` + IsCreated bool `json:"is_created"` + IsDeleted bool `json:"is_deleted"` + IsLFSFile bool `json:"is_lfs_file"` + IsRenamed bool `json:"is_renamed"` + IsSubmodule bool `json:"is_submodule"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + Changes int `json:"changes"` + Sha string `json:"sha"` + HTMLURL string `json:"html_url,omitempty"` + ContentsURL string `json:"contents_url,omitempty"` + RawURL string `json:"raw_url,omitempty"` +} diff --git a/routers/hat/repo/commits.go b/routers/hat/repo/commits.go index 748138a..ce56b81 100644 --- a/routers/hat/repo/commits.go +++ b/routers/hat/repo/commits.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/services/gitdiff" hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git" + hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" ) func GetAllCommitsSliceByTime(ctx *context.APIContext) { @@ -358,11 +359,10 @@ func GetCommitFiles(ctx *context.APIContext) { lenFiles = 0 } - apiFiles := make([]*api.ChangedFile, 0, lenFiles) + apiFiles := make([]*hat_api.ChangedFile, 0, lenFiles) for i := start; i < end; i++ { - apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, commitID)) + apiFiles = append(apiFiles, hat_convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, commitID)) } - ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) @@ -372,7 +372,15 @@ func GetCommitFiles(ctx *context.APIContext) { ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") - ctx.JSON(http.StatusOK, &apiFiles) + ctx.JSON(http.StatusOK, struct { + Files []*hat_api.ChangedFile `json:"files"` + TotalAddition int `json:"total_addition"` + TotalDeletion int `json:"total_deletion"` + }{ + Files: apiFiles, + TotalAddition: diff.TotalAddition, + TotalDeletion: diff.TotalDeletion, + }) } func GetCommitFilesByPath(ctx *context.APIContext) { diff --git a/routers/hat/repo/pull.go b/routers/hat/repo/pull.go index 90b9c9b..b02d826 100644 --- a/routers/hat/repo/pull.go +++ b/routers/hat/repo/pull.go @@ -3,7 +3,9 @@ package repo import ( "errors" "fmt" + "math" "net/http" + "strconv" "strings" "time" @@ -14,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/routers/api/v1/utils" "code.gitea.io/gitea/services/gitdiff" "code.gitea.io/gitea/modules/context" @@ -28,6 +31,7 @@ import ( notify_service "code.gitea.io/gitea/services/notify" pull_service "code.gitea.io/gitea/services/pull" hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" + hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" hat_pull_service "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/pull" ) @@ -391,16 +395,59 @@ func GetPullFiles(ctx *context.APIContext) { ctx.ServerError("GetDiff", err) return } + isNew := ctx.FormString("isNew") + if isNew != "" { + listOptions := utils.GetListOptions(ctx) - fileDiff := struct { - *gitdiff.Diff - LatestSha string - }{ - Diff: diff, - LatestSha: endCommitID, + totalNumberOfFiles := diff.NumFiles + totalNumberOfPages := int(math.Ceil(float64(totalNumberOfFiles) / float64(listOptions.PageSize))) + + start, end := listOptions.GetStartEnd() + + if end > totalNumberOfFiles { + end = totalNumberOfFiles + } + + lenFiles := end - start + if lenFiles < 0 { + lenFiles = 0 + } + apiFiles := make([]*hat_api.ChangedFile, 0, lenFiles) + for i := start; i < end; i++ { + apiFiles = append(apiFiles, hat_convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, headCommitID)) + } + + ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) + ctx.SetTotalCountHeader(int64(totalNumberOfFiles)) + + ctx.RespHeader().Set("X-Page", strconv.Itoa(listOptions.Page)) + ctx.RespHeader().Set("X-PerPage", strconv.Itoa(listOptions.PageSize)) + ctx.RespHeader().Set("X-PageCount", strconv.Itoa(totalNumberOfPages)) + ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) + ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") + ctx.JSON(http.StatusOK, struct { + Files []*hat_api.ChangedFile `json:"files"` + TotalAddition int `json:"total_addition"` + TotalDeletion int `json:"total_deletion"` + }{ + Files: apiFiles, + TotalAddition: diff.TotalAddition, + TotalDeletion: diff.TotalDeletion, + }) + + } else { + + fileDiff := struct { + *gitdiff.Diff + LatestSha string + }{ + Diff: diff, + LatestSha: endCommitID, + } + + ctx.JSON(http.StatusOK, fileDiff) } - ctx.JSON(http.StatusOK, fileDiff) } func GetPullFilesByPath(ctx *context.APIContext) { diff --git a/routers/hat/repo/repo.go b/routers/hat/repo/repo.go index 1908a13..fa55573 100644 --- a/routers/hat/repo/repo.go +++ b/routers/hat/repo/repo.go @@ -19,11 +19,11 @@ import ( "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" - api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/routers/api/v1/utils" - "code.gitea.io/gitea/services/convert" "code.gitea.io/gitea/services/gitdiff" + hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git" + hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" hat_repo_service "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/repository" ) @@ -179,9 +179,9 @@ func CompareFiles(ctx *context.APIContext) { lenFiles = 0 } - apiFiles := make([]*api.ChangedFile, 0, lenFiles) + apiFiles := make([]*hat_api.ChangedFile, 0, lenFiles) for i := start; i < end; i++ { - apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, headCommitID)) + apiFiles = append(apiFiles, hat_convert.ToChangedFile(diff.Files[i], ctx.Repo.Repository, headCommitID)) } ctx.SetLinkHeader(totalNumberOfFiles, listOptions.PageSize) @@ -193,7 +193,15 @@ func CompareFiles(ctx *context.APIContext) { ctx.RespHeader().Set("X-HasMore", strconv.FormatBool(listOptions.Page < totalNumberOfPages)) ctx.AppendAccessControlExposeHeaders("X-Page", "X-PerPage", "X-PageCount", "X-HasMore") - ctx.JSON(http.StatusOK, &apiFiles) + ctx.JSON(http.StatusOK, struct { + Files []*hat_api.ChangedFile `json:"files"` + TotalAddition int `json:"total_addition"` + TotalDeletion int `json:"total_deletion"` + }{ + Files: apiFiles, + TotalAddition: diff.TotalAddition, + TotalDeletion: diff.TotalDeletion, + }) } @@ -253,7 +261,7 @@ func ParseCompareInfo(ctx *context.APIContext) (*user_model.User, *repo_model.Re if shaFrom == "" || shaTo == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - infos = []string{shaFrom, shaTo} + infos = []string{shaTo, shaFrom} } ctx.Data["BaseName"] = baseRepo.OwnerName