From 0f61e592e00ef57ae8c748fbec709d4edeeab637 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= <sahin@sahinakkaya.dev>
Date: Thu, 25 Jan 2024 18:24:01 +0300
Subject: [PATCH] Move types out from "modules/structs" to not pollute api

---
 modules/structs/repo_collaborator.go      | 17 ------
 modules/structs/repo_commit.go            |  6 --
 routers/api/v1/swagger/repo.go            |  7 ---
 services/repository/contributors_graph.go | 51 ++++++++++++-----
 templates/swagger/v1_json.tmpl            | 70 -----------------------
 5 files changed, 37 insertions(+), 114 deletions(-)

diff --git a/modules/structs/repo_collaborator.go b/modules/structs/repo_collaborator.go
index 553546272beae..946a6ec7e78e0 100644
--- a/modules/structs/repo_collaborator.go
+++ b/modules/structs/repo_collaborator.go
@@ -14,20 +14,3 @@ type RepoCollaboratorPermission struct {
 	RoleName   string `json:"role_name"`
 	User       *User  `json:"user"`
 }
-
-type WeekData struct {
-	Week      int64 `json:"week"`      // Starting day of the week as Unix timestamp
-	Additions int   `json:"additions"` // Number of additions in that week
-	Deletions int   `json:"deletions"` // Number of deletions in that week
-	Commits   int   `json:"commits"`   // Number of commits in that week
-}
-
-// ContributorData represents statistical git commit count data
-type ContributorData struct {
-	Name         string      `json:"name"`  // Display name of the contributor
-	Login        string      `json:"login"` // Login name of the contributor in case it exists
-	AvatarLink   string      `json:"avatar_link"`
-	HomeLink     string      `json:"home_link"`
-	TotalCommits int64       `json:"total_commits"`
-	Weeks        []*WeekData `json:"weeks"`
-}
diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go
index 46ed99e197f88..fec7d97608d92 100644
--- a/modules/structs/repo_commit.go
+++ b/modules/structs/repo_commit.go
@@ -58,12 +58,6 @@ type Commit struct {
 	Stats      *CommitStats           `json:"stats"`
 }
 
-// ExtendedCommitStats contains information for commit stats with author data
-type ExtendedCommitStats struct {
-	Author *CommitUser  `json:"author"`
-	Stats  *CommitStats `json:"stats"`
-}
-
 // CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
 type CommitDateOptions struct {
 	// swagger:strfmt date-time
diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go
index a4e35b260f033..3e23aa4d5a5ac 100644
--- a/routers/api/v1/swagger/repo.go
+++ b/routers/api/v1/swagger/repo.go
@@ -253,13 +253,6 @@ type swaggerCommitList struct {
 	Body []api.Commit `json:"body"`
 }
 
-// ContributorDataMap
-// swagger:response ContributorDataMap
-type swaggerContributorDataMap struct {
-	// in: body
-	Body map[string]*api.ContributorData `json:"body"`
-}
-
 // ChangedFileList
 // swagger:response ChangedFileList
 type swaggerChangedFileList struct {
diff --git a/services/repository/contributors_graph.go b/services/repository/contributors_graph.go
index 09e2e4e33253e..5adfed380799d 100644
--- a/services/repository/contributors_graph.go
+++ b/services/repository/contributors_graph.go
@@ -37,11 +37,34 @@ var (
 	generateLock        = sync.Map{}
 )
 
+type WeekData struct {
+	Week      int64 `json:"week"`      // Starting day of the week as Unix timestamp
+	Additions int   `json:"additions"` // Number of additions in that week
+	Deletions int   `json:"deletions"` // Number of deletions in that week
+	Commits   int   `json:"commits"`   // Number of commits in that week
+}
+
+// ContributorData represents statistical git commit count data
+type ContributorData struct {
+	Name         string      `json:"name"`  // Display name of the contributor
+	Login        string      `json:"login"` // Login name of the contributor in case it exists
+	AvatarLink   string      `json:"avatar_link"`
+	HomeLink     string      `json:"home_link"`
+	TotalCommits int64       `json:"total_commits"`
+	Weeks        []*WeekData `json:"weeks"`
+}
+
+// ExtendedCommitStats contains information for commit stats with author data
+type ExtendedCommitStats struct {
+	Author *api.CommitUser  `json:"author"`
+	Stats  *api.CommitStats `json:"stats"`
+}
+
 // CreateWeeks converts list of sundays to list of *api.WeekData
-func CreateWeeks(sundays []int64) []*api.WeekData {
-	var weeks []*api.WeekData
+func CreateWeeks(sundays []int64) []*WeekData {
+	var weeks []*WeekData
 	for _, week := range sundays {
-		weeks = append(weeks, &api.WeekData{
+		weeks = append(weeks, &WeekData{
 			Week:      week,
 			Additions: 0,
 			Deletions: 0,
@@ -53,7 +76,7 @@ func CreateWeeks(sundays []int64) []*api.WeekData {
 }
 
 // GetContributorStats returns contributors stats for git commits for given revision or default branch
-func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*api.ContributorData, error) {
+func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*ContributorData, error) {
 	// as GetContributorStats is resource intensive we cache the result
 	cacheKey := fmt.Sprintf(contributorStatsCacheKey, repo.FullName(), revision)
 	if !cache.IsExist(cacheKey) {
@@ -82,15 +105,15 @@ func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_mode
 	switch v := cache.Get(cacheKey).(type) {
 	case error:
 		return nil, v
-	case map[string]*api.ContributorData:
+	case map[string]*ContributorData:
 		return v, nil
 	default:
 		return nil, fmt.Errorf("unexpected type in cache detected")
 	}
 }
 
-// ExtendedCommitStats return the list of *api.ExtendedCommitStats for the given revision
-func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*api.ExtendedCommitStats, error) {
+// GetExtendedCommitStats return the list of *ExtendedCommitStats for the given revision
+func GetExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*ExtendedCommitStats, error) {
 	baseCommit, err := repo.GetCommit(revision)
 	if err != nil {
 		return nil, err
@@ -108,7 +131,7 @@ func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */)
 	// AddOptionFormat("--max-count=%d", limit)
 	gitCmd.AddDynamicArguments(baseCommit.ID.String())
 
-	var extendedCommitStats []*api.ExtendedCommitStats
+	var extendedCommitStats []*ExtendedCommitStats
 	stderr := new(strings.Builder)
 	err = gitCmd.Run(&git.RunOpts{
 		Dir:    repo.Path,
@@ -157,7 +180,7 @@ func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */)
 				scanner.Scan()
 				scanner.Text() // empty line at the end
 
-				res := &api.ExtendedCommitStats{
+				res := &ExtendedCommitStats{
 					Author: &api.CommitUser{
 						Identity: api.Identity{
 							Name:  authorName,
@@ -194,7 +217,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
 	if len(revision) == 0 {
 		revision = repo.DefaultBranch
 	}
-	extendedCommitStats, err := ExtendedCommitStats(gitRepo, revision)
+	extendedCommitStats, err := GetExtendedCommitStats(gitRepo, revision)
 	if err != nil {
 		err := fmt.Errorf("ExtendedCommitStats: %w", err)
 		_ = cache.Put(cacheKey, err, contributorStatsCacheTimeout)
@@ -209,8 +232,8 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
 	sundays, _ := util.ListSundaysBetween(startingSunday, endingSunday)
 
 	unknownUserAvatarLink := user_model.NewGhostUser().AvatarLink(ctx)
-	contributorsCommitStats := make(map[string]*api.ContributorData)
-	contributorsCommitStats["total"] = &api.ContributorData{
+	contributorsCommitStats := make(map[string]*ContributorData)
+	contributorsCommitStats["total"] = &ContributorData{
 		Name:       "Total",
 		AvatarLink: unknownUserAvatarLink,
 		Weeks:      CreateWeeks(sundays),
@@ -234,13 +257,13 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
 				if avatarLink == "" {
 					avatarLink = unknownUserAvatarLink
 				}
-				contributorsCommitStats[userEmail] = &api.ContributorData{
+				contributorsCommitStats[userEmail] = &ContributorData{
 					Name:       v.Author.Name,
 					AvatarLink: avatarLink,
 					Weeks:      CreateWeeks(sundays),
 				}
 			} else {
-				contributorsCommitStats[userEmail] = &api.ContributorData{
+				contributorsCommitStats[userEmail] = &ContributorData{
 					Name:       u.DisplayName(),
 					Login:      u.LowerName,
 					AvatarLink: u.AvatarLink(ctx),
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index 49184faa80561..dc04a97b833c7 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -17585,41 +17585,6 @@
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"
     },
-    "ContributorData": {
-      "description": "ContributorData represents statistical git commit count data",
-      "type": "object",
-      "properties": {
-        "avatar_link": {
-          "type": "string",
-          "x-go-name": "AvatarLink"
-        },
-        "home_link": {
-          "type": "string",
-          "x-go-name": "HomeLink"
-        },
-        "login": {
-          "type": "string",
-          "x-go-name": "Login"
-        },
-        "name": {
-          "type": "string",
-          "x-go-name": "Name"
-        },
-        "total_commits": {
-          "type": "integer",
-          "format": "int64",
-          "x-go-name": "TotalCommits"
-        },
-        "weeks": {
-          "type": "array",
-          "items": {
-            "$ref": "#/definitions/WeekData"
-          },
-          "x-go-name": "Weeks"
-        }
-      },
-      "x-go-package": "code.gitea.io/gitea/modules/structs"
-    },
     "CreateAccessTokenOption": {
       "description": "CreateAccessTokenOption options when create access token",
       "type": "object",
@@ -23109,32 +23074,6 @@
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"
     },
-    "WeekData": {
-      "type": "object",
-      "properties": {
-        "additions": {
-          "type": "integer",
-          "format": "int64",
-          "x-go-name": "Additions"
-        },
-        "commits": {
-          "type": "integer",
-          "format": "int64",
-          "x-go-name": "Commits"
-        },
-        "deletions": {
-          "type": "integer",
-          "format": "int64",
-          "x-go-name": "Deletions"
-        },
-        "week": {
-          "type": "integer",
-          "format": "int64",
-          "x-go-name": "Week"
-        }
-      },
-      "x-go-package": "code.gitea.io/gitea/modules/structs"
-    },
     "WikiCommit": {
       "description": "WikiCommit page commit/revision",
       "type": "object",
@@ -23447,15 +23386,6 @@
         "$ref": "#/definitions/ContentsResponse"
       }
     },
-    "ContributorDataMap": {
-      "description": "ContributorDataMap",
-      "schema": {
-        "type": "object",
-        "additionalProperties": {
-          "$ref": "#/definitions/ContributorData"
-        }
-      }
-    },
     "CronList": {
       "description": "CronList",
       "schema": {