woodpecker/remote/gitlab/helper.go

99 lines
2.4 KiB
Go
Raw Normal View History

2015-07-25 08:49:39 +00:00
package gitlab
import (
"fmt"
"net/url"
"strconv"
2015-07-25 08:49:39 +00:00
2015-12-12 12:20:01 +00:00
"github.com/drone/drone/remote/gitlab/client"
2015-07-25 08:49:39 +00:00
)
// NewClient is a helper function that returns a new GitHub
// client using the provided OAuth token.
2015-12-12 12:20:01 +00:00
func NewClient(url, accessToken string, skipVerify bool) *client.Client {
client := client.New(url, "/api/v3", accessToken, skipVerify)
2015-07-25 08:49:39 +00:00
return client
}
// IsRead is a helper function that returns true if the
// user has Read-only access to the repository.
2015-12-12 12:20:01 +00:00
func IsRead(proj *client.Project) bool {
2015-07-25 08:49:39 +00:00
var user = proj.Permissions.ProjectAccess
var group = proj.Permissions.GroupAccess
switch {
case proj.Public:
return true
case user != nil && user.AccessLevel >= 20:
return true
case group != nil && group.AccessLevel >= 20:
return true
default:
return false
}
}
// IsWrite is a helper function that returns true if the
// user has Read-Write access to the repository.
2015-12-12 12:20:01 +00:00
func IsWrite(proj *client.Project) bool {
2015-07-25 08:49:39 +00:00
var user = proj.Permissions.ProjectAccess
var group = proj.Permissions.GroupAccess
switch {
case user != nil && user.AccessLevel >= 30:
return true
case group != nil && group.AccessLevel >= 30:
return true
default:
return false
}
}
// IsAdmin is a helper function that returns true if the
// user has Admin access to the repository.
2015-12-12 12:20:01 +00:00
func IsAdmin(proj *client.Project) bool {
2015-07-25 08:49:39 +00:00
var user = proj.Permissions.ProjectAccess
var group = proj.Permissions.GroupAccess
switch {
case user != nil && user.AccessLevel >= 40:
return true
case group != nil && group.AccessLevel >= 40:
return true
default:
return false
}
}
// GetKeyTitle is a helper function that generates a title for the
// RSA public key based on the username and domain name.
func GetKeyTitle(rawurl string) (string, error) {
var uri, err = url.Parse(rawurl)
if err != nil {
return "", err
}
return fmt.Sprintf("drone@%s", uri.Host), nil
}
func ns(owner, name string) string {
return fmt.Sprintf("%s%%2F%s", owner, name)
}
2015-12-12 12:20:01 +00:00
func GetUserEmail(c *client.Client, defaultURL string) (*client.Client, error) {
return c, nil
2015-07-25 08:49:39 +00:00
}
2015-12-12 12:20:01 +00:00
func GetProjectId(r *Gitlab, c *client.Client, owner, name string) (projectId string, err error) {
if r.Search {
2015-12-12 12:20:01 +00:00
_projectId, err := c.SearchProjectId(owner, name)
if err != nil || _projectId == 0 {
return "", err
}
projectId := strconv.Itoa(_projectId)
return projectId, nil
} else {
projectId := ns(owner, name)
return projectId, nil
}
}