func DeleteRepo(ctx *middleware.Context) { user, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.APIError(422, "", err) } else { ctx.APIError(500, "GetUserByName", err) } return } repo, err := models.GetRepositoryByName(user.Id, ctx.Params(":reponame")) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Error(404) } else { ctx.APIError(500, "GetRepositoryByName", err) } return } if user.IsOrganization() && !user.IsOwnedBy(ctx.User.Id) { ctx.APIError(403, "", "Given user is not owner of organization.") return } if err := models.DeleteRepository(user.Id, repo.ID); err != nil { ctx.APIError(500, "DeleteRepository", err) return } log.Trace("Repository deleted: %s/%s", user.Name, repo.Name) ctx.Status(204) }
func createRepo(ctx *middleware.Context, owner *models.User, opt api.CreateRepoOption) { repo, err := models.CreateRepository(owner, models.CreateRepoOptions{ Name: opt.Name, Description: opt.Description, Gitignores: opt.Gitignores, License: opt.License, Readme: opt.Readme, IsPrivate: opt.Private, AutoInit: opt.AutoInit, }) if err != nil { if models.IsErrRepoAlreadyExist(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.APIError(422, "", err) } else { if repo != nil { if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil { log.Error(4, "DeleteRepository: %v", err) } } ctx.APIError(500, "CreateRepository", err) } return } ctx.JSON(201, ToApiRepository(owner, repo, api.Permission{true, true, true})) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories func ListMyRepos(ctx *middleware.Context) { ownRepos, err := models.GetRepositories(ctx.User.Id, true) if err != nil { ctx.APIError(500, "GetRepositories", err) return } numOwnRepos := len(ownRepos) accessibleRepos, err := ctx.User.GetAccessibleRepositories() if err != nil { ctx.APIError(500, "GetAccessibleRepositories", err) return } repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos)) for i := range ownRepos { repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true}) } i := numOwnRepos for repo, access := range accessibleRepos { repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{ Admin: access >= models.ACCESS_MODE_ADMIN, Push: access >= models.ACCESS_MODE_WRITE, Pull: true, }) i++ } ctx.JSON(200, &repos) }
// Render a Markdown document in raw mode. func MarkdownRaw(ctx *middleware.Context) { body, err := ctx.Req.Body().Bytes() if err != nil { ctx.APIError(422, "", err) return } ctx.Write(base.RenderRawMarkdown(body, "")) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create func CreateRepo(ctx *middleware.Context, opt api.CreateRepoOption) { // Shouldn't reach this condition, but just in case. if ctx.User.IsOrganization() { ctx.APIError(422, "", "not allowed creating repository for organization") return } createRepo(ctx, ctx.User, opt) }
// POST /users/:username/tokens func CreateAccessToken(ctx *middleware.Context, form CreateAccessTokenForm) { t := &models.AccessToken{ UID: ctx.User.Id, Name: form.Name, } if err := models.NewAccessToken(t); err != nil { ctx.APIError(500, "NewAccessToken", err) return } ctx.JSON(201, &api.AccessToken{t.Name, t.Sha1}) }
func GetRepoArchive(ctx *middleware.Context) { repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) gitRepo, err := git.OpenRepository(repoPath) if err != nil { ctx.APIError(500, "OpenRepository", err) return } ctx.Repo.GitRepo = gitRepo repo.Download(ctx) }
// GET /users/:username/tokens func ListAccessTokens(ctx *middleware.Context) { tokens, err := models.ListAccessTokens(ctx.User.Id) if err != nil { ctx.APIError(500, "ListAccessTokens", err) return } apiTokens := make([]*api.AccessToken, len(tokens)) for i := range tokens { apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1} } ctx.JSON(200, &apiTokens) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks func ListRepoHooks(ctx *middleware.Context) { hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID) if err != nil { ctx.APIError(500, "GetWebhooksByRepoId", err) return } apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { apiHooks[i] = ToApiHook(ctx.Repo.RepoLink, hooks[i]) } ctx.JSON(200, &apiHooks) }
// GET /users/:username func GetUserInfo(ctx *middleware.Context) { u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(404) } else { ctx.APIError(500, "GetUserByName", err) } return } // Hide user e-mail when API caller isn't signed in. if !ctx.IsSigned { u.Email = "" } ctx.JSON(200, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()}) }
// Render an arbitrary Markdown document. func Markdown(ctx *middleware.Context, form apiv1.MarkdownForm) { if ctx.HasApiError() { ctx.APIError(422, "", ctx.GetErrMsg()) return } if len(form.Text) == 0 { ctx.Write([]byte("")) return } switch form.Mode { case "gfm": ctx.Write(base.RenderMarkdown([]byte(form.Text), setting.AppUrl+strings.TrimPrefix(form.Context, "/"))) default: ctx.Write(base.RenderRawMarkdown([]byte(form.Text), "")) } }
func GetRepoRawFile(ctx *middleware.Context) { if !ctx.Repo.HasAccess() { ctx.Error(404) return } blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreeName) if err != nil { if err == git.ErrNotExist { ctx.Error(404) } else { ctx.APIError(500, "GetBlobByPath", err) } return } if err = repo.ServeBlob(ctx, blob); err != nil { ctx.APIError(500, "ServeBlob", err) } }
func CreateOrgRepo(ctx *middleware.Context, opt api.CreateRepoOption) { org, err := models.GetOrgByName(ctx.Params(":org")) if err != nil { if models.IsErrUserNotExist(err) { ctx.APIError(422, "", err) } else { ctx.APIError(500, "GetOrgByName", err) } return } if !org.IsOwnedBy(ctx.User.Id) { ctx.APIError(403, "", "Given user is not owner of organization.") return } createRepo(ctx, org, opt) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook func CreateRepoHook(ctx *middleware.Context, form api.CreateHookOption) { if !models.IsValidHookTaskType(form.Type) { ctx.APIError(422, "", "Invalid hook type") return } for _, name := range []string{"url", "content_type"} { if _, ok := form.Config[name]; !ok { ctx.APIError(422, "", "Missing config option: "+name) return } } if !models.IsValidHookContentType(form.Config["content_type"]) { ctx.APIError(422, "", "Invalid content type") return } if len(form.Events) == 0 { form.Events = []string{"push"} } w := &models.Webhook{ RepoID: ctx.Repo.Repository.ID, URL: form.Config["url"], ContentType: models.ToHookContentType(form.Config["content_type"]), Secret: form.Config["secret"], HookEvent: &models.HookEvent{ ChooseEvents: true, HookEvents: models.HookEvents{ Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)), Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)), }, }, IsActive: form.Active, HookTaskType: models.ToHookTaskType(form.Type), } if w.HookTaskType == models.SLACK { channel, ok := form.Config["channel"] if !ok { ctx.APIError(422, "", "Missing config option: channel") return } meta, err := json.Marshal(&models.SlackMeta{ Channel: channel, Username: form.Config["username"], IconURL: form.Config["icon_url"], Color: form.Config["color"], }) if err != nil { ctx.APIError(500, "slack: JSON marshal failed", err) return } w.Meta = string(meta) } if err := w.UpdateEvent(); err != nil { ctx.APIError(500, "UpdateEvent", err) return } else if err := models.CreateWebhook(w); err != nil { ctx.APIError(500, "CreateWebhook", err) return } ctx.JSON(201, ToApiHook(ctx.Repo.RepoLink, w)) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) { w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) if err != nil { ctx.APIError(500, "GetWebhookById", err) return } if form.Config != nil { if url, ok := form.Config["url"]; ok { w.URL = url } if ct, ok := form.Config["content_type"]; ok { if !models.IsValidHookContentType(ct) { ctx.APIError(422, "", "Invalid content type") return } w.ContentType = models.ToHookContentType(ct) } if w.HookTaskType == models.SLACK { if channel, ok := form.Config["channel"]; ok { meta, err := json.Marshal(&models.SlackMeta{ Channel: channel, Username: form.Config["username"], IconURL: form.Config["icon_url"], Color: form.Config["color"], }) if err != nil { ctx.APIError(500, "slack: JSON marshal failed", err) return } w.Meta = string(meta) } } } // Update events if len(form.Events) == 0 { form.Events = []string{"push"} } w.PushOnly = false w.SendEverything = false w.ChooseEvents = true w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)) w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)) if err = w.UpdateEvent(); err != nil { ctx.APIError(500, "UpdateEvent", err) return } if form.Active != nil { w.IsActive = *form.Active } if err := models.UpdateWebhook(w); err != nil { ctx.APIError(500, "UpdateWebhook", err) return } ctx.JSON(200, ToApiHook(ctx.Repo.RepoLink, w)) }
func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { ctxUser := ctx.User // Not equal means current user is an organization. if form.Uid != ctxUser.Id { org, err := models.GetUserByID(form.Uid) if err != nil { if models.IsErrUserNotExist(err) { ctx.APIError(422, "", err) } else { ctx.APIError(500, "GetUserByID", err) } return } ctxUser = org } if ctx.HasError() { ctx.APIError(422, "", ctx.GetErrMsg()) return } if ctxUser.IsOrganization() { // Check ownership of organization. if !ctxUser.IsOwnedBy(ctx.User.Id) { ctx.APIError(403, "", "Given user is not owner of organization.") return } } // Remote address can be HTTP/HTTPS/Git URL or local path. remoteAddr := form.CloneAddr if strings.HasPrefix(form.CloneAddr, "http://") || strings.HasPrefix(form.CloneAddr, "https://") || strings.HasPrefix(form.CloneAddr, "git://") { u, err := url.Parse(form.CloneAddr) if err != nil { ctx.APIError(422, "", err) return } if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 { u.User = url.UserPassword(form.AuthUsername, form.AuthPassword) } remoteAddr = u.String() } else if !com.IsDir(remoteAddr) { ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.") return } repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr) if err != nil { if repo != nil { if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } ctx.APIError(500, "MigrateRepository", err) return } log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true})) }