Browse Source

Fix Collaborators cannot commit

Unknown 5 years ago
parent
commit
7b60756f2c
7 changed files with 45 additions and 36 deletions
  1. 1 1
      cmd/serve.go
  2. 3 3
      cmd/update.go
  3. 1 1
      gogs.go
  4. 24 21
      models/action.go
  5. 3 2
      models/repo.go
  6. 10 5
      models/update.go
  7. 3 3
      routers/repo/http.go

+ 1 - 1
cmd/serve.go

@@ -175,7 +175,7 @@ func runServ(k *cli.Context) {
175 175
 		qlog.Fatal("Unknown command")
176 176
 	}
177 177
 
178
-	models.SetRepoEnvs(user.Id, user.Name, repoName)
178
+	models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName)
179 179
 
180 180
 	gitcmd := exec.Command(verb, repoPath)
181 181
 	gitcmd.Dir = base.RepoRootPath

+ 3 - 3
cmd/update.go

@@ -76,10 +76,10 @@ func runUpdate(c *cli.Context) {
76 76
 	//updateEnv(args[0], args[1], args[2])
77 77
 
78 78
 	userName := os.Getenv("userName")
79
-	userId := os.Getenv("userId")
80
-	iUserId, _ := strconv.ParseInt(userId, 10, 64)
79
+	userId, _ := strconv.ParseInt(os.Getenv("userId"), 10, 64)
81 80
 	//repoId := os.Getenv("repoId")
81
+	repoUserName := os.Getenv("repoUserName")
82 82
 	repoName := os.Getenv("repoName")
83 83
 
84
-	models.Update(args[0], args[1], args[2], userName, repoName, iUserId)
84
+	models.Update(args[0], args[1], args[2], userName, repoUserName, repoName, userId)
85 85
 }

+ 1 - 1
gogs.go

@@ -20,7 +20,7 @@ import (
20 20
 // Test that go1.2 tag above is included in builds. main.go refers to this definition.
21 21
 const go12tag = true
22 22
 
23
-const APP_VER = "0.3.2.0502 Alpha"
23
+const APP_VER = "0.3.2.0503 Alpha"
24 24
 
25 25
 func init() {
26 26
 	base.AppVer = APP_VER

+ 24 - 21
models/action.go

@@ -10,6 +10,7 @@ import (
10 10
 	"time"
11 11
 
12 12
 	"github.com/gogits/git"
13
+	qlog "github.com/qiniu/log"
13 14
 
14 15
 	"github.com/gogits/gogs/modules/base"
15 16
 	"github.com/gogits/gogs/modules/log"
@@ -31,18 +32,19 @@ const (
31 32
 // Action represents user operation type and other information to repository.,
32 33
 // it implemented interface base.Actioner so that can be used in template render.
33 34
 type Action struct {
34
-	Id          int64
35
-	UserId      int64  // Receiver user id.
36
-	OpType      int    // Operations: CREATE DELETE STAR ...
37
-	ActUserId   int64  // Action user id.
38
-	ActUserName string // Action user name.
39
-	ActEmail    string
40
-	RepoId      int64
41
-	RepoName    string
42
-	RefName     string
43
-	IsPrivate   bool      `xorm:"NOT NULL DEFAULT false"`
44
-	Content     string    `xorm:"TEXT"`
45
-	Created     time.Time `xorm:"created"`
35
+	Id           int64
36
+	UserId       int64  // Receiver user id.
37
+	OpType       int    // Operations: CREATE DELETE STAR ...
38
+	ActUserId    int64  // Action user id.
39
+	ActUserName  string // Action user name.
40
+	ActEmail     string
41
+	RepoId       int64
42
+	RepoUserName string
43
+	RepoName     string
44
+	RefName      string
45
+	IsPrivate    bool      `xorm:"NOT NULL DEFAULT false"`
46
+	Content      string    `xorm:"TEXT"`
47
+	Created      time.Time `xorm:"created"`
46 48
 }
47 49
 
48 50
 func (a Action) GetOpType() int {
@@ -70,8 +72,8 @@ func (a Action) GetContent() string {
70 72
 }
71 73
 
72 74
 // CommitRepoAction adds new action for committing repository.
73
-func CommitRepoAction(userId int64, userName, actEmail string,
74
-	repoId int64, repoName string, refName string, commit *base.PushCommits) error {
75
+func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
76
+	repoId int64, repoUserName, repoName string, refName string, commit *base.PushCommits) error {
75 77
 	// log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
76 78
 
77 79
 	opType := OP_COMMIT_REPO
@@ -85,30 +87,31 @@ func CommitRepoAction(userId int64, userName, actEmail string,
85 87
 
86 88
 	bs, err := json.Marshal(commit)
87 89
 	if err != nil {
88
-		log.Error("action.CommitRepoAction(json): %d/%s", userId, repoName)
90
+		qlog.Error("action.CommitRepoAction(json): %d/%s", repoUserId, repoName)
89 91
 		return err
90 92
 	}
91 93
 
92 94
 	// Change repository bare status and update last updated time.
93
-	repo, err := GetRepositoryByName(userId, repoName)
95
+	repo, err := GetRepositoryByName(repoUserId, repoName)
94 96
 	if err != nil {
95
-		log.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", userId, repoName)
97
+		qlog.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", repoUserId, repoName)
96 98
 		return err
97 99
 	}
98 100
 	repo.IsBare = false
99 101
 	if err = UpdateRepository(repo); err != nil {
100
-		log.Error("action.CommitRepoAction(UpdateRepository): %d/%s", userId, repoName)
102
+		qlog.Error("action.CommitRepoAction(UpdateRepository): %d/%s", repoUserId, repoName)
101 103
 		return err
102 104
 	}
103 105
 
104 106
 	if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
105
-		OpType: opType, Content: string(bs), RepoId: repoId, RepoName: repoName, RefName: refName,
107
+		OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
108
+		RepoName: repoName, RefName: refName,
106 109
 		IsPrivate: repo.IsPrivate}); err != nil {
107
-		log.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
110
+		qlog.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
108 111
 		return err
109 112
 	}
110 113
 
111
-	log.Trace("action.CommitRepoAction(end): %d/%s", userId, repoName)
114
+	qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
112 115
 	return nil
113 116
 }
114 117
 

+ 3 - 2
models/repo.go

@@ -379,10 +379,11 @@ func createHookUpdate(hookPath, content string) error {
379 379
 }
380 380
 
381 381
 // SetRepoEnvs sets environment variables for command update.
382
-func SetRepoEnvs(userId int64, userName, repoName string) {
382
+func SetRepoEnvs(userId int64, userName, repoName, repoUserName string) {
383 383
 	os.Setenv("userId", base.ToStr(userId))
384 384
 	os.Setenv("userName", userName)
385 385
 	os.Setenv("repoName", repoName)
386
+	os.Setenv("repoUserName", repoUserName)
386 387
 }
387 388
 
388 389
 // InitRepository initializes README and .gitignore if needed.
@@ -459,7 +460,7 @@ func initRepository(f string, user *User, repo *Repository, initReadme bool, rep
459 460
 		return nil
460 461
 	}
461 462
 
462
-	SetRepoEnvs(user.Id, user.Name, repo.Name)
463
+	SetRepoEnvs(user.Id, user.Name, repo.Name, user.Name)
463 464
 
464 465
 	// Apply changes and commit.
465 466
 	return initRepoCommit(tmpDir, user.NewGitSig())

+ 10 - 5
models/update.go

@@ -16,14 +16,14 @@ import (
16 16
 	"github.com/gogits/gogs/modules/base"
17 17
 )
18 18
 
19
-func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId int64) {
19
+func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName string, userId int64) {
20 20
 	isNew := strings.HasPrefix(oldCommitId, "0000000")
21 21
 	if isNew &&
22 22
 		strings.HasPrefix(newCommitId, "0000000") {
23 23
 		qlog.Fatal("old rev and new rev both 000000")
24 24
 	}
25 25
 
26
-	f := RepoPath(userName, repoName)
26
+	f := RepoPath(repoUserName, repoName)
27 27
 
28 28
 	gitUpdate := exec.Command("git", "update-server-info")
29 29
 	gitUpdate.Dir = f
@@ -59,7 +59,12 @@ func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId
59 59
 		qlog.Fatalf("runUpdate.Commit repoId: %v", err)
60 60
 	}
61 61
 
62
-	repos, err := GetRepositoryByName(userId, repoName)
62
+	ru, err := GetUserByName(repoUserName)
63
+	if err != nil {
64
+		qlog.Fatalf("runUpdate.GetUserByName: %v", err)
65
+	}
66
+
67
+	repos, err := GetRepositoryByName(ru.Id, repoName)
63 68
 	if err != nil {
64 69
 		qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err)
65 70
 	}
@@ -83,8 +88,8 @@ func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId
83 88
 	}
84 89
 
85 90
 	//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
86
-	if err = CommitRepoAction(userId, userName, actEmail,
87
-		repos.Id, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
91
+	if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
92
+		repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
88 93
 		qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
89 94
 	}
90 95
 }

+ 3 - 3
routers/repo/http.go

@@ -60,8 +60,8 @@ func Http(ctx *middleware.Context, params martini.Params) {
60 60
 	// only public pull don't need auth
61 61
 	isPublicPull := !repo.IsPrivate && isPull
62 62
 	var askAuth = !isPublicPull || base.Service.RequireSignInView
63
-
64 63
 	var authUser *models.User
64
+	var authUsername, passwd string
65 65
 
66 66
 	// check access
67 67
 	if askAuth {
@@ -79,7 +79,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
79 79
 			ctx.Handle(401, "no basic auth and digit auth", nil)
80 80
 			return
81 81
 		}
82
-		authUsername, passwd, err := basicDecode(auths[1])
82
+		authUsername, passwd, err = basicDecode(auths[1])
83 83
 		if err != nil {
84 84
 			ctx.Handle(401, "no basic auth and digit auth", nil)
85 85
 			return
@@ -133,7 +133,7 @@ func Http(ctx *middleware.Context, params martini.Params) {
133 133
 					newCommitId := fields[1]
134 134
 					refName := fields[2]
135 135
 
136
-					models.Update(refName, oldCommitId, newCommitId, username, reponame, authUser.Id)
136
+					models.Update(refName, oldCommitId, newCommitId, authUsername, username, reponame, authUser.Id)
137 137
 				}
138 138
 			}
139 139
 		}