Skip to content
This repository has been archived by the owner on Oct 3, 2018. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kzaitsev committed Jul 26, 2015
2 parents 46efba4 + 10d143d commit 912567b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
3 changes: 1 addition & 2 deletions deploy_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ Parameters:
*/
func (g *Gitlab) AddProjectDeployKey(id, title, key string) error {
var err error

path, opaque := g.ResourceUrlRaw(project_url_deploy_keys, map[string]string{":id": id})

var err error

v := url.Values{}
v.Set("title", title)
v.Set("key", key)
Expand Down
36 changes: 36 additions & 0 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gogitlab
import (
"encoding/json"
"strconv"
"strings"
)

const (
Expand Down Expand Up @@ -207,3 +208,38 @@ func (g *Gitlab) ProjectMergeRequests(id string, page int, per_page int, state s

return mr, err
}

/*
Get single project id.
GET /projects/search/:query
Parameters:
namespace The namespace of a project
name The id of a project
*/
func (g *Gitlab) SearchProjectId(namespace string, name string) (id int, err error) {

url, opaque := g.ResourceUrlRaw(projects_search_url, map[string]string{
":query": strings.ToLower(name),
})

var projects []*Project

contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &projects)
} else {
return id, err
}

for _, project := range projects {
if project.Namespace.Name == namespace {
id = project.Id
}
}

return id, err
}
12 changes: 12 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ func TestProjectMergeRequests(t *testing.T) {
assert.Equal(t, mr[0].SourceBranch, "test1")
}
}

func TestSearchProjectId(t *testing.T) {
ts, gitlab := Stub("stubs/projects/index.json")

namespace := "Brightbox"
name := "Puppet"
id, err := gitlab.SearchProjectId(namespace, name)

assert.Equal(t, err, nil)
assert.Equal(t, id, 6)
defer ts.Close()
}
37 changes: 37 additions & 0 deletions repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ type Commit struct {
CreatedAt time.Time
}

type File struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Mode string `json:"mode,omitempty"`

Children []*File
}

/*
Get a list of repository branches from a project, sorted by name alphabetically.
Expand Down Expand Up @@ -207,3 +216,31 @@ func (g *Gitlab) RepoRawFile(id, sha, filepath string) ([]byte, error) {

return contents, err
}

/*
Get Raw file content
*/
func (g *Gitlab) RepoTree(id, ref, path string) ([]*File, error) {

url := g.ResourceUrlQuery(repo_url_tree, map[string]string{
":id": id,
}, map[string]string{
"ref": ref,
"path": path,
})

var files []*File

contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &files)
}

for _, f := range files {
if f.Type == "tree" {
f.Children, err = g.RepoTree(id, ref, path+"/"+f.Name)
}
}

return files, err
}

0 comments on commit 912567b

Please sign in to comment.