Skip to content

Commit b75450a

Browse files
ethantkoeniglunny
authored andcommitted
API endpoints for forks (#509)
1 parent 527c2dd commit b75450a

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

routers/api/v1/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ func RegisterRoutes(m *macaron.Macaron) {
276276
})
277277
m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
278278
m.Get("/archive/*", repo.GetArchive)
279+
m.Combo("/forks").Get(repo.ListForks).
280+
Post(bind(api.CreateForkOption{}), repo.CreateFork)
279281
m.Group("/branches", func() {
280282
m.Get("", repo.ListBranches)
281283
m.Get("/:branchname", repo.GetBranch)

routers/api/v1/repo/fork.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2016 The Gogs Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
api "code.gitea.io/sdk/gitea"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/context"
12+
)
13+
14+
// ListForks list a repository's forks
15+
func ListForks(ctx *context.APIContext) {
16+
forks, err := ctx.Repo.Repository.GetForks()
17+
if err != nil {
18+
ctx.Error(500, "GetForks", err)
19+
return
20+
}
21+
apiForks := make([]*api.Repository, len(forks))
22+
for i, fork := range forks {
23+
access, err := models.AccessLevel(ctx.User, fork)
24+
if err != nil {
25+
ctx.Error(500, "AccessLevel", err)
26+
return
27+
}
28+
apiForks[i] = fork.APIFormat(access)
29+
}
30+
ctx.JSON(200, apiForks)
31+
}
32+
33+
// CreateFork create a fork of a repo
34+
func CreateFork(ctx *context.APIContext, form api.CreateForkOption) {
35+
repo := ctx.Repo.Repository
36+
var forker *models.User // user/org that will own the fork
37+
if form.Organization == nil {
38+
forker = ctx.User
39+
} else {
40+
org, err := models.GetOrgByName(*form.Organization)
41+
if err != nil {
42+
if err == models.ErrOrgNotExist {
43+
ctx.Error(422, "", err)
44+
} else {
45+
ctx.Error(500, "GetOrgByName", err)
46+
}
47+
return
48+
}
49+
if !org.IsOrgMember(ctx.User.ID) {
50+
ctx.Status(403)
51+
return
52+
}
53+
forker = org
54+
}
55+
fork, err := models.ForkRepository(forker, repo, repo.Name, repo.Description)
56+
if err != nil {
57+
ctx.Error(500, "ForkRepository", err)
58+
return
59+
}
60+
ctx.JSON(202, fork.APIFormat(models.AccessModeOwner))
61+
}

vendor/code.gitea.io/sdk/gitea/fork.go

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
"revisionTime": "2016-12-28T14:57:51Z"
1010
},
1111
{
12-
"checksumSHA1": "dnGaLR7sd9D5YpQZP4QUGZiEq+c=",
12+
"checksumSHA1": "5J8ejjEp2moLyK1pD++Jzof8DFs=",
1313
"path": "code.gitea.io/sdk/gitea",
14-
"revision": "d628d07f7377c2c10df5e0292370b21d9ac689eb",
15-
"revisionTime": "2016-12-24T02:50:46Z"
14+
"revision": "c0e081342a4b99d90371081b888765b91f05546f",
15+
"revisionTime": "2016-12-29T09:40:42Z"
1616
},
1717
{
1818
"checksumSHA1": "IyfS7Rbl6OgR83QR7TOfKdDCq+M=",

0 commit comments

Comments
 (0)