Fix collaboration migration code

This commit is contained in:
Peter Smit 2015-02-04 15:47:40 +02:00
parent 76f8904718
commit bef38d9d3f

View File

@ -90,7 +90,7 @@ func accessToCollaboration(x *xorm.Engine) error {
x.Sync(new(Collaboration))
sql := `SELECT u.id AS uid, a.repo_name AS repo, a.mode AS mode FROM access a JOIN user u ON a.user_name=u.lower_name`
sql := `SELECT u.id AS uid, a.repo_name AS repo, a.mode AS mode, a.created as created FROM access a JOIN user u ON a.user_name=u.lower_name`
results, err := x.Query(sql)
if err != nil {
return err
@ -100,12 +100,14 @@ func accessToCollaboration(x *xorm.Engine) error {
userID := mustParseInt64(result["uid"])
repoRefName := string(result["repo"])
mode := mustParseInt64(result["mode"])
created := result["created"]
//Collaborators must have write access
if mode < 2 {
continue
}
// find owner of repository
parts := strings.SplitN(repoRefName, "/", 2)
ownerName := parts[0]
repoName := parts[1]
@ -118,15 +120,24 @@ func accessToCollaboration(x *xorm.Engine) error {
if len(results) < 1 {
continue
}
ownerID := mustParseInt64(results[0]["uid"])
ownerID := mustParseInt64(results[0]["uid"])
if ownerID == userID {
continue
}
// test if user is member of owning organization
isMember := false
for _, member := range results {
memberID := mustParseInt64(member["memberid"])
// We can skip all cases that a user is member of the owning organization
if memberID == userID {
continue
isMember = true
}
}
if isMember {
continue
}
sql = `SELECT id FROM repository WHERE owner_id=? AND lower_name=?`
results, err = x.Query(sql, ownerID, repoName)
@ -139,8 +150,8 @@ func accessToCollaboration(x *xorm.Engine) error {
repoID := results[0]["id"]
sql = `INSERT INTO collaboration (user_id, repo_id) VALUES (?,?)`
_, err = x.Exec(sql, userID, repoID)
sql = `INSERT INTO collaboration (user_id, repo_id, created) VALUES (?,?,?)`
_, err = x.Exec(sql, userID, repoID, created)
if err != nil {
return err
}