summaryrefslogtreecommitdiffstats
path: root/routers/api/v1/utils
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-12-04 17:16:42 -0500
committerUnknwon <u@gogs.io>2015-12-04 17:16:42 -0500
commit56dd430a10bf5281caf648344e4660fbdc5d4dee (patch)
tree9493b1a9f77321525d62ce1ccefc4dd792391832 /routers/api/v1/utils
parente0bae9547af03e5e7c0201faaa9568d6a1cc9e1f (diff)
downloadgitea-56dd430a10bf5281caf648344e4660fbdc5d4dee.tar.gz
gitea-56dd430a10bf5281caf648344e4660fbdc5d4dee.zip
refactor API routes and some work for #976
Diffstat (limited to 'routers/api/v1/utils')
-rw-r--r--routers/api/v1/utils/convert.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/routers/api/v1/utils/convert.go b/routers/api/v1/utils/convert.go
new file mode 100644
index 0000000000..7ac4edf26f
--- /dev/null
+++ b/routers/api/v1/utils/convert.go
@@ -0,0 +1,92 @@
+// Copyright 2015 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package utils
+
+import (
+ "fmt"
+
+ "github.com/Unknwon/com"
+
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/modules/setting"
+)
+
+// ApiUser converts user to its API format.
+func ApiUser(u *models.User) *api.User {
+ return &api.User{
+ ID: u.Id,
+ UserName: u.Name,
+ FullName: u.FullName,
+ Email: u.Email,
+ AvatarUrl: u.AvatarLink(),
+ }
+}
+
+// ApiRepository converts repository to API format.
+func ApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
+ cl := repo.CloneLink()
+ return &api.Repository{
+ Id: repo.ID,
+ Owner: *ApiUser(owner),
+ FullName: owner.Name + "/" + repo.Name,
+ Private: repo.IsPrivate,
+ Fork: repo.IsFork,
+ HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
+ CloneUrl: cl.HTTPS,
+ SshUrl: cl.SSH,
+ Permissions: permission,
+ }
+}
+
+// ApiPublicKey converts public key to its API format.
+func ApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
+ return &api.PublicKey{
+ ID: key.ID,
+ Key: key.Content,
+ URL: apiLink + com.ToStr(key.ID),
+ Title: key.Name,
+ Created: key.Created,
+ }
+}
+
+// ApiHook converts webhook to its API format.
+func ApiHook(repoLink string, w *models.Webhook) *api.Hook {
+ config := map[string]string{
+ "url": w.URL,
+ "content_type": w.ContentType.Name(),
+ }
+ if w.HookTaskType == models.SLACK {
+ s := w.GetSlackHook()
+ config["channel"] = s.Channel
+ config["username"] = s.Username
+ config["icon_url"] = s.IconURL
+ config["color"] = s.Color
+ }
+
+ return &api.Hook{
+ ID: w.ID,
+ Type: w.HookTaskType.Name(),
+ URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
+ Active: w.IsActive,
+ Config: config,
+ Events: w.EventsArray(),
+ Updated: w.Updated,
+ Created: w.Created,
+ }
+}
+
+// ApiDeployKey converts deploy key to its API format.
+func ApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
+ return &api.DeployKey{
+ ID: key.ID,
+ Key: key.Content,
+ URL: apiLink + com.ToStr(key.ID),
+ Title: key.Name,
+ Created: key.Created,
+ ReadOnly: true, // All deploy keys are read-only.
+ }
+}