Skip to content

Commit

Permalink
Go fileserver check repo type
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Jun 18, 2024
1 parent 7367f1e commit eaebe84
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
41 changes: 27 additions & 14 deletions fileserver/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,15 @@ type SharedRepo struct {
Permission string `json:"permission"`
Type string `json:"type"`
Owner string `json:"owner"`
RepoType string `json:"-"`
}

// GetReposByOwner get repos by owner
func GetReposByOwner(email string) ([]*SharedRepo, error) {
var repos []*SharedRepo

query := "SELECT o.repo_id, b.commit_id, i.name, " +
"i.version, i.update_time, i.last_modifier FROM " +
"i.version, i.update_time, i.last_modifier, i.type FROM " +
"RepoOwner o LEFT JOIN Branch b ON o.repo_id = b.repo_id " +
"LEFT JOIN RepoInfo i ON o.repo_id = i.repo_id " +
"LEFT JOIN VirtualRepo v ON o.repo_id = v.repo_id " +
Expand All @@ -443,10 +444,10 @@ func GetReposByOwner(email string) ([]*SharedRepo, error) {

for rows.Next() {
repo := new(SharedRepo)
var repoName, lastModifier sql.NullString
var repoName, lastModifier, repoType sql.NullString
if err := rows.Scan(&repo.ID, &repo.HeadCommitID,
&repoName, &repo.Version, &repo.MTime,
&lastModifier); err == nil {
&lastModifier, &repoType); err == nil {

if repo.HeadCommitID == "" {
continue
Expand All @@ -458,6 +459,9 @@ func GetReposByOwner(email string) ([]*SharedRepo, error) {
continue
}
repo.Name = repoName.String
if repoType.Valid {
repo.RepoType = repoType.String
}
repos = append(repos, repo)
}
}
Expand All @@ -473,7 +477,7 @@ func GetReposByOwner(email string) ([]*SharedRepo, error) {
func ListInnerPubRepos() ([]*SharedRepo, error) {
query := "SELECT InnerPubRepo.repo_id, " +
"owner_id, permission, commit_id, i.name, " +
"i.update_time, i.version " +
"i.update_time, i.version, i.type " +
"FROM InnerPubRepo " +
"LEFT JOIN RepoInfo i ON InnerPubRepo.repo_id = i.repo_id, RepoOwner, Branch " +
"WHERE InnerPubRepo.repo_id=RepoOwner.repo_id AND " +
Expand All @@ -495,10 +499,10 @@ func ListInnerPubRepos() ([]*SharedRepo, error) {
var repos []*SharedRepo
for rows.Next() {
repo := new(SharedRepo)
var repoName sql.NullString
var repoName, repoType sql.NullString
if err := rows.Scan(&repo.ID, &repo.Owner,
&repo.Permission, &repo.HeadCommitID, &repoName,
&repo.MTime, &repo.Version); err == nil {
&repo.MTime, &repo.Version, &repoType); err == nil {

if !repoName.Valid {
continue
Expand All @@ -507,6 +511,9 @@ func ListInnerPubRepos() ([]*SharedRepo, error) {
continue
}
repo.Name = repoName.String
if repoType.Valid {
repo.RepoType = repoType.String
}
repos = append(repos, repo)
}
}
Expand All @@ -525,7 +532,7 @@ func ListShareRepos(email, columnType string) ([]*SharedRepo, error) {
if columnType == "from_email" {
query = "SELECT sh.repo_id, to_email, " +
"permission, commit_id, " +
"i.name, i.update_time, i.version FROM " +
"i.name, i.update_time, i.version, i.type FROM " +
"SharedRepo sh LEFT JOIN RepoInfo i ON sh.repo_id = i.repo_id, Branch b " +
"WHERE from_email=? AND " +
"sh.repo_id = b.repo_id AND " +
Expand All @@ -534,7 +541,7 @@ func ListShareRepos(email, columnType string) ([]*SharedRepo, error) {
} else if columnType == "to_email" {
query = "SELECT sh.repo_id, from_email, " +
"permission, commit_id, " +
"i.name, i.update_time, i.version FROM " +
"i.name, i.update_time, i.version, i.type FROM " +
"SharedRepo sh LEFT JOIN RepoInfo i ON sh.repo_id = i.repo_id, Branch b " +
"WHERE to_email=? AND " +
"sh.repo_id = b.repo_id AND " +
Expand All @@ -561,10 +568,10 @@ func ListShareRepos(email, columnType string) ([]*SharedRepo, error) {

for rows.Next() {
repo := new(SharedRepo)
var repoName sql.NullString
var repoName, repoType sql.NullString
if err := rows.Scan(&repo.ID, &repo.Owner,
&repo.Permission, &repo.HeadCommitID,
&repoName, &repo.MTime, &repo.Version); err == nil {
&repoName, &repo.MTime, &repo.Version, &repoType); err == nil {

if !repoName.Valid {
continue
Expand All @@ -573,6 +580,9 @@ func ListShareRepos(email, columnType string) ([]*SharedRepo, error) {
continue
}
repo.Name = repoName.String
if repoType.Valid {
repo.RepoType = repoType.String
}

repos = append(repos, repo)
}
Expand All @@ -599,15 +609,15 @@ func GetGroupReposByUser(user string, orgID int) ([]*SharedRepo, error) {
if orgID < 0 {
sqlBuilder.WriteString("SELECT g.repo_id, " +
"user_name, permission, commit_id, " +
"i.name, i.update_time, i.version " +
"i.name, i.update_time, i.version, i.type " +
"FROM RepoGroup g " +
"LEFT JOIN RepoInfo i ON g.repo_id = i.repo_id, " +
"Branch b WHERE g.repo_id = b.repo_id AND " +
"b.name = 'master' AND group_id IN (")
} else {
sqlBuilder.WriteString("SELECT g.repo_id, " +
"owner, permission, commit_id, " +
"i.name, i.update_time, i.version " +
"i.name, i.update_time, i.version, i.type " +
"FROM OrgGroupRepo g " +
"LEFT JOIN RepoInfo i ON g.repo_id = i.repo_id, " +
"Branch b WHERE g.repo_id = b.repo_id AND " +
Expand All @@ -631,10 +641,13 @@ func GetGroupReposByUser(user string, orgID int) ([]*SharedRepo, error) {
var repos []*SharedRepo
for rows.Next() {
gRepo := new(SharedRepo)
var repoType sql.NullString
if err := rows.Scan(&gRepo.ID, &gRepo.Owner,
&gRepo.Permission, &gRepo.HeadCommitID,
&gRepo.Name, &gRepo.MTime, &gRepo.Version); err == nil {

&gRepo.Name, &gRepo.MTime, &gRepo.Version, &repoType); err == nil {
if repoType.Valid {
gRepo.RepoType = repoType.String
}
repos = append(repos, gRepo)
}
}
Expand Down
12 changes: 12 additions & 0 deletions fileserver/sync_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ func getAccessibleRepoListCB(rsp http.ResponseWriter, r *http.Request) *appError

var repoObjects []*share.SharedRepo
for _, repo := range repos {
if repo.RepoType != "" {
continue
}
if _, ok := obtainedRepos[repo.ID]; !ok {
obtainedRepos[repo.ID] = repo.ID
}
Expand All @@ -370,6 +373,9 @@ func getAccessibleRepoListCB(rsp http.ResponseWriter, r *http.Request) *appError
if _, ok := obtainedRepos[sRepo.ID]; ok {
continue
}
if sRepo.RepoType != "" {
continue
}
sRepo.Type = "srepo"
sRepo.Owner = strings.ToLower(sRepo.Owner)
repoObjects = append(repoObjects, sRepo)
Expand Down Expand Up @@ -402,6 +408,9 @@ func getAccessibleRepoListCB(rsp http.ResponseWriter, r *http.Request) *appError
if _, ok := obtainedRepos[sRepo.ID]; ok {
continue
}
if sRepo.RepoType != "" {
continue
}

sRepo.Type = "grepo"
sRepo.Owner = "Organization"
Expand All @@ -428,6 +437,9 @@ func filterGroupRepos(repos []*share.SharedRepo) map[string]*share.SharedRepo {
table := make(map[string]*share.SharedRepo)

for _, repo := range repos {
if repo.RepoType != "" {
continue
}
if repoPrev, ok := table[repo.ID]; ok {
if repo.Permission == "rw" && repoPrev.Permission == "r" {
table[repo.ID] = repo
Expand Down

0 comments on commit eaebe84

Please sign in to comment.