From c85cdbbcc449010e49e73824b3bb87cc9b5eface Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 24 Oct 2022 16:19:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E6=96=B0=E5=A2=9ERe?= =?UTF-8?q?adme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 74 +++++++++++++++++++++++++++++++++ modules/structs/repo_tag.go | 5 +++ routers/hat/hat.go | 83 +++++++++++++++++++++++++++++++++++-- routers/hat/repo/tag.go | 47 +++++++++++---------- 4 files changed, 182 insertions(+), 27 deletions(-) create mode 100644 README.md create mode 100644 modules/structs/repo_tag.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..a0207de --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +

+ + Project logo +

+ +

Gitea GitLink版本

+ +
+ +[![Status](https://img.shields.io/badge/status-active-success.svg)]() +[![GitLink Issues](https://img.shields.io/github/issues/kylelobo/The-Documentation-Compendium.svg)](https://www.gitlink.org.cn/Gitlink/gitea_hat/issues) +[![GitLink Pull Requests](https://img.shields.io/github/issues-pr/kylelobo/The-Documentation-Compendium.svg)](https://www.gitlink.org.cn/Gitlink/gitea_hat/pulls) +[![License](https://img.shields.io/badge/license-MIT-blue.svg)](/LICENSE) + +
+ +--- + +

此版本为GitLink平台使用的Git服务,该服务基于Gitea进行开发。 +
+

+ +## 📝 目录 + +- [📝 目录](#-目录) +- [🧐 关于](#-关于) +- [编译](#编译) +- [运行](#运行) +- [接口文档](#接口文档) +- [⛏️ 使用](#️-使用) + +## 🧐 关于 + +该项目是为了方便平台后续Gitea版本的升级,将个性化接口部分从项目中抽离出来的项目,模块定义遵循gitea规则。 + +## 编译 + +类Linux平台,在项目根目录运行: +```shell +# 编译Linux 64位可执行程序 +CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gitea main.go + +# 编译Windows 64位可执行程序 +CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gitea main.go + +# 编译mac 64位可执行程序 +CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o gitea main.go +``` + +Win平台,在项目根目录运行: +```shell +# 编译Linux 64位可执行程序 +SET CGO_ENABLED=0 +SET GOOS=linux +SET GOARCH=amd64 +go build -o gitea main.go + +... +``` + +## 运行 + +在项目根目录下运行: + +``` +./gitea -p 10082 +``` + +## 接口文档 +- [ApiDoc](https://www.apifox.cn/apidoc/project-1808982) - 接口文档 + +## ⛏️ 使用 + +- [Gitea](https://gitea.io/zh-cn/) - Gitea diff --git a/modules/structs/repo_tag.go b/modules/structs/repo_tag.go new file mode 100644 index 0000000..251aa00 --- /dev/null +++ b/modules/structs/repo_tag.go @@ -0,0 +1,5 @@ +package structs + +type BranchNameSet struct { + BranchName []*string `json:"branch_name"` +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index dfcb82a..cd0b2f5 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -2,13 +2,18 @@ package hat import ( "net/http" + "strings" + access_model "code.gitea.io/gitea/models/perm/access" + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/misc" "code.gitea.io/gitea/services/auth" + "code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat/repo" "github.com/go-chi/cors" ) @@ -41,12 +46,15 @@ func Routers() *web.Route { SignInRequired: setting.Service.RequireSignInView, })) - m.Use(context.ToggleAPI(&context.ToggleOptions{ - SignInRequired: setting.Service.RequireSignInView, - })) - m.Group("", func() { m.Get("/version", misc.Version) + m.Group("/repos", func() { + m.Group("/{username}/{reponame}", func() { + m.Get("/branch_name_set", context.ReferencesGitRepo(), repo.BranchNameSet) + + m.Get("/tag_name_set", context.ReferencesGitRepo(), repo.TagNameSet) + }, repoAssignment()) + }) }) return m @@ -75,3 +83,70 @@ func buildAuthGroup() *auth.Group { return group } + +func repoAssignment() func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + userName := ctx.Params("username") + repoName := ctx.Params("reponame") + + var ( + owner *user_model.User + err error + ) + + // Check if the user is the same as the repository owner. + if ctx.IsSigned && ctx.Doer.LowerName == strings.ToLower(userName) { + owner = ctx.Doer + } else { + owner, err = user_model.GetUserByName(ctx, userName) + if err != nil { + if user_model.IsErrUserNotExist(err) { + if redirectUserID, err := user_model.LookupUserRedirect(userName); err == nil { + context.RedirectToUser(ctx.Context, userName, redirectUserID) + } else if user_model.IsErrUserRedirectNotExist(err) { + ctx.NotFound("GetUserByName", err) + } else { + ctx.Error(http.StatusInternalServerError, "LookupUserRedirect", err) + } + } else { + ctx.Error(http.StatusInternalServerError, "GetUserByName", err) + } + return + } + } + ctx.Repo.Owner = owner + ctx.ContextUser = owner + + // Get repository. + repo, err := repo_model.GetRepositoryByName(owner.ID, repoName) + if err != nil { + if repo_model.IsErrRepoNotExist(err) { + redirectRepoID, err := repo_model.LookupRedirect(owner.ID, repoName) + if err == nil { + context.RedirectToRepo(ctx.Context, redirectRepoID) + } else if repo_model.IsErrRedirectNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "LookupRepoRedirect", err) + } + } else { + ctx.Error(http.StatusInternalServerError, "GetRepositoryByName", err) + } + return + } + + repo.Owner = owner + ctx.Repo.Repository = repo + + ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, repo, ctx.Doer) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetUserRepoPermission", err) + return + } + + if !ctx.Repo.HasAccess() { + ctx.NotFound() + return + } + } +} diff --git a/routers/hat/repo/tag.go b/routers/hat/repo/tag.go index 7427ce6..059058e 100644 --- a/routers/hat/repo/tag.go +++ b/routers/hat/repo/tag.go @@ -8,30 +8,31 @@ import ( "code.gitea.io/gitea/modules/log" ) +func BranchNameSet(ctx *context.APIContext) { + + searchName := ctx.Params("name") + branches, _, err := ctx.Repo.GitRepo.GetBranches(0, 0) //get count of the branch + if err != nil { + ctx.ServerError("GetBranches", err) + return + } + + if searchName == "" { + ctx.JSON(http.StatusOK, branches) + } else { + var branchNameSet []string + for _, branch := range branches { + + log.Info("branch is \n", branch) + if strings.Contains(branch.Name, searchName) { + branchNameSet = append(branchNameSet, branch.Name) + } + } + ctx.JSON(http.StatusOK, branchNameSet) + } +} + func TagNameSet(ctx *context.APIContext) { - // swagger:operation GET /repos/{owner}/{repo}/tag_name_set repository repoTagNameSet - // --- - // summary: List a repository's tag name*** - // produces: - // - application/json - // parameters: - // - name: owner - // in: path - // description: owner of the repo - // type: string - // required: true - // - name: repo - // in: path - // description: name of the repo - // type: string - // required: true - // - name: name - // in: query - // description: name of the tag - // type: string - // responses: - // "200": - // "$ref": "#/responses/BranchNameSet" searchName := ctx.Params("name")