Commit 7608e90b authored by Martin Sefcik's avatar Martin Sefcik

Added support for project permissions attribute

parent 0925bd84
......@@ -36,7 +36,7 @@ const (
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
type AccessLevel int
......@@ -52,6 +52,22 @@ const (
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 = 0
ParticipatingNotifications NotificationLevel = 1
WatchNotifications NotificationLevel = 2
GlobalNotifications NotificationLevel = 3
MentionNotifications NotificationLevel = 4
)
// VisibilityLevel represents a visibility level within GitLab.
//
// 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,
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
// NAMESPACE/PROJECT_NAME, which is owned by the authenticated user.
//
// GitLab API docs:
// 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)
if err != nil {
return nil, nil, err
......@@ -159,7 +183,7 @@ func (s *ProjectsService) GetProject(pid interface{}) (*Project, *Response, erro
return nil, nil, err
}
p := new(Project)
p := new(ProjectWithAccess)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
......
package gitlab
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestListAllProjects(t *testing.T) {
ts, client := Stub("stubs/projects/index.json")
opt := &ListProjectsOptions{}
projects, _, err := client.Projects.ListAllProjects(opt)
assert.Equal(t, err, nil)
assert.Equal(t, len(projects), 2)
assert.Equal(t, projects[0].Name, "project")
assert.Equal(t, projects[1].Name, "project2")
defer ts.Close()
}
func TestGetProject(t *testing.T) {
ts, client := Stub("stubs/projects/show.json")
project, _, err := client.Projects.GetProject(1)
assert.Equal(t, err, nil)
assert.Equal(t, project.Name, "project")
assert.Equal(t, project.Namespace.Name, "group")
assert.Equal(t, project.Permissions.ProjectAccess.AccessLevel, MasterPermissions)
assert.Nil(t, project.Permissions.GroupAccess)
defer ts.Close()
}
\ No newline at end of file
[
{
"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
}
]
\ No newline at end of file
{
"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
}
}
\ No newline at end of file
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