Skip to content

Commit

Permalink
Revert "Move types out from "modules/structs" to not pollute api"
Browse files Browse the repository at this point in the history
This reverts commit 0f61e59.
  • Loading branch information
sahinakkaya committed Feb 16, 2024
1 parent 69ed1a4 commit df172cf
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 37 deletions.
17 changes: 17 additions & 0 deletions modules/structs/repo_collaborator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
6 changes: 6 additions & 0 deletions modules/structs/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
51 changes: 14 additions & 37 deletions services/repository/contributors_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -108,15 +85,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]*ContributorData:
case map[string]*api.ContributorData:
return v, nil
default:
return nil, fmt.Errorf("unexpected type in cache detected")
}
}

// 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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]

Expand All @@ -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),
}
}
}
Expand All @@ -284,15 +261,15 @@ 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,
Week: week,
}
}
if total.Weeks[week] == nil {
total.Weeks[week] = &WeekData{
total.Weeks[week] = &api.WeekData{
Additions: 0,
Deletions: 0,
Commits: 0,
Expand Down
40 changes: 40 additions & 0 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit df172cf

Please sign in to comment.