Skip to content

Commit cf7f33f

Browse files
author
Raphaël Benitte
committed
Merge pull request #17 from agunawan/master
Implement the List repository tree API method
2 parents e71119f + 0452ac2 commit cf7f33f

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

repositories.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ const (
1414
repo_url_raw_file = "/projects/:id/repository/blobs/:sha" // Get raw file content for specific commit/branch
1515
)
1616

17+
type TreeNode struct {
18+
Name string
19+
Type string
20+
Mode string
21+
Id string
22+
}
23+
1724
type BranchCommit struct {
1825
Id string `json:"id,omitempty"`
1926
Tree string `json:"tree,omitempty"`
@@ -52,6 +59,36 @@ type Commit struct {
5259
CreatedAt time.Time
5360
}
5461

62+
/*
63+
Get a list of repository files and directories in a project.
64+
65+
GET /projects/:id/repository/tree
66+
67+
Parameters:
68+
69+
id (required) The ID of a project
70+
path (optional) The path inside repository. Used to get contend of subdirectories
71+
ref_name (optional) The name of a repository branch or tag or if not given the default branch
72+
73+
Usage:
74+
pass nil when not using optional parameters
75+
*/
76+
func (g *Gitlab) RepoTree(id, path, ref_name string) ([]*TreeNode, error) {
77+
78+
url, opaque := g.ResourceUrlRaw(repo_url_tree, map[string]string{":id": id})
79+
url += "&path=" + path
80+
url += "&ref_name=" + ref_name
81+
82+
var treeNodes []*TreeNode
83+
84+
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
85+
if err == nil {
86+
err = json.Unmarshal(contents, &treeNodes)
87+
}
88+
89+
return treeNodes, err
90+
}
91+
5592
/*
5693
Get a list of repository branches from a project, sorted by name alphabetically.
5794

repositories_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ func TestRepoCommits(t *testing.T) {
4141
assert.Equal(t, len(commits), 2)
4242
defer ts.Close()
4343
}
44+
45+
func TestRepoTree(t *testing.T) {
46+
ts, gitlab := Stub("stubs/trees/show.json")
47+
tree, err := gitlab.RepoTree("1", "path", "ref_name")
48+
49+
assert.Equal(t, err, nil)
50+
assert.Equal(t, len(tree), 6)
51+
defer ts.Close()
52+
}

stubs/trees/show.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"name": "assets",
4+
"type": "tree",
5+
"mode": "040000",
6+
"id": "6229c43a7e16fcc7e95f923f8ddadb8281d9c6c6"
7+
},
8+
{
9+
"name": "contexts",
10+
"type": "tree",
11+
"mode": "040000",
12+
"id": "faf1cdf33feadc7973118ca42d35f1e62977e91f"
13+
},
14+
{
15+
"name": "controllers",
16+
"type": "tree",
17+
"mode": "040000",
18+
"id": "95633e8d258bf3dfba3a5268fb8440d263218d74"
19+
},
20+
{
21+
"name": "Rakefile",
22+
"type": "blob",
23+
"mode": "100644",
24+
"id": "35b2f05cbb4566b71b34554cf184a9d0bd9d46d6"
25+
},
26+
{
27+
"name": "VERSION",
28+
"type": "blob",
29+
"mode": "100644",
30+
"id": "803e4a4f3727286c3093c63870c2b6524d30ec4f"
31+
},
32+
{
33+
"name": "config.ru",
34+
"type": "blob",
35+
"mode": "100644",
36+
"id": "dfd2d862237323aa599be31b473d70a8a817943b"
37+
}
38+
]

0 commit comments

Comments
 (0)