func Action(ctx *context.Context) { u := GetUserByParams(ctx) if ctx.Written() { return } var err error switch ctx.Params(":action") { case "follow": err = models.FollowUser(ctx.User.Id, u.Id) case "unfollow": err = models.UnfollowUser(ctx.User.Id, u.Id) } if err != nil { ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err) return } redirectTo := ctx.Query("redirect_to") if len(redirectTo) == 0 { redirectTo = u.HomeLink() } ctx.Redirect(redirectTo) }
func DeleteAuthSource(ctx *context.Context) { source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid")) if err != nil { ctx.Handle(500, "GetLoginSourceByID", err) return } if err = models.DeleteSource(source); err != nil { switch err { case models.ErrAuthenticationUserUsed: ctx.Flash.Error(ctx.Tr("admin.auths.still_in_used")) default: ctx.Flash.Error(fmt.Sprintf("DeleteSource: %v", err)) } ctx.JSON(200, map[string]interface{}{ "redirect": setting.AppSubUrl + "/admin/auths/" + ctx.Params(":authid"), }) return } log.Trace("Authentication deleted by admin(%s): %d", ctx.User.Name, source.ID) ctx.Flash.Success(ctx.Tr("admin.auths.deletion_success")) ctx.JSON(200, map[string]interface{}{ "redirect": setting.AppSubUrl + "/admin/auths", }) }
func DeleteUser(ctx *context.Context) { u, err := models.GetUserByID(ctx.ParamsInt64(":userid")) if err != nil { ctx.Handle(500, "GetUserByID", err) return } if err = models.DeleteUser(u); err != nil { switch { case models.IsErrUserOwnRepos(err): ctx.Flash.Error(ctx.Tr("admin.users.still_own_repo")) ctx.JSON(200, map[string]interface{}{ "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"), }) case models.IsErrUserHasOrgs(err): ctx.Flash.Error(ctx.Tr("admin.users.still_has_org")) ctx.JSON(200, map[string]interface{}{ "redirect": setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid"), }) default: ctx.Handle(500, "DeleteUser", err) } return } log.Trace("Account deleted by admin (%s): %s", ctx.User.Name, u.Name) ctx.Flash.Success(ctx.Tr("admin.users.deletion_success")) ctx.JSON(200, map[string]interface{}{ "redirect": setting.AppSubUrl + "/admin/users", }) }
func TeamsRepoAction(ctx *context.Context) { if !ctx.Org.IsOwner { ctx.Error(404) return } var err error switch ctx.Params(":action") { case "add": repoName := path.Base(ctx.Query("repo_name")) var repo *models.Repository repo, err = models.GetRepositoryByName(ctx.Org.Organization.Id, repoName) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Flash.Error(ctx.Tr("org.teams.add_nonexistent_repo")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories") return } ctx.Handle(500, "GetRepositoryByName", err) return } err = ctx.Org.Team.AddRepository(repo) case "remove": err = ctx.Org.Team.RemoveRepository(com.StrTo(ctx.Query("repoid")).MustInt64()) } if err != nil { log.Error(3, "Action(%s): '%s' %v", ctx.Params(":action"), ctx.Org.Team.Name, err) ctx.Handle(500, "TeamsRepoAction", err) return } ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName + "/repositories") }
func ChangeMilestonStatus(ctx *context.Context) { m, err := models.GetMilestoneByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Handle(404, "GetMilestoneByID", err) } else { ctx.Handle(500, "GetMilestoneByID", err) } return } switch ctx.Params(":action") { case "open": if m.IsClosed { if err = models.ChangeMilestoneStatus(m, false); err != nil { ctx.Handle(500, "ChangeMilestoneStatus", err) return } } ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=open") case "close": if !m.IsClosed { m.ClosedDate = time.Now() if err = models.ChangeMilestoneStatus(m, true); err != nil { ctx.Handle(500, "ChangeMilestoneStatus", err) return } } ctx.Redirect(ctx.Repo.RepoLink + "/milestones?state=closed") default: ctx.Redirect(ctx.Repo.RepoLink + "/milestones") } }
func getDashboardContextUser(ctx *context.Context) *models.User { ctxUser := ctx.User orgName := ctx.Params(":org") if len(orgName) > 0 { // Organization. org, err := models.GetUserByName(orgName) if err != nil { if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", err) } else { ctx.Handle(500, "GetUserByName", err) } return nil } ctxUser = org } ctx.Data["ContextUser"] = ctxUser if err := ctx.User.GetOrganizations(true); err != nil { ctx.Handle(500, "GetOrganizations", err) return nil } ctx.Data["Orgs"] = ctx.User.Orgs return ctxUser }
func Action(ctx *context.Context) { var err error switch ctx.Params(":action") { case "watch": err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, true) case "unwatch": err = models.WatchRepo(ctx.User.Id, ctx.Repo.Repository.ID, false) case "star": err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, true) case "unstar": err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.ID, false) case "desc": // FIXME: this is not used if !ctx.Repo.IsOwner() { ctx.Error(404) return } ctx.Repo.Repository.Description = ctx.Query("desc") ctx.Repo.Repository.Website = ctx.Query("site") err = models.UpdateRepository(ctx.Repo.Repository, false) } if err != nil { ctx.Handle(500, fmt.Sprintf("Action (%s)", ctx.Params(":action")), err) return } redirectTo := ctx.Query("redirect_to") if len(redirectTo) == 0 { redirectTo = ctx.Repo.RepoLink } ctx.Redirect(redirectTo) }
func checkHookType(ctx *context.Context) string { hookType := strings.ToLower(ctx.Params(":type")) if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) { ctx.Handle(404, "checkHookType", nil) return "" } return hookType }
func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) { ctx.Data["Title"] = ctx.Tr("admin.users.edit_account") ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdminUsers"] = true u := prepareUserInfo(ctx) if ctx.Written() { return } if ctx.HasError() { ctx.HTML(200, USER_EDIT) return } fields := strings.Split(form.LoginType, "-") if len(fields) == 2 { loginType := models.LoginType(com.StrTo(fields[0]).MustInt()) loginSource := com.StrTo(fields[1]).MustInt64() if u.LoginSource != loginSource { u.LoginSource = loginSource u.LoginType = loginType } } if len(form.Password) > 0 { u.Passwd = form.Password u.Salt = models.GetUserSalt() u.EncodePasswd() } u.LoginName = form.LoginName u.FullName = form.FullName u.Email = form.Email u.Website = form.Website u.Location = form.Location u.MaxRepoCreation = form.MaxRepoCreation u.IsActive = form.Active u.IsAdmin = form.Admin u.AllowGitHook = form.AllowGitHook u.AllowImportLocal = form.AllowImportLocal if err := models.UpdateUser(u); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.Data["Err_Email"] = true ctx.RenderWithErr(ctx.Tr("form.email_been_used"), USER_EDIT, &form) } else { ctx.Handle(500, "UpdateUser", err) } return } log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) ctx.Flash.Success(ctx.Tr("admin.users.update_profile_success")) ctx.Redirect(setting.AppSubUrl + "/admin/users/" + ctx.Params(":userid")) }
func TemplatePreview(ctx *context.Context) { ctx.Data["User"] = models.User{Name: "Unknown"} ctx.Data["AppName"] = setting.AppName ctx.Data["AppVer"] = setting.AppVer ctx.Data["AppUrl"] = setting.AppUrl ctx.Data["Code"] = "2014031910370000009fff6782aadb2162b4a997acb69d4400888e0b9274657374" ctx.Data["ActiveCodeLives"] = setting.Service.ActiveCodeLives / 60 ctx.Data["ResetPwdCodeLives"] = setting.Service.ResetPwdCodeLives / 60 ctx.Data["CurDbValue"] = "" ctx.HTML(200, base.TplName(ctx.Params("*"))) }
func Diff(ctx *context.Context) { ctx.Data["PageIsDiff"] = true userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name commitID := ctx.Params(":sha") commit, err := ctx.Repo.GitRepo.GetCommit(commitID) if err != nil { ctx.Handle(500, "Repo.GitRepo.GetCommit", err) return } diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), commitID, setting.Git.MaxGitDiffLines) if err != nil { ctx.Handle(404, "GetDiffCommit", err) return } parents := make([]string, commit.ParentCount()) for i := 0; i < commit.ParentCount(); i++ { sha, err := commit.ParentID(i) parents[i] = sha.String() if err != nil { ctx.Handle(404, "repo.Diff", err) return } } ctx.Data["CommitID"] = commitID ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName ctx.Data["IsImageFile"] = commit.IsImageFile ctx.Data["Title"] = commit.Summary() + " · " + base.ShortSha(commitID) ctx.Data["Commit"] = commit ctx.Data["Author"] = models.ValidateCommitWithEmail(commit) ctx.Data["Diff"] = diff ctx.Data["Parents"] = parents ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", commitID) if commit.ParentCount() > 0 { ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0]) } ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID) ctx.Data["RequireHighlightJS"] = true ctx.HTML(200, DIFF) }
func DeleteWikiPagePost(ctx *context.Context) { pageURL := ctx.Params(":page") if len(pageURL) == 0 { pageURL = "Home" } pageName := models.ToWikiPageName(pageURL) if err := ctx.Repo.Repository.DeleteWikiPage(ctx.User, pageName); err != nil { ctx.Handle(500, "DeleteWikiPage", err) return } ctx.JSON(200, map[string]interface{}{ "redirect": ctx.Repo.RepoLink + "/wiki/", }) }
func GitHooksEdit(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.githooks") ctx.Data["PageIsSettingsGitHooks"] = true name := ctx.Params(":name") hook, err := ctx.Repo.GitRepo.GetHook(name) if err != nil { if err == git.ErrNotValidHook { ctx.Handle(404, "GetHook", err) } else { ctx.Handle(500, "GetHook", err) } return } ctx.Data["Hook"] = hook ctx.HTML(200, GITHOOK_EDIT) }
func CompareDiff(ctx *context.Context) { ctx.Data["IsRepoToolbarCommits"] = true ctx.Data["IsDiffCompare"] = true userName := ctx.Repo.Owner.Name repoName := ctx.Repo.Repository.Name beforeCommitID := ctx.Params(":before") afterCommitID := ctx.Params(":after") commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID) if err != nil { ctx.Handle(404, "GetCommit", err) return } diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID, afterCommitID, setting.Git.MaxGitDiffLines) if err != nil { ctx.Handle(404, "GetDiffRange", err) return } commits, err := commit.CommitsBeforeUntil(beforeCommitID) if err != nil { ctx.Handle(500, "CommitsBeforeUntil", err) return } commits = models.ValidateCommitsWithEmails(commits) ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split" ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink ctx.Data["Commits"] = commits ctx.Data["CommitCount"] = commits.Len() ctx.Data["BeforeCommitID"] = beforeCommitID ctx.Data["AfterCommitID"] = afterCommitID ctx.Data["Username"] = userName ctx.Data["Reponame"] = repoName ctx.Data["IsImageFile"] = commit.IsImageFile ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName ctx.Data["Commit"] = commit ctx.Data["Diff"] = diff ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0 ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", afterCommitID) ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", beforeCommitID) ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", afterCommitID) ctx.HTML(200, DIFF) }
func GitHooksEditPost(ctx *context.Context) { name := ctx.Params(":name") hook, err := ctx.Repo.GitRepo.GetHook(name) if err != nil { if err == git.ErrNotValidHook { ctx.Handle(404, "GetHook", err) } else { ctx.Handle(500, "GetHook", err) } return } hook.Content = ctx.Query("content") if err = hook.Update(); err != nil { ctx.Handle(500, "hook.Update", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git") }
func showOrgProfile(ctx *context.Context) { ctx.SetParams(":org", ctx.Params(":username")) context.HandleOrgAssignment(ctx) if ctx.Written() { return } org := ctx.Org.Organization ctx.Data["Title"] = org.FullName if ctx.IsSigned { if ctx.User.IsAdmin { repos, err := models.GetRepositories(org.Id, true) if err != nil { ctx.Handle(500, "GetRepositoriesAsAdmin", err) return } ctx.Data["Repos"] = repos } else { if err := org.GetUserRepositories(ctx.User.Id); err != nil { ctx.Handle(500, "GetUserRepositories", err) return } ctx.Data["Repos"] = org.Repos } } else { repos, err := models.GetRepositories(org.Id, false) if err != nil { ctx.Handle(500, "GetRepositories", err) return } ctx.Data["Repos"] = repos } if err := org.GetMembers(); err != nil { ctx.Handle(500, "GetMembers", err) return } ctx.Data["Members"] = org.Members ctx.Data["Teams"] = org.Teams ctx.HTML(200, ORG_HOME) }
func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) { owner, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", err) } else { ctx.Handle(500, "GetUserByName", err) } return nil, nil } repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame")) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Handle(404, "GetRepositoryByName", err) } else { ctx.Handle(500, "GetRepositoryByName", err) } return nil, nil } return owner, repo }
func MembersAction(ctx *context.Context) { uid := com.StrTo(ctx.Query("uid")).MustInt64() if uid == 0 { ctx.Redirect(ctx.Org.OrgLink + "/members") return } org := ctx.Org.Organization var err error switch ctx.Params(":action") { case "private": if ctx.User.Id != uid && !ctx.Org.IsOwner { ctx.Error(404) return } err = models.ChangeOrgUserStatus(org.Id, uid, false) case "public": if ctx.User.Id != uid && !ctx.Org.IsOwner { ctx.Error(404) return } err = models.ChangeOrgUserStatus(org.Id, uid, true) case "remove": if !ctx.Org.IsOwner { ctx.Error(404) return } err = org.RemoveMember(uid) if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.Redirect(ctx.Org.OrgLink + "/members") return } case "leave": err = org.RemoveMember(ctx.User.Id) if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) ctx.Redirect(ctx.Org.OrgLink + "/members") return } } if err != nil { log.Error(4, "Action(%s): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } if ctx.Params(":action") != "leave" { ctx.Redirect(ctx.Org.OrgLink + "/members") } else { ctx.Redirect(setting.AppSubUrl + "/") } }
func EditReleasePost(ctx *context.Context, form auth.EditReleaseForm) { ctx.Data["Title"] = ctx.Tr("repo.release.edit_release") ctx.Data["PageIsReleaseList"] = true ctx.Data["PageIsEditRelease"] = true tagName := ctx.Params(":tagname") rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName) if err != nil { if models.IsErrReleaseNotExist(err) { ctx.Handle(404, "GetRelease", err) } else { ctx.Handle(500, "GetRelease", err) } return } ctx.Data["tag_name"] = rel.TagName ctx.Data["tag_target"] = rel.Target ctx.Data["title"] = rel.Title ctx.Data["content"] = rel.Note ctx.Data["prerelease"] = rel.IsPrerelease if ctx.HasError() { ctx.HTML(200, RELEASE_NEW) return } rel.Title = form.Title rel.Note = form.Content rel.IsDraft = len(form.Draft) > 0 rel.IsPrerelease = form.Prerelease if err = models.UpdateRelease(ctx.Repo.GitRepo, rel); err != nil { ctx.Handle(500, "UpdateRelease", err) return } ctx.Redirect(ctx.Repo.RepoLink + "/releases") }
func EditRelease(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.release.edit_release") ctx.Data["PageIsReleaseList"] = true ctx.Data["PageIsEditRelease"] = true tagName := ctx.Params(":tagname") rel, err := models.GetRelease(ctx.Repo.Repository.ID, tagName) if err != nil { if models.IsErrReleaseNotExist(err) { ctx.Handle(404, "GetRelease", err) } else { ctx.Handle(500, "GetRelease", err) } return } ctx.Data["ID"] = rel.ID ctx.Data["tag_name"] = rel.TagName ctx.Data["tag_target"] = rel.Target ctx.Data["title"] = rel.Title ctx.Data["content"] = rel.Note ctx.Data["prerelease"] = rel.IsPrerelease ctx.HTML(200, RELEASE_NEW) }
func Issues(ctx *context.Context) { isPullList := ctx.Params(":type") == "pulls" if isPullList { MustAllowPulls(ctx) if ctx.Written() { return } ctx.Data["Title"] = ctx.Tr("repo.pulls") ctx.Data["PageIsPullList"] = true } else { MustEnableIssues(ctx) if ctx.Written() { return } ctx.Data["Title"] = ctx.Tr("repo.issues") ctx.Data["PageIsIssueList"] = true } viewType := ctx.Query("type") sortType := ctx.Query("sort") types := []string{"assigned", "created_by", "mentioned"} if !com.IsSliceContainsStr(types, viewType) { viewType = "all" } // Must sign in to see issues about you. if viewType != "all" && !ctx.IsSigned { ctx.SetCookie("redirect_to", "/"+url.QueryEscape(setting.AppSubUrl+ctx.Req.RequestURI), 0, setting.AppSubUrl) ctx.Redirect(setting.AppSubUrl + "/user/login") return } var ( assigneeID = ctx.QueryInt64("assignee") posterID int64 ) filterMode := models.FM_ALL switch viewType { case "assigned": filterMode = models.FM_ASSIGN assigneeID = ctx.User.Id case "created_by": filterMode = models.FM_CREATE posterID = ctx.User.Id case "mentioned": filterMode = models.FM_MENTION } var uid int64 = -1 if ctx.IsSigned { uid = ctx.User.Id } repo := ctx.Repo.Repository selectLabels := ctx.Query("labels") milestoneID := ctx.QueryInt64("milestone") isShowClosed := ctx.Query("state") == "closed" issueStats := models.GetIssueStats(&models.IssueStatsOptions{ RepoID: repo.ID, UserID: uid, Labels: selectLabels, MilestoneID: milestoneID, AssigneeID: assigneeID, FilterMode: filterMode, IsPull: isPullList, }) page := ctx.QueryInt("page") if page <= 1 { page = 1 } var total int if !isShowClosed { total = int(issueStats.OpenCount) } else { total = int(issueStats.ClosedCount) } pager := paginater.New(total, setting.IssuePagingNum, page, 5) ctx.Data["Page"] = pager // Get issues. issues, err := models.Issues(&models.IssuesOptions{ UserID: uid, AssigneeID: assigneeID, RepoID: repo.ID, PosterID: posterID, MilestoneID: milestoneID, Page: pager.Current(), IsClosed: isShowClosed, IsMention: filterMode == models.FM_MENTION, IsPull: isPullList, Labels: selectLabels, SortType: sortType, }) if err != nil { ctx.Handle(500, "Issues: %v", err) return } // Get issue-user relations. pairs, err := models.GetIssueUsers(repo.ID, posterID, isShowClosed) if err != nil { ctx.Handle(500, "GetIssueUsers: %v", err) return } // Get posters. for i := range issues { if !ctx.IsSigned { issues[i].IsRead = true continue } // Check read status. idx := models.PairsContains(pairs, issues[i].ID, ctx.User.Id) if idx > -1 { issues[i].IsRead = pairs[idx].IsRead } else { issues[i].IsRead = true } } ctx.Data["Issues"] = issues // Get milestones. ctx.Data["Milestones"], err = models.GetAllRepoMilestones(repo.ID) if err != nil { ctx.Handle(500, "GetAllRepoMilestones: %v", err) return } // Get assignees. ctx.Data["Assignees"], err = repo.GetAssignees() if err != nil { ctx.Handle(500, "GetAssignees: %v", err) return } ctx.Data["IssueStats"] = issueStats ctx.Data["SelectLabels"] = com.StrTo(selectLabels).MustInt64() ctx.Data["ViewType"] = viewType ctx.Data["SortType"] = sortType ctx.Data["MilestoneID"] = milestoneID ctx.Data["AssigneeID"] = assigneeID ctx.Data["IsShowClosed"] = isShowClosed if isShowClosed { ctx.Data["State"] = "closed" } else { ctx.Data["State"] = "open" } ctx.HTML(200, ISSUES) }
func ViewIssue(ctx *context.Context) { ctx.Data["RequireDropzone"] = true renderAttachmentSettings(ctx) issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Handle(404, "GetIssueByIndex", err) } else { ctx.Handle(500, "GetIssueByIndex", err) } return } ctx.Data["Title"] = issue.Name // Make sure type and URL matches. if ctx.Params(":type") == "issues" && issue.IsPull { ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(issue.Index)) return } else if ctx.Params(":type") == "pulls" && !issue.IsPull { ctx.Redirect(ctx.Repo.RepoLink + "/issues/" + com.ToStr(issue.Index)) return } if issue.IsPull { MustAllowPulls(ctx) if ctx.Written() { return } ctx.Data["PageIsPullList"] = true if err = issue.GetPullRequest(); err != nil { ctx.Handle(500, "GetPullRequest", err) return } ctx.Data["PageIsPullConversation"] = true } else { MustEnableIssues(ctx) if ctx.Written() { return } ctx.Data["PageIsIssueList"] = true } issue.RenderedContent = string(markdown.Render([]byte(issue.Content), ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) repo := ctx.Repo.Repository // Get more information if it's a pull request. if issue.IsPull { if issue.HasMerged { ctx.Data["DisableStatusChange"] = issue.HasMerged PrepareMergedViewPullInfo(ctx, issue) } else { PrepareViewPullInfo(ctx, issue) } if ctx.Written() { return } } // Metas. // Check labels. labelIDMark := make(map[int64]bool) for i := range issue.Labels { labelIDMark[issue.Labels[i].ID] = true } labels, err := models.GetLabelsByRepoID(repo.ID) if err != nil { ctx.Handle(500, "GetLabelsByRepoID: %v", err) return } hasSelected := false for i := range labels { if labelIDMark[labels[i].ID] { labels[i].IsChecked = true hasSelected = true } } ctx.Data["HasSelectedLabel"] = hasSelected ctx.Data["Labels"] = labels // Check milestone and assignee. if ctx.Repo.IsWriter() { RetrieveRepoMilestonesAndAssignees(ctx, repo) if ctx.Written() { return } } if ctx.IsSigned { // Update issue-user. if err = issue.ReadBy(ctx.User.Id); err != nil { ctx.Handle(500, "ReadBy", err) return } } var ( tag models.CommentTag ok bool marked = make(map[int64]models.CommentTag) comment *models.Comment participants = make([]*models.User, 1, 10) ) // Render comments and and fetch participants. participants[0] = issue.Poster for _, comment = range issue.Comments { if comment.Type == models.COMMENT_TYPE_COMMENT { comment.RenderedContent = string(markdown.Render([]byte(comment.Content), ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) // Check tag. tag, ok = marked[comment.PosterID] if ok { comment.ShowTag = tag continue } if repo.IsOwnedBy(comment.PosterID) || (repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(comment.PosterID)) { comment.ShowTag = models.COMMENT_TAG_OWNER } else if comment.Poster.IsWriterOfRepo(repo) { comment.ShowTag = models.COMMENT_TAG_WRITER } else if comment.PosterID == issue.PosterID { comment.ShowTag = models.COMMENT_TAG_POSTER } marked[comment.PosterID] = comment.ShowTag isAdded := false for j := range participants { if comment.Poster == participants[j] { isAdded = true break } } if !isAdded && !issue.IsPoster(comment.Poster.Id) { participants = append(participants, comment.Poster) } } } ctx.Data["Participants"] = participants ctx.Data["NumParticipants"] = len(participants) ctx.Data["Issue"] = issue ctx.Data["IsIssueOwner"] = ctx.Repo.IsWriter() || (ctx.IsSigned && issue.IsPoster(ctx.User.Id)) ctx.Data["SignInLink"] = setting.AppSubUrl + "/user/login" ctx.Data["RequireHighlightJS"] = true ctx.HTML(200, ISSUE_VIEW) }
func TeamsAction(ctx *context.Context) { uid := com.StrTo(ctx.Query("uid")).MustInt64() if uid == 0 { ctx.Redirect(ctx.Org.OrgLink + "/teams") return } page := ctx.Query("page") var err error switch ctx.Params(":action") { case "join": if !ctx.Org.IsOwner { ctx.Error(404) return } err = ctx.Org.Team.AddMember(ctx.User.Id) case "leave": err = ctx.Org.Team.RemoveMember(ctx.User.Id) case "remove": if !ctx.Org.IsOwner { ctx.Error(404) return } err = ctx.Org.Team.RemoveMember(uid) page = "team" case "add": if !ctx.Org.IsOwner { ctx.Error(404) return } uname := ctx.Query("uname") var u *models.User u, err = models.GetUserByName(uname) if err != nil { if models.IsErrUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName) } else { ctx.Handle(500, " GetUserByName", err) } return } err = ctx.Org.Team.AddMember(u.Id) page = "team" } if err != nil { if models.IsErrLastOrgOwner(err) { ctx.Flash.Error(ctx.Tr("form.last_org_owner")) } else { log.Error(3, "Action(%s): %v", ctx.Params(":action"), err) ctx.JSON(200, map[string]interface{}{ "ok": false, "err": err.Error(), }) return } } switch page { case "team": ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName) default: ctx.Redirect(ctx.Org.OrgLink + "/teams") } }
func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) { baseRepo := ctx.Repo.Repository // Get compared branches information // format: <base branch>...[<head repo>:]<head branch> // base<-head: master...head:feature // same repo: master...feature infos := strings.Split(ctx.Params("*"), "...") if len(infos) != 2 { log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos) ctx.Handle(404, "CompareAndPullRequest", nil) return nil, nil, nil, nil, "", "" } baseBranch := infos[0] ctx.Data["BaseBranch"] = baseBranch var ( headUser *models.User headBranch string isSameRepo bool err error ) // If there is no head repository, it means pull request between same repository. headInfos := strings.Split(infos[1], ":") if len(headInfos) == 1 { isSameRepo = true headUser = ctx.Repo.Owner headBranch = headInfos[0] } else if len(headInfos) == 2 { headUser, err = models.GetUserByName(headInfos[0]) if err != nil { if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", nil) } else { ctx.Handle(500, "GetUserByName", err) } return nil, nil, nil, nil, "", "" } headBranch = headInfos[1] } else { ctx.Handle(404, "CompareAndPullRequest", nil) return nil, nil, nil, nil, "", "" } ctx.Data["HeadUser"] = headUser ctx.Data["HeadBranch"] = headBranch ctx.Repo.PullRequest.SameRepo = isSameRepo // Check if base branch is valid. if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) { ctx.Handle(404, "IsBranchExist", nil) return nil, nil, nil, nil, "", "" } // Check if current user has fork of repository or in the same repository. headRepo, has := models.HasForkedRepo(headUser.Id, baseRepo.ID) if !has && !isSameRepo { log.Trace("ParseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID) ctx.Handle(404, "ParseCompareInfo", nil) return nil, nil, nil, nil, "", "" } var headGitRepo *git.Repository if isSameRepo { headRepo = ctx.Repo.Repository headGitRepo = ctx.Repo.GitRepo } else { headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name)) if err != nil { ctx.Handle(500, "OpenRepository", err) return nil, nil, nil, nil, "", "" } } if !ctx.User.IsWriterOfRepo(headRepo) && !ctx.User.IsAdmin { log.Trace("ParseCompareInfo[%d]: does not have write access or site admin", baseRepo.ID) ctx.Handle(404, "ParseCompareInfo", nil) return nil, nil, nil, nil, "", "" } // Check if head branch is valid. if !headGitRepo.IsBranchExist(headBranch) { ctx.Handle(404, "IsBranchExist", nil) return nil, nil, nil, nil, "", "" } headBranches, err := headGitRepo.GetBranches() if err != nil { ctx.Handle(500, "GetBranches", err) return nil, nil, nil, nil, "", "" } ctx.Data["HeadBranches"] = headBranches prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch) if err != nil { ctx.Handle(500, "GetPullRequestInfo", err) return nil, nil, nil, nil, "", "" } ctx.Data["BeforeCommitID"] = prInfo.MergeBase return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch }
func HTTP(ctx *context.Context) { username := ctx.Params(":username") reponame := strings.TrimSuffix(ctx.Params(":reponame"), ".git") var isPull bool service := ctx.Query("service") if service == "git-receive-pack" || strings.HasSuffix(ctx.Req.URL.Path, "git-receive-pack") { isPull = false } else if service == "git-upload-pack" || strings.HasSuffix(ctx.Req.URL.Path, "git-upload-pack") { isPull = true } else { isPull = (ctx.Req.Method == "GET") } isWiki := false if strings.HasSuffix(reponame, ".wiki") { isWiki = true reponame = reponame[:len(reponame)-5] } repoUser, err := models.GetUserByName(username) if err != nil { if models.IsErrUserNotExist(err) { ctx.Handle(404, "GetUserByName", nil) } else { ctx.Handle(500, "GetUserByName", err) } return } repo, err := models.GetRepositoryByName(repoUser.Id, reponame) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Handle(404, "GetRepositoryByName", nil) } else { ctx.Handle(500, "GetRepositoryByName", err) } return } // Only public pull don't need auth. isPublicPull := !repo.IsPrivate && isPull var ( askAuth = !isPublicPull || setting.Service.RequireSignInView authUser *models.User authUsername string authPasswd string ) // check access if askAuth { authHead := ctx.Req.Header.Get("Authorization") if len(authHead) == 0 { authRequired(ctx) return } auths := strings.Fields(authHead) // currently check basic auth // TODO: support digit auth // FIXME: middlewares/context.go did basic auth check already, // maybe could use that one. if len(auths) != 2 || auths[0] != "Basic" { ctx.HandleText(401, "no basic auth and digit auth") return } authUsername, authPasswd, err = base.BasicAuthDecode(auths[1]) if err != nil { ctx.HandleText(401, "no basic auth and digit auth") return } authUser, err = models.UserSignIn(authUsername, authPasswd) if err != nil { if !models.IsErrUserNotExist(err) { ctx.Handle(500, "UserSignIn error: %v", err) return } // Assume username now is a token. token, err := models.GetAccessTokenBySHA(authUsername) if err != nil { if models.IsErrAccessTokenNotExist(err) { ctx.HandleText(401, "invalid token") } else { ctx.Handle(500, "GetAccessTokenBySha", err) } return } token.Updated = time.Now() if err = models.UpdateAccessToken(token); err != nil { ctx.Handle(500, "UpdateAccessToken", err) } authUser, err = models.GetUserByID(token.UID) if err != nil { ctx.Handle(500, "GetUserByID", err) return } } if !isPublicPull { var tp = models.ACCESS_MODE_WRITE if isPull { tp = models.ACCESS_MODE_READ } has, err := models.HasAccess(authUser, repo, tp) if err != nil { ctx.Handle(500, "HasAccess", err) return } else if !has { if tp == models.ACCESS_MODE_READ { has, err = models.HasAccess(authUser, repo, models.ACCESS_MODE_WRITE) if err != nil { ctx.Handle(500, "HasAccess2", err) return } else if !has { ctx.HandleText(403, "User permission denied") return } } else { ctx.HandleText(403, "User permission denied") return } } if !isPull && repo.IsMirror { ctx.HandleText(403, "mirror repository is read-only") return } } } callback := func(rpc string, input []byte) { if rpc != "receive-pack" || isWiki { return } var lastLine int64 = 0 for { head := input[lastLine : lastLine+2] if head[0] == '0' && head[1] == '0' { size, err := strconv.ParseInt(string(input[lastLine+2:lastLine+4]), 16, 32) if err != nil { log.Error(4, "%v", err) return } if size == 0 { //fmt.Println(string(input[lastLine:])) break } line := input[lastLine : lastLine+size] idx := bytes.IndexRune(line, '\000') if idx > -1 { line = line[:idx] } fields := strings.Fields(string(line)) if len(fields) >= 3 { oldCommitId := fields[0][4:] newCommitId := fields[1] refName := fields[2] // FIXME: handle error. if err = models.PushUpdate(models.PushUpdateOptions{ RefName: refName, OldCommitID: oldCommitId, NewCommitID: newCommitId, PusherID: authUser.Id, PusherName: authUser.Name, RepoUserName: username, RepoName: reponame, }); err == nil { go models.HookQueue.Add(repo.ID) go models.AddTestPullRequestTask(repo.ID, strings.TrimPrefix(refName, "refs/heads/")) } } lastLine = lastLine + size } else { break } } } HTTPBackend(ctx, &Config{ RepoRootPath: setting.RepoRootPath, GitBinPath: "git", UploadPack: true, ReceivePack: true, OnSucceed: callback, })(ctx.Resp, ctx.Req.Request) runtime.GC() }
func renderWikiPage(ctx *context.Context, isViewPage bool) (*git.Repository, string) { wikiRepo, err := git.OpenRepository(ctx.Repo.Repository.WikiPath()) if err != nil { ctx.Handle(500, "OpenRepository", err) return nil, "" } commit, err := wikiRepo.GetBranchCommit("master") if err != nil { ctx.Handle(500, "GetBranchCommit", err) return nil, "" } // Get page list. if isViewPage { entries, err := commit.ListEntries() if err != nil { ctx.Handle(500, "ListEntries", err) return nil, "" } pages := make([]PageMeta, 0, len(entries)) for i := range entries { if entries[i].Type == git.OBJECT_BLOB { name := strings.TrimSuffix(entries[i].Name(), ".md") pages = append(pages, PageMeta{ Name: name, URL: models.ToWikiPageURL(name), }) } } ctx.Data["Pages"] = pages } pageURL := ctx.Params(":page") if len(pageURL) == 0 { pageURL = "Home" } ctx.Data["PageURL"] = pageURL pageName := models.ToWikiPageName(pageURL) ctx.Data["old_title"] = pageName ctx.Data["Title"] = pageName ctx.Data["title"] = pageName ctx.Data["RequireHighlightJS"] = true blob, err := commit.GetBlobByPath(pageName + ".md") if err != nil { if git.IsErrNotExist(err) { ctx.Redirect(ctx.Repo.RepoLink + "/wiki/_pages") } else { ctx.Handle(500, "GetBlobByPath", err) } return nil, "" } r, err := blob.Data() if err != nil { ctx.Handle(500, "Data", err) return nil, "" } data, err := ioutil.ReadAll(r) if err != nil { ctx.Handle(500, "ReadAll", err) return nil, "" } if isViewPage { ctx.Data["content"] = string(markdown.Render(data, ctx.Repo.RepoLink, ctx.Repo.Repository.ComposeMetas())) } else { ctx.Data["content"] = string(data) } return wikiRepo, pageName }
func Download(ctx *context.Context) { var ( uri = ctx.Params("*") refName string ext string archivePath string archiveType git.ArchiveType ) switch { case strings.HasSuffix(uri, ".zip"): ext = ".zip" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/zip") archiveType = git.ZIP case strings.HasSuffix(uri, ".tar.gz"): ext = ".tar.gz" archivePath = path.Join(ctx.Repo.GitRepo.Path, "archives/targz") archiveType = git.TARGZ default: log.Trace("Unknown format: %s", uri) ctx.Error(404) return } refName = strings.TrimSuffix(uri, ext) if !com.IsDir(archivePath) { if err := os.MkdirAll(archivePath, os.ModePerm); err != nil { ctx.Handle(500, "Download -> os.MkdirAll(archivePath)", err) return } } // Get corresponding commit. var ( commit *git.Commit err error ) gitRepo := ctx.Repo.GitRepo if gitRepo.IsBranchExist(refName) { commit, err = gitRepo.GetBranchCommit(refName) if err != nil { ctx.Handle(500, "GetBranchCommit", err) return } } else if gitRepo.IsTagExist(refName) { commit, err = gitRepo.GetTagCommit(refName) if err != nil { ctx.Handle(500, "GetTagCommit", err) return } } else if len(refName) == 40 { commit, err = gitRepo.GetCommit(refName) if err != nil { ctx.Handle(404, "GetCommit", nil) return } } else { ctx.Handle(404, "Download", nil) return } archivePath = path.Join(archivePath, base.ShortSha(commit.ID.String())+ext) if !com.IsFile(archivePath) { if err := commit.CreateArchive(archivePath, archiveType); err != nil { ctx.Handle(500, "Download -> CreateArchive "+archivePath, err) return } } ctx.ServeFile(archivePath, ctx.Repo.Repository.Name+"-"+refName+ext) }
// GetUserByParams returns user whose name is presented in URL paramenter. func GetUserByParams(ctx *context.Context) *models.User { return GetUserByName(ctx, ctx.Params(":username")) }
func Profile(ctx *context.Context) { uname := ctx.Params(":username") // Special handle for FireFox requests favicon.ico. if uname == "favicon.ico" { ctx.ServeFile(path.Join(setting.StaticRootPath, "public/img/favicon.png")) return } else if strings.HasSuffix(uname, ".png") { ctx.Error(404) return } isShowKeys := false if strings.HasSuffix(uname, ".keys") { isShowKeys = true } u := GetUserByName(ctx, strings.TrimSuffix(uname, ".keys")) if ctx.Written() { return } // Show SSH keys. if isShowKeys { ShowSSHKeys(ctx, u.Id) return } if u.IsOrganization() { showOrgProfile(ctx) return } ctx.Data["Title"] = u.DisplayName() ctx.Data["PageIsUserProfile"] = true ctx.Data["Owner"] = u orgs, err := models.GetOrgsByUserID(u.Id, ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.Id == u.Id)) if err != nil { ctx.Handle(500, "GetOrgsByUserIDDesc", err) return } ctx.Data["Orgs"] = orgs tab := ctx.Query("tab") ctx.Data["TabName"] = tab switch tab { case "activity": retrieveFeeds(ctx, u.Id, -1, 0, true) if ctx.Written() { return } default: var err error ctx.Data["Repos"], err = models.GetRepositories(u.Id, ctx.IsSigned && ctx.User.Id == u.Id) if err != nil { ctx.Handle(500, "GetRepositories", err) return } } ctx.HTML(200, PROFILE) }
func Issues(ctx *context.Context) { isPullList := ctx.Params(":type") == "pulls" if isPullList { ctx.Data["Title"] = ctx.Tr("pull_requests") ctx.Data["PageIsPulls"] = true } else { 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 sortType = ctx.Query("sort") 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. if ctxUser.IsOrganization() { if err := ctxUser.GetUserRepositories(ctx.User.Id); err != nil { ctx.Handle(500, "GetRepositories", err) return } } else { if err := ctxUser.GetRepositories(); err != nil { ctx.Handle(500, "GetRepositories", err) return } } repos := ctxUser.Repos allCount := 0 repoIDs := make([]int64, 0, len(repos)) showRepos := make([]*models.Repository, 0, len(repos)) for _, repo := range repos { if (isPullList && repo.NumPulls == 0) || (!isPullList && repo.NumIssues == 0) { continue } repoIDs = append(repoIDs, repo.ID) if isPullList { allCount += repo.NumOpenPulls repo.NumOpenIssues = repo.NumOpenPulls repo.NumClosedIssues = repo.NumClosedPulls } else { allCount += repo.NumOpenIssues } if filterMode != models.FM_ALL { // Calculate repository issue count with filter mode. numOpen, numClosed := repo.IssueStats(ctxUser.Id, filterMode, isPullList) 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, isPullList) 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(&models.IssuesOptions{ UserID: ctxUser.Id, AssigneeID: assigneeID, RepoID: repoID, PosterID: posterID, RepoIDs: repoIDs, Page: page, IsClosed: isShowClosed, IsPull: isPullList, SortType: sortType, }) if err != nil { ctx.Handle(500, "Issues", 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 } } ctx.Data["Issues"] = issues ctx.Data["IssueStats"] = issueStats ctx.Data["ViewType"] = viewType ctx.Data["SortType"] = sortType ctx.Data["RepoID"] = repoID ctx.Data["IsShowClosed"] = isShowClosed if isShowClosed { ctx.Data["State"] = "closed" } else { ctx.Data["State"] = "open" } ctx.HTML(200, ISSUES) }