diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/error.go | 23 | ||||
-rw-r--r-- | models/git_diff.go | 61 | ||||
-rw-r--r-- | models/org.go | 47 | ||||
-rw-r--r-- | models/pull.go | 8 | ||||
-rw-r--r-- | models/repo.go | 2 | ||||
-rw-r--r-- | models/user.go | 7 | ||||
-rw-r--r-- | models/webhook.go | 2 |
7 files changed, 104 insertions, 46 deletions
diff --git a/models/error.go b/models/error.go index 83a24e7f4e..8e2048ded9 100644 --- a/models/error.go +++ b/models/error.go @@ -559,5 +559,26 @@ func IsErrAuthenticationNotExist(err error) bool { } func (err ErrAuthenticationNotExist) Error() string { - return fmt.Sprintf("Authentication does not exist [id: %d]", err.ID) + return fmt.Sprintf("authentication does not exist [id: %d]", err.ID) +} + +// ___________ +// \__ ___/___ _____ _____ +// | |_/ __ \\__ \ / \ +// | |\ ___/ / __ \| Y Y \ +// |____| \___ >____ /__|_| / +// \/ \/ \/ + +type ErrTeamAlreadyExist struct { + OrgID int64 + Name string +} + +func IsErrTeamAlreadyExist(err error) bool { + _, ok := err.(ErrTeamAlreadyExist) + return ok +} + +func (err ErrTeamAlreadyExist) Error() string { + return fmt.Sprintf("team already exists [org_id: %d, name: %s]", err.OrgID, err.Name) } diff --git a/models/git_diff.go b/models/git_diff.go index 1913ec46ea..e8bfe61027 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -51,7 +51,6 @@ type DiffLine struct { RightIdx int Type DiffLineType Content string - ParsedContent template.HTML } func (d *DiffLine) GetType() int { @@ -112,42 +111,42 @@ func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLin return nil } -// computes diff of each diff line and set the HTML on diffLine.ParsedContent -func (diffSection *DiffSection) ComputeLinesDiff() { - for _, diffLine := range diffSection.Lines { - var compareDiffLine *DiffLine - var diff1, diff2 string +// computes inline diff for the given line +func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML { + var compareDiffLine *DiffLine + var diff1, diff2 string - diffLine.ParsedContent = template.HTML(html.EscapeString(diffLine.Content[1:])) + getDefaultReturn := func() template.HTML { + return template.HTML(html.EscapeString(diffLine.Content[1:])) + } - // just compute diff for adds and removes - if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL { - continue - } + // just compute diff for adds and removes + if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL { + return getDefaultReturn() + } - // try to find equivalent diff line. ignore, otherwise - if diffLine.Type == DIFF_LINE_ADD { - compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx) - if compareDiffLine == nil { - continue - } - diff1 = compareDiffLine.Content - diff2 = diffLine.Content - } else { - compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx) - if compareDiffLine == nil { - continue - } - diff1 = diffLine.Content - diff2 = compareDiffLine.Content + // try to find equivalent diff line. ignore, otherwise + if diffLine.Type == DIFF_LINE_ADD { + compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx) + if compareDiffLine == nil { + return getDefaultReturn() + } + diff1 = compareDiffLine.Content + diff2 = diffLine.Content + } else { + compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx) + if compareDiffLine == nil { + return getDefaultReturn() } + diff1 = diffLine.Content + diff2 = compareDiffLine.Content + } - dmp := diffmatchpatch.New() - diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true) - diffRecord = dmp.DiffCleanupSemantic(diffRecord) + dmp := diffmatchpatch.New() + diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true) + diffRecord = dmp.DiffCleanupSemantic(diffRecord) - diffLine.ParsedContent = diffToHTML(diffRecord, diffLine.Type) - } + return diffToHTML(diffRecord, diffLine.Type) } type DiffFile struct { diff --git a/models/org.go b/models/org.go index fa26b59e66..3de89e4bd3 100644 --- a/models/org.go +++ b/models/org.go @@ -14,10 +14,8 @@ import ( ) var ( - ErrOrgNotExist = errors.New("Organization does not exist") - ErrTeamAlreadyExist = errors.New("Team already exist") - ErrTeamNotExist = errors.New("Team does not exist") - ErrTeamNameIllegal = errors.New("Team name contains illegal characters") + ErrOrgNotExist = errors.New("Organization does not exist") + ErrTeamNotExist = errors.New("Team does not exist") ) // IsOwnedBy returns true if given user is in the owner team. @@ -255,6 +253,26 @@ func IsPublicMembership(orgId, uid int64) bool { return has } +func getPublicOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { + orgs := make([]*User, 0, 10) + return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_public=?", true). + Join("INNER", "`org_user`", "`org_user`.org_id=`user`.id").Find(&orgs) +} + +// GetPublicOrgsByUserID returns a list of organizations that the given user ID +// has joined publicly. +func GetPublicOrgsByUserID(userID int64) ([]*User, error) { + sess := x.NewSession() + return getPublicOrgsByUserID(sess, userID) +} + +// GetPublicOrgsByUserID returns a list of organizations that the given user ID +// has joined publicly, ordered descending by the given condition. +func GetPublicOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { + sess := x.NewSession() + return getPublicOrgsByUserID(sess.Desc(desc), userID) +} + func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) { orgs := make([]*User, 0, 10) return orgs, sess.Where("`org_user`.uid=?", userID).And("`org_user`.is_owner=?", true). @@ -268,7 +286,7 @@ func GetOwnedOrgsByUserID(userID int64) ([]*User, error) { } // GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by -// given user ID and descring order by given condition. +// given user ID, ordered descending by the given condition. func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) { sess := x.NewSession() return getOwnedOrgsByUserID(sess.Desc(desc), userID) @@ -598,9 +616,9 @@ func (t *Team) RemoveRepository(repoID int64) error { // NewTeam creates a record of new team. // It's caller's responsibility to assign organization ID. -func NewTeam(t *Team) (err error) { - if err = IsUsableName(t.Name); err != nil { - return err +func NewTeam(t *Team) error { + if len(t.Name) == 0 { + return errors.New("empty team name") } has, err := x.Id(t.OrgID).Get(new(User)) @@ -615,7 +633,7 @@ func NewTeam(t *Team) (err error) { if err != nil { return err } else if has { - return ErrTeamAlreadyExist + return ErrTeamAlreadyExist{t.OrgID, t.LowerName} } sess := x.NewSession() @@ -674,8 +692,8 @@ func GetTeamById(teamId int64) (*Team, error) { // UpdateTeam updates information of team. func UpdateTeam(t *Team, authChanged bool) (err error) { - if err = IsUsableName(t.Name); err != nil { - return err + if len(t.Name) == 0 { + return errors.New("empty team name") } if len(t.Description) > 255 { @@ -689,6 +707,13 @@ func UpdateTeam(t *Team, authChanged bool) (err error) { } t.LowerName = strings.ToLower(t.Name) + has, err := x.Where("org_id=?", t.OrgID).And("lower_name=?", t.LowerName).And("id!=?", t.ID).Get(new(Team)) + if err != nil { + return err + } else if has { + return ErrTeamAlreadyExist{t.OrgID, t.LowerName} + } + if _, err = sess.Id(t.ID).AllCols().Update(t); err != nil { return fmt.Errorf("update: %v", err) } diff --git a/models/pull.go b/models/pull.go index 8497285e16..47d80473a0 100644 --- a/models/pull.go +++ b/models/pull.go @@ -525,6 +525,14 @@ func AddTestPullRequestTask(repoID int64, branch string) { } } +func ChangeUsernameInPullRequests(oldUserName, newUserName string) error { + pr := PullRequest{ + HeadUserName: strings.ToLower(newUserName), + } + _, err := x.Cols("head_user_name").Where("head_user_name = ?", strings.ToLower(oldUserName)).Update(pr) + return err +} + // checkAndUpdateStatus checks if pull request is possible to levaing checking status, // and set to be either conflict or mergeable. func (pr *PullRequest) checkAndUpdateStatus() { diff --git a/models/repo.go b/models/repo.go index e53d806576..8ce1f7190c 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1602,7 +1602,7 @@ func GitFsck() { repo := bean.(*Repository) repoPath := repo.RepoPath() if err := git.Fsck(repoPath, setting.Cron.RepoHealthCheck.Timeout, setting.Cron.RepoHealthCheck.Args...); err != nil { - desc := fmt.Sprintf("Fail to health check repository(%s)", repoPath) + desc := fmt.Sprintf("Fail to health check repository (%s): %v", repoPath, err) log.Warn(desc) if err = CreateRepositoryNotice(desc); err != nil { log.Error(4, "CreateRepositoryNotice: %v", err) diff --git a/models/user.go b/models/user.go index 5c43a23a2f..31ac1e22dc 100644 --- a/models/user.go +++ b/models/user.go @@ -599,7 +599,12 @@ func ChangeUserName(u *User, newUserName string) (err error) { return ErrUserAlreadyExist{newUserName} } - return os.Rename(UserPath(u.LowerName), UserPath(newUserName)) + err = ChangeUsernameInPullRequests(u.Name, newUserName) + if err != nil { + return fmt.Errorf("ChangeUsernameInPullRequests: %v", err) + } + + return os.Rename(UserPath(u.Name), UserPath(newUserName)) } func updateUser(e Engine, u *User) error { diff --git a/models/webhook.go b/models/webhook.go index f58cfbf56f..27ac75fe07 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -285,7 +285,7 @@ type HookTask struct { HookID int64 UUID string Type HookTaskType - URL string + URL string `xorm:"TEXT"` api.Payloader `xorm:"-"` PayloadContent string `xorm:"TEXT"` ContentType HookContentType |