func (t *HookTask) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { case "delivered": t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST") case "request_content": if len(t.RequestContent) == 0 { return } t.RequestInfo = &HookRequest{} if err = json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil { log.Error(3, "Unmarshal[%d]: %v", t.ID, err) } case "response_content": if len(t.ResponseContent) == 0 { return } t.ResponseInfo = &HookResponse{} if err = json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil { log.Error(3, "Unmarshal [%d]: %v", t.ID, err) } } }
// SignedInID returns the id of signed in user. func SignedInID(ctx *macaron.Context, sess session.Store) int64 { if !models.HasEngine { return 0 } // Check access token. if IsAPIPath(ctx.Req.URL.Path) { tokenSHA := ctx.Query("token") if len(tokenSHA) == 0 { // Well, check with header again. auHead := ctx.Req.Header.Get("Authorization") if len(auHead) > 0 { auths := strings.Fields(auHead) if len(auths) == 2 && auths[0] == "token" { tokenSHA = auths[1] } } } // Let's see if token is valid. if len(tokenSHA) > 0 { t, err := models.GetAccessTokenBySHA(tokenSHA) if err != nil { if models.IsErrAccessTokenNotExist(err) { log.Error(4, "GetAccessTokenBySHA: %v", err) } return 0 } t.Updated = time.Now() if err = models.UpdateAccessToken(t); err != nil { log.Error(4, "UpdateAccessToken: %v", err) } return t.UID } } uid := sess.Get("uid") if uid == nil { return 0 } if id, ok := uid.(int64); ok { if _, err := models.GetUserByID(id); err != nil { if !models.IsErrUserNotExist(err) { log.Error(4, "GetUserById: %v", err) } return 0 } return id } return 0 }
func addHeadRepoTasks(prs []*PullRequest) { for _, pr := range prs { log.Trace("addHeadRepoTasks[%d]: composing new test task", pr.ID) if err := pr.UpdatePatch(); err != nil { log.Error(4, "UpdatePatch: %v", err) continue } else if err := pr.PushToBaseRepo(); err != nil { log.Error(4, "PushToBaseRepo: %v", err) continue } pr.AddToTaskQueue() } }
func (w *Webhook) GetSlackHook() *SlackMeta { s := &SlackMeta{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { log.Error(4, "webhook.GetSlackHook(%d): %v", w.ID, err) } return s }
// saveAuthorizedKeyFile writes SSH key content to authorized_keys file. func saveAuthorizedKeyFile(keys ...*PublicKey) error { sshOpLocker.Lock() defer sshOpLocker.Unlock() fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys") f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) if err != nil { return err } defer f.Close() fi, err := f.Stat() if err != nil { return err } // FIXME: following command does not support in Windows. if !setting.IsWindows { // .ssh directory should have mode 700, and authorized_keys file should have mode 600. if fi.Mode().Perm() > 0600 { log.Error(4, "authorized_keys file has unusual permission flags: %s - setting to -rw-------", fi.Mode().Perm().String()) if err = f.Chmod(0600); err != nil { return err } } } for _, key := range keys { if _, err = f.WriteString(key.GetAuthorizedString()); err != nil { return err } } return nil }
// IsWriterOfRepo returns true if user has write access to given repository. func (u *User) IsWriterOfRepo(repo *Repository) bool { has, err := HasAccess(u, repo, ACCESS_MODE_WRITE) if err != nil { log.Error(3, "HasAccess: %v", err) } return has }
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 ChangeCollaborationAccessMode(ctx *context.Context) { if err := ctx.Repo.Repository.ChangeCollaborationAccessMode( ctx.QueryInt64("uid"), models.AccessMode(ctx.QueryInt("mode"))); err != nil { log.Error(4, "ChangeCollaborationAccessMode: %v", err) } }
// Listen starts a SSH server listens on given port. func Listen(port int) { config := &ssh.ServerConfig{ PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(ssh.MarshalAuthorizedKey(key)))) if err != nil { log.Error(3, "SearchPublicKeyByContent: %v", err) return nil, err } return &ssh.Permissions{Extensions: map[string]string{"key-id": com.ToStr(pkey.ID)}}, nil }, } keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa") if !com.IsExist(keyPath) { os.MkdirAll(filepath.Dir(keyPath), os.ModePerm) _, stderr, err := com.ExecCmd("ssh-keygen", "-f", keyPath, "-t", "rsa", "-N", "") if err != nil { panic(fmt.Sprintf("Fail to generate private key: %v - %s", err, stderr)) } log.Trace("SSH: New private key is generateed: %s", keyPath) } privateBytes, err := ioutil.ReadFile(keyPath) if err != nil { panic("SSH: Fail to load private key") } private, err := ssh.ParsePrivateKey(privateBytes) if err != nil { panic("SSH: Fail to parse private key") } config.AddHostKey(private) go listen(config, port) }
func ActionContent2Commits(act Actioner) *models.PushCommits { push := models.NewPushCommits() if err := json.Unmarshal([]byte(act.GetContent()), push); err != nil { log.Error(4, "json.Unmarshal:\n%s\nERROR: %v", act.GetContent(), err) } return push }
// GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own. func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) { accesses := make([]*Access, 0, 10) if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil { return nil, err } repos := make(map[*Repository]AccessMode, len(accesses)) for _, access := range accesses { repo, err := GetRepositoryByID(access.RepoID) if err != nil { if IsErrRepoNotExist(err) { log.Error(4, "GetRepositoryByID: %v", err) continue } return nil, err } if err = repo.GetOwner(); err != nil { return nil, err } else if repo.OwnerID == u.Id { continue } repos[repo] = access.Mode } return repos, nil }
func ToIssue(issue *models.Issue) *api.Issue { apiLabels := make([]*api.Label, len(issue.Labels)) for i := range issue.Labels { apiLabels[i] = ToLabel(issue.Labels[i]) } apiIssue := &api.Issue{ ID: issue.ID, Index: issue.Index, State: issue.State(), Title: issue.Name, Body: issue.Content, User: ToUser(issue.Poster), Labels: apiLabels, Assignee: ToUser(issue.Assignee), Milestone: ToMilestone(issue.Milestone), Comments: issue.NumComments, Created: issue.Created, Updated: issue.Updated, } if issue.IsPull { if err := issue.GetPullRequest(); err != nil { log.Error(4, "GetPullRequest", err) } else { apiIssue.PullRequest = &api.PullRequestMeta{ HasMerged: issue.PullRequest.HasMerged, } if issue.PullRequest.HasMerged { apiIssue.PullRequest.Merged = &issue.PullRequest.Merged } } } return apiIssue }
// NewIssue creates new issue with labels for repository. func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) (err error) { sess := x.NewSession() defer sessionRelease(sess) if err = sess.Begin(); err != nil { return err } if err = newIssue(sess, repo, issue, labelIDs, uuids, false); err != nil { return fmt.Errorf("newIssue: %v", err) } if err = sess.Commit(); err != nil { return fmt.Errorf("Commit: %v", err) } // Notify watchers. act := &Action{ ActUserID: issue.Poster.Id, ActUserName: issue.Poster.Name, ActEmail: issue.Poster.Email, OpType: ACTION_CREATE_ISSUE, Content: fmt.Sprintf("%d|%s", issue.Index, issue.Name), RepoID: repo.ID, RepoUserName: repo.Owner.Name, RepoName: repo.Name, IsPrivate: repo.IsPrivate, } if err = NotifyWatchers(act); err != nil { log.Error(4, "notifyWatchers: %v", err) } return nil }
func CreateUserRepo(ctx *context.APIContext, 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.Error(422, "", err) } else { if repo != nil { if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil { log.Error(4, "DeleteRepository: %v", err) } } ctx.Error(500, "CreateRepository", err) } return } ctx.JSON(201, convert.ToRepository(owner, repo, api.Permission{true, true, true})) }
// IsAdminOfRepo returns true if user has admin or higher access of repository. func (u *User) IsAdminOfRepo(repo *Repository) bool { has, err := HasAccess(u, repo, ACCESS_MODE_ADMIN) if err != nil { log.Error(3, "HasAccess: %v", err) } return has }
func (t *HookTask) MarshalJSON(v interface{}) string { p, err := json.Marshal(v) if err != nil { log.Error(3, "Marshal [%d]: %v", t.ID, err) } return string(p) }
// AddToTaskQueue adds itself to pull request test task queue. func (pr *PullRequest) AddToTaskQueue() { go PullRequestQueue.AddFunc(pr.ID, func() { pr.Status = PULL_REQUEST_STATUS_CHECKING if err := pr.UpdateCols("status"); err != nil { log.Error(5, "AddToTaskQueue.UpdateCols[%d].(add to queue): %v", pr.ID, err) } }) }
func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsEmails"] = true // Make emailaddress primary. if ctx.Query("_method") == "PRIMARY" { if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil { ctx.Handle(500, "MakeEmailPrimary", err) return } log.Trace("Email made primary: %s", ctx.User.Name) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") return } // Add Email address. emails, err := models.GetEmailAddresses(ctx.User.Id) if err != nil { ctx.Handle(500, "GetEmailAddresses", err) return } ctx.Data["Emails"] = emails if ctx.HasError() { ctx.HTML(200, SETTINGS_EMAILS) return } e := &models.EmailAddress{ UID: ctx.User.Id, Email: form.Email, IsActivated: !setting.Service.RegisterEmailConfirm, } if err := models.AddEmailAddress(e); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form) return } ctx.Handle(500, "AddEmailAddress", err) return } // Send confirmation e-mail if setting.Service.RegisterEmailConfirm { mailer.SendActivateEmailMail(ctx.Context, ctx.User, e) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", e.Email, setting.Service.ActiveCodeLives/60)) } else { ctx.Flash.Success(ctx.Tr("settings.add_email_success")) } log.Trace("Email address added: %s", e.Email) ctx.Redirect(setting.AppSubUrl + "/user/settings/email") }
func (a *Action) GetIssueContent() string { index := com.StrTo(a.GetIssueInfos()[0]).MustInt64() issue, err := GetIssueByIndex(a.RepoID, index) if err != nil { log.Error(4, "GetIssueByIndex: %v", err) return "500 when get issue" } return issue.Content }
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 + "/") } }
// TestPullRequests checks and tests untested patches of pull requests. // TODO: test more pull requests at same time. func TestPullRequests() { prs := make([]*PullRequest, 0, 10) x.Iterate(PullRequest{ Status: PULL_REQUEST_STATUS_CHECKING, }, func(idx int, bean interface{}) error { pr := bean.(*PullRequest) if err := pr.GetBaseRepo(); err != nil { log.Error(3, "GetBaseRepo: %v", err) return nil } if err := pr.testPatch(); err != nil { log.Error(3, "testPatch: %v", err) return nil } prs = append(prs, pr) return nil }) // Update pull request status. for _, pr := range prs { pr.checkAndUpdateStatus() } // Start listening on new test requests. for prID := range PullRequestQueue.Queue() { log.Trace("TestPullRequests[%v]: processing test task", prID) PullRequestQueue.Remove(prID) pr, err := GetPullRequestByID(com.StrTo(prID).MustInt64()) if err != nil { log.Error(4, "GetPullRequestByID[%d]: %v", prID, err) continue } else if err = pr.testPatch(); err != nil { log.Error(4, "testPatch[%d]: %v", pr.ID, err) continue } pr.checkAndUpdateStatus() } }
// AddTestPullRequestTask adds new test tasks by given head/base repository and head/base branch, // and generate new patch for testing as needed. func AddTestPullRequestTask(repoID int64, branch string) { log.Trace("AddTestPullRequestTask[head_repo_id: %d, head_branch: %s]: finding pull requests", repoID, branch) prs, err := GetUnmergedPullRequestsByHeadInfo(repoID, branch) if err != nil { log.Error(4, "Find pull requests[head_repo_id: %d, head_branch: %s]: %v", repoID, branch, err) return } addHeadRepoTasks(prs) log.Trace("AddTestPullRequestTask[base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch) prs, err = GetUnmergedPullRequestsByBaseInfo(repoID, branch) if err != nil { log.Error(4, "Find pull requests[base_repo_id: %d, base_branch: %s]: %v", repoID, branch, err) return } for _, pr := range prs { pr.AddToTaskQueue() } }
// SendRegisterNotifyMail triggers a notify e-mail by admin created a account. func SendRegisterNotifyMail(c *macaron.Context, u *models.User) { body, err := c.HTMLString(string(AUTH_REGISTER_NOTIFY), ComposeTplData(u)) if err != nil { log.Error(4, "HTMLString: %v", err) return } msg := NewMessage([]string{u.Email}, c.Tr("mail.register_notify"), body) msg.Info = fmt.Sprintf("UID: %d, registration notify", u.Id) SendAsync(msg) }
func (i *Issue) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { case "id": i.Attachments, err = GetAttachmentsByIssueID(i.ID) if err != nil { log.Error(3, "GetAttachmentsByIssueID[%d]: %v", i.ID, err) } i.Comments, err = GetCommentsByIssueID(i.ID) if err != nil { log.Error(3, "GetCommentsByIssueID[%d]: %v", i.ID, err) } i.Labels, err = GetLabelsByIssueID(i.ID) if err != nil { log.Error(3, "GetLabelsByIssueID[%d]: %v", i.ID, err) } case "poster_id": i.Poster, err = GetUserByID(i.PosterID) if err != nil { if IsErrUserNotExist(err) { i.PosterID = -1 i.Poster = NewFakeUser() } else { log.Error(3, "GetUserByID[%d]: %v", i.ID, err) } return } case "milestone_id": if i.MilestoneID == 0 { return } i.Milestone, err = GetMilestoneByID(i.MilestoneID) if err != nil { log.Error(3, "GetMilestoneById[%d]: %v", i.ID, err) } case "assignee_id": if i.AssigneeID == 0 { return } i.Assignee, err = GetUserByID(i.AssigneeID) if err != nil { log.Error(3, "GetUserByID[%d]: %v", i.ID, err) } case "deadline_unix": i.Deadline = time.Unix(i.DeadlineUnix, 0).Local() case "created_unix": i.Created = time.Unix(i.CreatedUnix, 0).Local() case "updated_unix": i.Updated = time.Unix(i.UpdatedUnix, 0).Local() } }
// checkAndUpdateStatus checks if pull request is possible to levaing checking status, // and set to be either conflict or mergeable. func (pr *PullRequest) checkAndUpdateStatus() { // Status is not changed to conflict means mergeable. if pr.Status == PULL_REQUEST_STATUS_CHECKING { pr.Status = PULL_REQUEST_STATUS_MERGEABLE } // Make sure there is no waiting test to process before levaing the checking status. if !PullRequestQueue.Exist(pr.ID) { if err := pr.UpdateCols("status"); err != nil { log.Error(4, "Update[%d]: %v", pr.ID, err) } } }
func Activate(ctx *context.Context) { code := ctx.Query("code") if len(code) == 0 { ctx.Data["IsActivatePage"] = true if ctx.User.IsActive { ctx.Error(404) return } // Resend confirmation e-mail. if setting.Service.RegisterEmailConfirm { if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) { ctx.Data["ResendLimited"] = true } else { ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 mailer.SendActivateAccountMail(ctx.Context, ctx.User) if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil { log.Error(4, "Set cache(MailResendLimit) fail: %v", err) } } } else { ctx.Data["ServiceNotEnabled"] = true } ctx.HTML(200, ACTIVATE) return } // Verify code. if user := models.VerifyUserActiveCode(code); user != nil { user.IsActive = true user.Rands = models.GetUserSalt() if err := models.UpdateUser(user); err != nil { if models.IsErrUserNotExist(err) { ctx.Error(404) } else { ctx.Handle(500, "UpdateUser", err) } return } log.Trace("User activated: %s", user.Name) ctx.Session.Set("uid", user.Id) ctx.Session.Set("uname", user.Name) ctx.Redirect(setting.AppSubUrl + "/") return } ctx.Data["IsActivateFailed"] = true ctx.HTML(200, ACTIVATE) }
func (c *Comment) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { case "id": c.Attachments, err = GetAttachmentsByCommentID(c.ID) if err != nil { log.Error(3, "GetAttachmentsByCommentID[%d]: %v", c.ID, err) } case "poster_id": c.Poster, err = GetUserByID(c.PosterID) if err != nil { if IsErrUserNotExist(err) { c.PosterID = -1 c.Poster = NewFakeUser() } else { log.Error(3, "GetUserByID[%d]: %v", c.ID, err) } } case "created_unix": c.Created = time.Unix(c.CreatedUnix, 0).Local() } }
func SendUserMail(c *macaron.Context, u *models.User, tpl base.TplName, code, subject, info string) { data := ComposeTplData(u) data["Code"] = code body, err := c.HTMLString(string(tpl), data) if err != nil { log.Error(4, "HTMLString: %v", err) return } msg := NewMessage([]string{u.Email}, subject, body) msg.Info = fmt.Sprintf("UID: %d, %s", u.Id, info) SendAsync(msg) }
func (w *Webhook) AfterSet(colName string, _ xorm.Cell) { var err error switch colName { case "events": w.HookEvent = &HookEvent{} if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { log.Error(3, "Unmarshal[%d]: %v", w.ID, err) } case "created_unix": w.Created = time.Unix(w.CreatedUnix, 0).Local() case "updated_unix": w.Updated = time.Unix(w.UpdatedUnix, 0).Local() } }
// SendActivateAccountMail sends confirmation e-mail. func SendActivateEmailMail(c *macaron.Context, u *models.User, email *models.EmailAddress) { data := ComposeTplData(u) data["Code"] = u.GenerateEmailActivateCode(email.Email) data["Email"] = email.Email body, err := c.HTMLString(string(AUTH_ACTIVATE_EMAIL), data) if err != nil { log.Error(4, "HTMLString: %v", err) return } msg := NewMessage([]string{email.Email}, c.Tr("mail.activate_email"), body) msg.Info = fmt.Sprintf("UID: %d, activate email", u.Id) SendAsync(msg) }