Commit 8e3e25a1 authored by Sander van Harmelen's avatar Sander van Harmelen

Merge pull request #19 from martinsefcik/project-permissions

Added support for project permissions attribute
parents 0925bd84 07bee06b
...@@ -36,7 +36,7 @@ const ( ...@@ -36,7 +36,7 @@ const (
userAgent = "go-gitlab/" + libraryVersion userAgent = "go-gitlab/" + libraryVersion
) )
// AccessLevel represents a premission level within GitLab. // AccessLevel represents a permission level within GitLab.
// //
// GitLab API docs: http://doc.gitlab.com/ce/permissions/permissions.html // GitLab API docs: http://doc.gitlab.com/ce/permissions/permissions.html
type AccessLevel int type AccessLevel int
...@@ -52,6 +52,22 @@ const ( ...@@ -52,6 +52,22 @@ const (
OwnerPermission AccessLevel = 50 OwnerPermission AccessLevel = 50
) )
// NotificationLevel represents a notification level within Gitlab.
//
// GitLab API docs: http://doc.gitlab.com/ce/...?
type NotificationLevel int
// List of available notification levels
//
// GitLab API docs: http://doc.gitlab.com/ce/...?
const (
DisabledNotifications NotificationLevel = iota
ParticipatingNotifications
WatchNotifications
GlobalNotifications
MentionNotifications
)
// VisibilityLevel represents a visibility level within GitLab. // VisibilityLevel represents a visibility level within GitLab.
// //
// GitLab API docs: http://doc.gitlab.com/ce/...? // GitLab API docs: http://doc.gitlab.com/ce/...?
......
package gitlab
import (
"io/ioutil"
"net/http"
"net/http/httptest"
)
func Stub(filename string) (*httptest.Server, *Client) {
stub, _ := ioutil.ReadFile(filename)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(stub))
}))
client := NewClient(nil, "")
client.SetBaseURL(ts.URL)
return ts, client
}
...@@ -142,12 +142,36 @@ func (s *ProjectsService) ListAllProjects(opt *ListProjectsOptions) ([]*Project, ...@@ -142,12 +142,36 @@ func (s *ProjectsService) ListAllProjects(opt *ListProjectsOptions) ([]*Project,
return p, resp, err return p, resp, err
} }
// ProjectWithAccess represents a GitLab project with permissions attributes.
//
// GitLab API docs:
// http://doc.gitlab.com/ce/api/projects.html#get-single-project
type ProjectWithAccess struct {
Project
Permissions *Permissions `json:"permissions"`
}
type Permissions struct {
ProjectAccess *ProjectAccess `json:"project_access"`
GroupAccess *GroupAccess `json:"group_access"`
}
type ProjectAccess struct {
AccessLevel AccessLevel `json:"access_level"`
NotificationLevel NotificationLevel `json:"notification_level"`
}
type GroupAccess struct {
AccessLevel AccessLevel `json:"access_level"`
NotificationLevel NotificationLevel `json:"notification_level"`
}
// GetProject gets a specific project, identified by project ID or // GetProject gets a specific project, identified by project ID or
// NAMESPACE/PROJECT_NAME, which is owned by the authenticated user. // NAMESPACE/PROJECT_NAME, which is owned by the authenticated user.
// //
// GitLab API docs: // GitLab API docs:
// http://doc.gitlab.com/ce/api/projects.html#get-single-project // http://doc.gitlab.com/ce/api/projects.html#get-single-project
func (s *ProjectsService) GetProject(pid interface{}) (*Project, *Response, error) { func (s *ProjectsService) GetProject(pid interface{}) (*ProjectWithAccess, *Response, error) {
project, err := parseID(pid) project, err := parseID(pid)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
...@@ -159,7 +183,7 @@ func (s *ProjectsService) GetProject(pid interface{}) (*Project, *Response, erro ...@@ -159,7 +183,7 @@ func (s *ProjectsService) GetProject(pid interface{}) (*Project, *Response, erro
return nil, nil, err return nil, nil, err
} }
p := new(Project) p := new(ProjectWithAccess)
resp, err := s.client.Do(req, p) resp, err := s.client.Do(req, p)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
......
package gitlab
import (
"testing"
)
func TestListAllProjects(t *testing.T) {
ts, client := Stub("stubs/projects/index.json")
defer ts.Close()
opt := &ListProjectsOptions{}
projects, _, err := client.Projects.ListAllProjects(opt)
if err != nil {
t.Fatalf("Expected error nil, got: %v", err)
}
count := len(projects)
if count != 2 {
t.Errorf("Expected number of projects %q, got %q", 2, count)
}
if projects[0].Name != "project" {
t.Errorf("Expected project name %q, got %q", "project", projects[0].Name)
}
if projects[1].Name != "project2" {
t.Errorf("Expected project name %q, got %q", "project2", projects[1].Name)
}
}
func TestGetProject(t *testing.T) {
ts, client := Stub("stubs/projects/show.json")
defer ts.Close()
project, _, err := client.Projects.GetProject(1)
if err != nil {
t.Fatalf("Expected error nil, got: %v", err)
}
if project.Name != "project" {
t.Errorf("Expected project name %q, got %q", "project", project.Name)
}
if project.Namespace.Name != "group" {
t.Errorf("Expected namespace name %q, got %q", "group", project.Namespace.Name)
}
if project.Permissions.ProjectAccess.AccessLevel != MasterPermissions {
t.Errorf("Expected project access level %q, got %q", MasterPermissions, project.Permissions.ProjectAccess.AccessLevel)
}
if project.Permissions.GroupAccess != nil {
t.Errorf("Expected project group access nil, got %q", project.Permissions.GroupAccess)
}
}
[
{
"id": 12,
"description": "",
"default_branch": "master",
"tag_list": [],
"public": false,
"archived": false,
"visibility_level": 0,
"ssh_url_to_repo": "git@gitlab.example.com:group/project",
"http_url_to_repo": "https://gitlab.example.com/group/project.git",
"web_url": "https://gitlab.example.com/group/project",
"name": "project",
"name_with_namespace": "group / project",
"path": "project",
"path_with_namespace": "group/project",
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": false,
"snippets_enabled": false,
"created_at": "2015-10-26T14:32:44.103+01:00",
"last_activity_at": "2015-11-01T22:07:47.456+01:00",
"creator_id": 1,
"namespace": {
"id": 7,
"name": "group",
"path": "group",
"owner_id": null,
"created_at": "2015-10-26T13:36:40.147+01:00",
"updated_at": "2015-10-27T08:57:55.199+01:00",
"description": "group desc",
"avatar": {
"url": "/uploads/group/avatar/7/group-logo.png"
}
},
"avatar_url": "https://gitlab.example.com/uploads/project/avatar/12/group-logo.png",
"star_count": 0,
"forks_count": 0
},
{
"id": 11,
"description": "",
"default_branch": "master",
"tag_list": [],
"public": false,
"archived": false,
"visibility_level": 0,
"ssh_url_to_repo": "git@gitlab.example.com:johndoe/project2.git",
"http_url_to_repo": "https://gitlab.example.com/johndoe/project2.git",
"web_url": "https://gitlab.example.com/johndoe/project2",
"owner": {
"name": "John Doe",
"username": "johndoe",
"id": 1,
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/123",
"web_url": "https://gitlab.example.com/u/johndoe"
},
"name": "project2",
"name_with_namespace": "John Doe / project2",
"path": "project2",
"path_with_namespace": "johndoe/project2",
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"snippets_enabled": true,
"created_at": "2015-03-18T19:33:20.166+01:00",
"last_activity_at": "2015-11-01T19:48:32.793+01:00",
"creator_id": 1,
"namespace": {
"id": 1,
"name": "John Doe",
"path": "johndoe",
"owner_id": 1,
"created_at": "2014-01-05T22:01:51.000+01:00",
"updated_at": "2014-01-05T22:51:08.000+01:00",
"description": "",
"avatar": null
},
"avatar_url": null,
"star_count": 0,
"forks_count": 0
}
]
{
"id": 11,
"description": "",
"default_branch": "master",
"tag_list": [],
"public": false,
"archived": false,
"visibility_level": 0,
"ssh_url_to_repo": "git@gitlab.example.com:group/project.git",
"http_url_to_repo": "https://gitlab.example.com/group/project.git",
"web_url": "https://gitlab.example.com/group/project",
"owner": {
"name": "John Doe",
"username": "johndoe",
"id": 1,
"state": "active",
"avatar_url": "https://secure.gravatar.com/avatar/123",
"web_url": "https://gitlab.example.com/u/johndoe"
},
"name": "project",
"name_with_namespace": "Group / project",
"path": "project",
"path_with_namespace": "group/project",
"issues_enabled": true,
"merge_requests_enabled": true,
"wiki_enabled": true,
"snippets_enabled": true,
"created_at": "2015-03-18T19:33:20.166+01:00",
"last_activity_at": "2015-11-01T19:48:32.793+01:00",
"creator_id": 1,
"namespace": {
"id": 1,
"name": "group",
"path": "group",
"owner_id": 1,
"created_at": "2014-01-05T22:01:51.000+01:00",
"updated_at": "2014-01-05T22:51:08.000+01:00",
"description": "",
"avatar": null
},
"avatar_url": null,
"star_count": 0,
"forks_count": 0,
"permissions": {
"project_access": {
"access_level": 40,
"notification_level": 3
},
"group_access": null
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment