From df172cf253f9c3a3baf43eb756ff10eb5e62632a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Sun, 28 Jan 2024 15:03:30 +0300 Subject: [PATCH] Revert "Move types out from "modules/structs" to not pollute api" This reverts commit 0f61e592e00ef57ae8c748fbec709d4edeeab637. --- 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 | 40 ++++++++++++++++++ 5 files changed, 84 insertions(+), 37 deletions(-) diff --git a/modules/structs/repo_collaborator.go b/modules/structs/repo_collaborator.go index 946a6ec7e78e0..a0c0b0cdd2b16 100644 --- a/modules/structs/repo_collaborator.go +++ b/modules/structs/repo_collaborator.go @@ -14,3 +14,20 @@ 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 map[int64]*WeekData `json:"weeks"` +} diff --git a/modules/structs/repo_commit.go b/modules/structs/repo_commit.go index fec7d97608d92..46ed99e197f88 100644 --- a/modules/structs/repo_commit.go +++ b/modules/structs/repo_commit.go @@ -58,6 +58,12 @@ 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 3e23aa4d5a5ac..a4e35b260f033 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -253,6 +253,13 @@ 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 8421df8e3ae7e..675d346ab7493 100644 --- a/services/repository/contributors_graph.go +++ b/services/repository/contributors_graph.go @@ -37,29 +37,6 @@ 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 map[int64]*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"` -} - const layout = time.DateOnly func findLastSundayBeforeDate(dateStr string) (string, error) { @@ -79,7 +56,7 @@ func findLastSundayBeforeDate(dateStr string) (string, error) { } // 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]*ContributorData, error) { +func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*api.ContributorData, error) { // as GetContributorStats is resource intensive we cache the result cacheKey := fmt.Sprintf(contributorStatsCacheKey, repo.FullName(), revision) if !cache.IsExist(cacheKey) { @@ -108,7 +85,7 @@ 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]*ContributorData: + case map[string]*api.ContributorData: return v, nil default: return nil, fmt.Errorf("unexpected type in cache detected") @@ -116,7 +93,7 @@ func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_mode } // getExtendedCommitStats return the list of *ExtendedCommitStats for the given revision -func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*ExtendedCommitStats, error) { +func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*api.ExtendedCommitStats, error) { baseCommit, err := repo.GetCommit(revision) if err != nil { return nil, err @@ -134,7 +111,7 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int // AddOptionFormat("--max-count=%d", limit) gitCmd.AddDynamicArguments(baseCommit.ID.String()) - var extendedCommitStats []*ExtendedCommitStats + var extendedCommitStats []*api.ExtendedCommitStats stderr := new(strings.Builder) err = gitCmd.Run(&git.RunOpts{ Dir: repo.Path, @@ -183,7 +160,7 @@ func getExtendedCommitStats(repo *git.Repository, revision string /*, limit int scanner.Scan() scanner.Text() // empty line at the end - res := &ExtendedCommitStats{ + res := &api.ExtendedCommitStats{ Author: &api.CommitUser{ Identity: api.Identity{ Name: authorName, @@ -236,10 +213,10 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey layout := time.DateOnly unknownUserAvatarLink := user_model.NewGhostUser().AvatarLinkWithSize(ctx, 0) - contributorsCommitStats := make(map[string]*ContributorData) - contributorsCommitStats["total"] = &ContributorData{ + contributorsCommitStats := make(map[string]*api.ContributorData) + contributorsCommitStats["total"] = &api.ContributorData{ Name: "Total", - Weeks: make(map[int64]*WeekData), + Weeks: make(map[int64]*api.WeekData), } total := contributorsCommitStats["total"] @@ -261,18 +238,18 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey if avatarLink == "" { avatarLink = unknownUserAvatarLink } - contributorsCommitStats[userEmail] = &ContributorData{ + contributorsCommitStats[userEmail] = &api.ContributorData{ Name: v.Author.Name, AvatarLink: avatarLink, - Weeks: make(map[int64]*WeekData), + Weeks: make(map[int64]*api.WeekData), } } else { - contributorsCommitStats[userEmail] = &ContributorData{ + contributorsCommitStats[userEmail] = &api.ContributorData{ Name: u.DisplayName(), Login: u.LowerName, AvatarLink: u.AvatarLinkWithSize(ctx, 0), HomeLink: u.HomeLink(), - Weeks: make(map[int64]*WeekData), + Weeks: make(map[int64]*api.WeekData), } } } @@ -284,7 +261,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey week := val.UnixMilli() if user.Weeks[week] == nil { - user.Weeks[week] = &WeekData{ + user.Weeks[week] = &api.WeekData{ Additions: 0, Deletions: 0, Commits: 0, @@ -292,7 +269,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey } } if total.Weeks[week] == nil { - total.Weeks[week] = &WeekData{ + total.Weeks[week] = &api.WeekData{ Additions: 0, Deletions: 0, Commits: 0, diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index a881afaf0ec63..a66ce03968f78 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -17585,6 +17585,37 @@ }, "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": { + "x-go-name": "Weeks" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CreateAccessTokenOption": { "description": "CreateAccessTokenOption options when create access token", "type": "object", @@ -23400,6 +23431,15 @@ "$ref": "#/definitions/ContentsResponse" } }, + "ContributorDataMap": { + "description": "ContributorDataMap", + "schema": { + "type": "object", + "additionalProperties": { + "$ref": "#/definitions/ContributorData" + } + } + }, "CronList": { "description": "CronList", "schema": {