func UpdateIssueMilestone(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } oldMid := issue.MilestoneID mid := ctx.QueryInt64("id") if oldMid == mid { ctx.JSON(200, map[string]interface{}{ "ok": true, }) return } // Not check for invalid milestone id and give responsibility to owners. issue.MilestoneID = mid if err := models.ChangeMilestoneAssign(oldMid, issue); err != nil { ctx.Handle(500, "ChangeMilestoneAssign", err) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func UpdateIssueTitle(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } if !ctx.IsSigned || (ctx.User.Id != issue.PosterID && !ctx.Repo.IsAdmin()) { ctx.Error(403) return } issue.Name = ctx.Query("title") if len(issue.Name) == 0 { ctx.Error(204) return } if err := models.UpdateIssue(issue); err != nil { ctx.Handle(500, "UpdateIssue", err) return } ctx.JSON(200, map[string]interface{}{ "title": issue.Name, }) }
func UpdateIssueAssignee(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } aid := ctx.QueryInt64("id") if issue.AssigneeID == aid { ctx.JSON(200, map[string]interface{}{ "ok": true, }) return } // Not check for invalid assignee id and give responsibility to owners. issue.AssigneeID = aid if err := models.UpdateIssueUserByAssignee(issue); err != nil { ctx.Handle(500, "UpdateIssueUserByAssignee: %v", err) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func MigratePost(ctx *middleware.Context, form auth.MigrateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_migrate") ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if ctx.HasError() { ctx.HTML(200, MIGRATE) return } // Remote address can be HTTP/HTTPS/Git URL or local path. // Note: remember to change api/v1/repo.go: MigrateRepo // FIXME: merge these two functions with better error handling 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.Data["Err_CloneAddr"] = true ctx.RenderWithErr(ctx.Tr("form.url_error"), MIGRATE, &form) 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.Data["Err_CloneAddr"] = true ctx.RenderWithErr(ctx.Tr("repo.migrate.invalid_local_path"), MIGRATE, &form) return } repo, err := models.MigrateRepository(ctxUser, form.RepoName, form.Description, form.Private, form.Mirror, remoteAddr) if err == nil { log.Trace("Repository migrated: %s/%s", ctxUser.Name, form.RepoName) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + form.RepoName) return } if repo != nil { if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } if strings.Contains(err.Error(), "Authentication failed") || strings.Contains(err.Error(), " not found") { ctx.Data["Err_Auth"] = true ctx.RenderWithErr(ctx.Tr("form.auth_failed", strings.Replace(err.Error(), ":"+form.AuthPassword+"@", ":<password>@", 1)), MIGRATE, &form) return } handleCreateError(ctx, err, "MigratePost", MIGRATE, &form) }
func CompareAndPullRequest(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes") ctx.Data["PageIsComparePull"] = true repo := ctx.Repo.Repository // Get compare branch information. infos := strings.Split(ctx.Params("*"), "...") if len(infos) != 2 { ctx.Handle(404, "CompareAndPullRequest", nil) return } baseBranch := infos[0] ctx.Data["BaseBranch"] = baseBranch headInfos := strings.Split(infos[1], ":") if len(headInfos) != 2 { ctx.Handle(404, "CompareAndPullRequest", nil) return } headUser := headInfos[0] headBranch := headInfos[1] ctx.Data["HeadBranch"] = headBranch // TODO: check if branches are valid. fmt.Println(baseBranch, headUser, headBranch) // TODO: add organization support // Check if current user has fork of repository. headRepo, has := models.HasForkedRepo(ctx.User.Id, repo.ID) if !has { ctx.Handle(404, "HasForkedRepo", nil) return } headGitRepo, err := git.OpenRepository(models.RepoPath(ctx.User.Name, headRepo.Name)) if err != nil { ctx.Handle(500, "OpenRepository", err) return } headBranches, err := headGitRepo.GetBranches() if err != nil { ctx.Handle(500, "GetBranches", err) return } ctx.Data["HeadBranches"] = headBranches // Setup information for new form. RetrieveRepoMetas(ctx, ctx.Repo.Repository) if ctx.Written() { return } // Get diff information. ctx.HTML(200, COMPARE_PULL) }
func Fork(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("new_fork") getForkRepository(ctx) if ctx.Written() { return } ctx.Data["ContextUser"] = ctx.User ctx.HTML(200, FORK) }
func Migrate(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("new_migrate") ctx.Data["private"] = ctx.User.LastRepoVisibility ctxUser := checkContextUser(ctx, ctx.QueryInt64("org")) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser ctx.HTML(200, MIGRATE) }
func ForkPost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_fork") forkRepo := getForkRepository(ctx) if ctx.Written() { return } ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if ctx.HasError() { ctx.HTML(200, FORK) return } repo, has := models.HasForkedRepo(ctxUser.Id, forkRepo.ID) if has { ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } // Check ownership of organization. if ctxUser.IsOrganization() { if !ctxUser.IsOwnedBy(ctx.User.Id) { ctx.Error(403) return } } repo, err := models.ForkRepository(ctxUser, forkRepo, form.RepoName, form.Description) if err != nil { ctx.Data["Err_RepoName"] = true switch { case models.IsErrRepoAlreadyExist(err): ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), FORK, &form) case models.IsErrNameReserved(err): ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &form) case models.IsErrNamePatternNotAllowed(err): ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &form) default: ctx.Handle(500, "ForkPost", err) } return } log.Trace("Repository forked[%d]: %s/%s", forkRepo.ID, ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) }
func WebHooksEdit(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true orCtx, w := checkWebhook(ctx) if ctx.Written() { return } ctx.Data["Webhook"] = w ctx.HTML(200, orCtx.NewTemplate) }
func NewIssue(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true ctx.Data["RequireDropzone"] = true renderAttachmentSettings(ctx) RetrieveRepoMetas(ctx, ctx.Repo.Repository) if ctx.Written() { return } ctx.HTML(200, ISSUE_NEW) }
func Create(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("new_repo") // Give default value for template to render. ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctx.Data["Readmes"] = models.Readmes ctx.Data["readme"] = "Default" ctx.Data["private"] = ctx.User.LastRepoVisibility ctxUser := checkContextUser(ctx, ctx.QueryInt64("org")) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser ctx.HTML(200, CREATE) }
func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true orCtx, w := checkWebhook(ctx) if ctx.Written() { return } ctx.Data["Webhook"] = w if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } meta, err := json.Marshal(&models.SlackMeta{ Channel: form.Channel, Username: form.Username, IconURL: form.IconURL, Color: form.Color, }) if err != nil { ctx.Handle(500, "Marshal", err) return } w.URL = form.PayloadURL w.Meta = string(meta) w.HookEvent = ParseHookEvent(form.WebhookForm) w.IsActive = form.Active if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.UpdateWebhook(w); err != nil { ctx.Handle(500, "UpdateWebhook", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) }
func WebhooksNew(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} orCtx, err := getOrgRepoCtx(ctx) if err != nil { ctx.Handle(500, "getOrgRepoCtx", err) return } ctx.Data["HookType"] = checkHookType(ctx) if ctx.Written() { return } ctx.Data["BaseLink"] = orCtx.Link ctx.HTML(200, orCtx.NewTemplate) }
func CreatePost(ctx *middleware.Context, form auth.CreateRepoForm) { ctx.Data["Title"] = ctx.Tr("new_repo") ctx.Data["Gitignores"] = models.Gitignores ctx.Data["Licenses"] = models.Licenses ctx.Data["Readmes"] = models.Readmes ctxUser := checkContextUser(ctx, form.Uid) if ctx.Written() { return } ctx.Data["ContextUser"] = ctxUser if ctx.HasError() { ctx.HTML(200, CREATE) return } repo, err := models.CreateRepository(ctxUser, models.CreateRepoOptions{ Name: form.RepoName, Description: form.Description, Gitignores: form.Gitignores, License: form.License, Readme: form.Readme, IsPrivate: form.Private, AutoInit: form.AutoInit, }) if err == nil { log.Trace("Repository created: %s/%s", ctxUser.Name, repo.Name) ctx.Redirect(setting.AppSubUrl + "/" + ctxUser.Name + "/" + repo.Name) return } if repo != nil { if errDelete := models.DeleteRepository(ctxUser.Id, repo.ID); errDelete != nil { log.Error(4, "DeleteRepository: %v", errDelete) } } handleCreateError(ctx, err, "CreatePost", CREATE, &form) }
func UpdateIssueContent(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } if !ctx.IsSigned || (ctx.User.Id != issue.PosterID && !ctx.Repo.IsAdmin()) { ctx.Error(403) return } issue.Content = ctx.Query("content") if err := models.UpdateIssue(issue); err != nil { ctx.Handle(500, "UpdateIssue", err) return } ctx.JSON(200, map[string]interface{}{ "content": string(base.RenderMarkdown([]byte(issue.Content), ctx.Query("context"))), }) }
func UpdateIssueLabel(ctx *middleware.Context) { issue := getActionIssue(ctx) if ctx.Written() { return } if ctx.Query("action") == "clear" { if err := issue.ClearLabels(); err != nil { ctx.Handle(500, "ClearLabels", err) return } } else { isAttach := ctx.Query("action") == "attach" label, err := models.GetLabelByID(ctx.QueryInt64("id")) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Error(404, "GetLabelByID") } else { ctx.Handle(500, "GetLabelByID", err) } return } if isAttach && !issue.HasLabel(label.ID) { if err = issue.AddLabel(label); err != nil { ctx.Handle(500, "AddLabel", err) return } } else if !isAttach && issue.HasLabel(label.ID) { if err = issue.RemoveLabel(label); err != nil { ctx.Handle(500, "RemoveLabel", err) return } } } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true orCtx, w := checkWebhook(ctx) if ctx.Written() { return } ctx.Data["Webhook"] = w if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } contentType := models.JSON if models.HookContentType(form.ContentType) == models.FORM { contentType = models.FORM } w.URL = form.PayloadURL w.ContentType = contentType w.Secret = form.Secret w.HookEvent = ParseHookEvent(form.WebhookForm) w.IsActive = form.Active if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.UpdateWebhook(w); err != nil { ctx.Handle(500, "WebHooksEditPost", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) }
func Dashboard(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("dashboard") ctx.Data["PageIsDashboard"] = true ctx.Data["PageIsNews"] = true ctxUser := getDashboardContextUser(ctx) if ctx.Written() { return } // Check context type. if !ctxUser.IsOrganization() { // Normal user. ctxUser = ctx.User collaborates, err := ctx.User.GetAccessibleRepositories() if err != nil { ctx.Handle(500, "GetAccessibleRepositories", err) return } repositories := make([]*models.Repository, 0, len(collaborates)) for repo := range collaborates { repositories = append(repositories, repo) } ctx.Data["CollaborateCount"] = len(repositories) ctx.Data["CollaborativeRepos"] = repositories } repos, err := models.GetRepositories(ctxUser.Id, true) if err != nil { ctx.Handle(500, "GetRepositories", err) return } ctx.Data["Repos"] = repos // Get mirror repositories. mirrors := make([]*models.Repository, 0, len(repos)/2) for _, repo := range repos { if repo.IsMirror { if err = repo.GetMirror(); err != nil { ctx.Handle(500, "GetMirror: "+repo.Name, err) return } mirrors = append(mirrors, repo) } } ctx.Data["MirrorCount"] = len(mirrors) ctx.Data["Mirrors"] = mirrors // Get feeds. actions, err := models.GetFeeds(ctxUser.Id, 0, false) if err != nil { ctx.Handle(500, "GetFeeds", err) return } // Check access of private repositories. feeds := make([]*models.Action, 0, len(actions)) for _, act := range actions { if act.IsPrivate { // This prevents having to retrieve the repository for each action repo := &models.Repository{ID: act.RepoID, IsPrivate: true} if act.RepoUserName != ctx.User.LowerName { if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has { continue } } } // FIXME: cache results? u, err := models.GetUserByName(act.ActUserName) if err != nil { if models.IsErrUserNotExist(err) { continue } ctx.Handle(500, "GetUserByName", err) return } act.ActAvatar = u.AvatarLink() feeds = append(feeds, act) } ctx.Data["Feeds"] = feeds ctx.HTML(200, DASHBOARD) }
func Issues(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("issues") ctx.Data["PageIsIssues"] = true ctxUser := getDashboardContextUser(ctx) if ctx.Written() { return } // Organization does not have view type and filter mode. var ( viewType string filterMode = models.FM_ALL assigneeID int64 posterID int64 ) if ctxUser.IsOrganization() { viewType = "all" } else { viewType = ctx.Query("type") types := []string{"assigned", "created_by"} if !com.IsSliceContainsStr(types, viewType) { viewType = "all" } switch viewType { case "assigned": filterMode = models.FM_ASSIGN assigneeID = ctxUser.Id case "created_by": filterMode = models.FM_CREATE posterID = ctxUser.Id } } repoID := ctx.QueryInt64("repo") isShowClosed := ctx.Query("state") == "closed" // Get repositories. repos, err := models.GetRepositories(ctxUser.Id, true) if err != nil { ctx.Handle(500, "GetRepositories", err) return } allCount := 0 repoIDs := make([]int64, 0, len(repos)) showRepos := make([]*models.Repository, 0, len(repos)) for _, repo := range repos { if repo.NumIssues == 0 { continue } repoIDs = append(repoIDs, repo.ID) repo.NumOpenIssues = repo.NumIssues - repo.NumClosedIssues allCount += repo.NumOpenIssues if filterMode != models.FM_ALL { // Calculate repository issue count with filter mode. numOpen, numClosed := repo.IssueStats(ctxUser.Id, filterMode) repo.NumOpenIssues, repo.NumClosedIssues = int(numOpen), int(numClosed) } if repo.ID == repoID || (isShowClosed && repo.NumClosedIssues > 0) || (!isShowClosed && repo.NumOpenIssues > 0) { showRepos = append(showRepos, repo) } } ctx.Data["Repos"] = showRepos issueStats := models.GetUserIssueStats(repoID, ctxUser.Id, repoIDs, filterMode) issueStats.AllCount = int64(allCount) page := ctx.QueryInt("page") if page <= 1 { page = 1 } var total int if !isShowClosed { total = int(issueStats.OpenCount) } else { total = int(issueStats.ClosedCount) } ctx.Data["Page"] = paginater.New(total, setting.IssuePagingNum, page, 5) // Get issues. issues, err := models.Issues(ctxUser.Id, assigneeID, repoID, posterID, 0, repoIDs, page, isShowClosed, false, "", "") if err != nil { ctx.Handle(500, "Issues: %v", err) return } // Get posters and repository. for i := range issues { issues[i].Repo, err = models.GetRepositoryByID(issues[i].RepoID) if err != nil { ctx.Handle(500, "GetRepositoryByID", fmt.Errorf("[#%d]%v", issues[i].ID, err)) return } if err = issues[i].Repo.GetOwner(); err != nil { ctx.Handle(500, "GetOwner", fmt.Errorf("[#%d]%v", issues[i].ID, err)) return } if err = issues[i].GetPoster(); err != nil { ctx.Handle(500, "GetPoster", fmt.Errorf("[#%d]%v", issues[i].ID, err)) return } } ctx.Data["Issues"] = issues ctx.Data["IssueStats"] = issueStats ctx.Data["ViewType"] = viewType ctx.Data["RepoID"] = repoID ctx.Data["IsShowClosed"] = isShowClosed if isShowClosed { ctx.Data["State"] = "closed" } else { ctx.Data["State"] = "open" } ctx.HTML(200, ISSUES) }
func NewIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) { ctx.Data["Title"] = ctx.Tr("repo.issues.new") ctx.Data["PageIsIssueList"] = true ctx.Data["RequireDropzone"] = true renderAttachmentSettings(ctx) var ( repo = ctx.Repo.Repository labelIDs []int64 milestoneID int64 assigneeID int64 attachments []string err error ) if ctx.Repo.IsAdmin() { labels := RetrieveRepoMetas(ctx, repo) if ctx.Written() { return } // Check labels. labelIDs = base.StringsToInt64s(strings.Split(form.LabelIDs, ",")) labelIDMark := base.Int64sToMap(labelIDs) hasSelected := false for i := range labels { if labelIDMark[labels[i].ID] { labels[i].IsChecked = true hasSelected = true } } ctx.Data["HasSelectedLabel"] = hasSelected ctx.Data["label_ids"] = form.LabelIDs ctx.Data["Labels"] = labels // Check milestone. milestoneID = form.MilestoneID if milestoneID > 0 { ctx.Data["Milestone"], err = repo.GetMilestoneByID(milestoneID) if err != nil { ctx.Handle(500, "GetMilestoneByID: %v", err) return } ctx.Data["milestone_id"] = milestoneID } // Check assignee. assigneeID = form.AssigneeID if assigneeID > 0 { ctx.Data["Assignee"], err = repo.GetAssigneeByID(assigneeID) if err != nil { ctx.Handle(500, "GetAssigneeByID: %v", err) return } ctx.Data["assignee_id"] = assigneeID } } if setting.AttachmentEnabled { attachments = form.Attachments } if ctx.HasError() { ctx.HTML(200, ISSUE_NEW) return } issue := &models.Issue{ RepoID: ctx.Repo.Repository.ID, Index: int64(repo.NumIssues) + 1, Name: form.Title, PosterID: ctx.User.Id, Poster: ctx.User, MilestoneID: milestoneID, AssigneeID: assigneeID, Content: form.Content, } if err := models.NewIssue(repo, issue, labelIDs, attachments); err != nil { ctx.Handle(500, "NewIssue", err) return } // Update mentions. mentions := base.MentionPattern.FindAllString(issue.Content, -1) if len(mentions) > 0 { for i := range mentions { mentions[i] = mentions[i][1:] } if err := models.UpdateMentions(mentions, issue.ID); err != nil { ctx.Handle(500, "UpdateMentions", err) return } } // Mail watchers and mentions. if setting.Service.EnableNotifyMail { tos, err := mailer.SendIssueNotifyMail(ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue) if err != nil { ctx.Handle(500, "SendIssueNotifyMail", err) return } tos = append(tos, ctx.User.LowerName) newTos := make([]string, 0, len(mentions)) for _, m := range mentions { if com.IsSliceContainsStr(tos, m) { continue } newTos = append(newTos, m) } if err = mailer.SendIssueMentionMail(ctx.Render, ctx.User, ctx.Repo.Owner, ctx.Repo.Repository, issue, models.GetUserEmailsByNames(newTos)); err != nil { ctx.Handle(500, "SendIssueMentionMail", err) return } } log.Trace("Issue created: %d/%d", ctx.Repo.Repository.ID, issue.ID) ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index)) }