Skip to content

Commit

Permalink
Made ResetVisibility and ParseRepo generic.
Browse files Browse the repository at this point in the history
Validate new from repo has admin rights
  • Loading branch information
josmo committed Aug 21, 2017
1 parent cee90e9 commit f54175d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
24 changes: 24 additions & 0 deletions model/repo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package model

import (
"strings"
"fmt"
)

type RepoLite struct {
Owner string `json:"owner"`
Name string `json:"name"`
Expand Down Expand Up @@ -38,6 +43,25 @@ type Repo struct {
Perm *Perm `json:"-" meddler:"-"`
}

func (r *Repo) ResetVisibility() {
r.Visibility = VisibilityPublic
if r.IsPrivate {
r.Visibility = VisibilityPrivate
}
}

// ParseRepo parses the repository owner and name from a string.
func ParseRepo(str string) (user, repo string, err error) {
var parts = strings.Split(str, "/")
if len(parts) != 2 {
err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world.")
return
}
user = parts[0]
repo = parts[1]
return
}

// Update updates the repository with values from the given Repo.
func (r *Repo) Update(from *Repo) {
r.Avatar = from.Avatar
Expand Down
25 changes: 12 additions & 13 deletions server/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
"strings"
)

func PostRepo(c *gin.Context) {
Expand Down Expand Up @@ -230,24 +229,35 @@ func MoveRepo(c *gin.Context) {
if !exists {
err := fmt.Errorf("Missing required to query value")
c.AbortWithError(http.StatusInternalServerError, err)
return
}

owner, name, errParse := ParseRepo(to)
owner, name, errParse := model.ParseRepo(to)
if errParse != nil {
c.AbortWithError(http.StatusInternalServerError, errParse)
return
}

from, err := remote.Repo(user, owner, name)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if !from.Perm.Admin {
c.AbortWithStatus(http.StatusUnauthorized)
return
}

repo.Name = from.Name
repo.Owner = from.Owner
repo.FullName = from.FullName
repo.Avatar = from.Avatar
repo.Link = from.Link
repo.Clone = from.Clone
repo.IsPrivate = from.IsPrivate
if repo.IsPrivate != from.IsPrivate {
repo.ResetVisibility()
}
repo.Visibility = from.Visibility

errStore := store.UpdateRepo(c, repo)
Expand All @@ -259,14 +269,3 @@ func MoveRepo(c *gin.Context) {
RepairRepo(c)
}

// ParseRepo parses the repository owner and name from a string.
func ParseRepo(str string) (user, repo string, err error) {
var parts = strings.Split(str, "/")
if len(parts) != 2 {
err = fmt.Errorf("Error: Invalid or missing repository. eg octocat/hello-world.")
return
}
user = parts[0]
repo = parts[1]
return
}

0 comments on commit f54175d

Please sign in to comment.