mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-30 19:08:37 +00:00 
			
		
		
		
	Add context.Context to more methods (#21546)
				
					
				
			This PR adds a context parameter to a bunch of methods. Some helper `xxxCtx()` methods got replaced with the normal name now. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		| @@ -5,6 +5,7 @@ | ||||
| package git | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"regexp" | ||||
| 	"strings" | ||||
|  | ||||
| @@ -69,13 +70,13 @@ func UpdateProtectedTag(pt *ProtectedTag) error { | ||||
| } | ||||
|  | ||||
| // DeleteProtectedTag deletes a protected tag by ID | ||||
| func DeleteProtectedTag(pt *ProtectedTag) error { | ||||
| 	_, err := db.GetEngine(db.DefaultContext).ID(pt.ID).Delete(&ProtectedTag{}) | ||||
| func DeleteProtectedTag(ctx context.Context, pt *ProtectedTag) error { | ||||
| 	_, err := db.GetEngine(ctx).ID(pt.ID).Delete(&ProtectedTag{}) | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| // IsUserAllowedModifyTag returns true if the user is allowed to modify the tag | ||||
| func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) { | ||||
| func IsUserAllowedModifyTag(ctx context.Context, pt *ProtectedTag, userID int64) (bool, error) { | ||||
| 	if base.Int64sContains(pt.AllowlistUserIDs, userID) { | ||||
| 		return true, nil | ||||
| 	} | ||||
| @@ -84,7 +85,7 @@ func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) { | ||||
| 		return false, nil | ||||
| 	} | ||||
|  | ||||
| 	in, err := organization.IsUserInTeams(db.DefaultContext, userID, pt.AllowlistTeamIDs) | ||||
| 	in, err := organization.IsUserInTeams(ctx, userID, pt.AllowlistTeamIDs) | ||||
| 	if err != nil { | ||||
| 		return false, err | ||||
| 	} | ||||
| @@ -92,9 +93,9 @@ func IsUserAllowedModifyTag(pt *ProtectedTag, userID int64) (bool, error) { | ||||
| } | ||||
|  | ||||
| // GetProtectedTags gets all protected tags of the repository | ||||
| func GetProtectedTags(repoID int64) ([]*ProtectedTag, error) { | ||||
| func GetProtectedTags(ctx context.Context, repoID int64) ([]*ProtectedTag, error) { | ||||
| 	tags := make([]*ProtectedTag, 0) | ||||
| 	return tags, db.GetEngine(db.DefaultContext).Find(&tags, &ProtectedTag{RepoID: repoID}) | ||||
| 	return tags, db.GetEngine(ctx).Find(&tags, &ProtectedTag{RepoID: repoID}) | ||||
| } | ||||
|  | ||||
| // GetProtectedTagByID gets the protected tag with the specific id | ||||
| @@ -112,7 +113,7 @@ func GetProtectedTagByID(id int64) (*ProtectedTag, error) { | ||||
|  | ||||
| // IsUserAllowedToControlTag checks if a user can control the specific tag. | ||||
| // It returns true if the tag name is not protected or the user is allowed to control it. | ||||
| func IsUserAllowedToControlTag(tags []*ProtectedTag, tagName string, userID int64) (bool, error) { | ||||
| func IsUserAllowedToControlTag(ctx context.Context, tags []*ProtectedTag, tagName string, userID int64) (bool, error) { | ||||
| 	isAllowed := true | ||||
| 	for _, tag := range tags { | ||||
| 		err := tag.EnsureCompiledPattern() | ||||
| @@ -124,7 +125,7 @@ func IsUserAllowedToControlTag(tags []*ProtectedTag, tagName string, userID int6 | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		isAllowed, err = IsUserAllowedModifyTag(tag, userID) | ||||
| 		isAllowed, err = IsUserAllowedModifyTag(ctx, tag, userID) | ||||
| 		if err != nil { | ||||
| 			return false, err | ||||
| 		} | ||||
|   | ||||
| @@ -7,6 +7,7 @@ package git_test | ||||
| import ( | ||||
| 	"testing" | ||||
|  | ||||
| 	"code.gitea.io/gitea/models/db" | ||||
| 	git_model "code.gitea.io/gitea/models/git" | ||||
| 	"code.gitea.io/gitea/models/unittest" | ||||
|  | ||||
| @@ -17,29 +18,29 @@ func TestIsUserAllowed(t *testing.T) { | ||||
| 	assert.NoError(t, unittest.PrepareTestDatabase()) | ||||
|  | ||||
| 	pt := &git_model.ProtectedTag{} | ||||
| 	allowed, err := git_model.IsUserAllowedModifyTag(pt, 1) | ||||
| 	allowed, err := git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.False(t, allowed) | ||||
|  | ||||
| 	pt = &git_model.ProtectedTag{ | ||||
| 		AllowlistUserIDs: []int64{1}, | ||||
| 	} | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 1) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.True(t, allowed) | ||||
|  | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 2) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.False(t, allowed) | ||||
|  | ||||
| 	pt = &git_model.ProtectedTag{ | ||||
| 		AllowlistTeamIDs: []int64{1}, | ||||
| 	} | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 1) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.False(t, allowed) | ||||
|  | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 2) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.True(t, allowed) | ||||
|  | ||||
| @@ -47,11 +48,11 @@ func TestIsUserAllowed(t *testing.T) { | ||||
| 		AllowlistUserIDs: []int64{1}, | ||||
| 		AllowlistTeamIDs: []int64{1}, | ||||
| 	} | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 1) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 1) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.True(t, allowed) | ||||
|  | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(pt, 2) | ||||
| 	allowed, err = git_model.IsUserAllowedModifyTag(db.DefaultContext, pt, 2) | ||||
| 	assert.NoError(t, err) | ||||
| 	assert.True(t, allowed) | ||||
| } | ||||
| @@ -135,7 +136,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) { | ||||
| 		} | ||||
|  | ||||
| 		for n, c := range cases { | ||||
| 			isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid) | ||||
| 			isAllowed, err := git_model.IsUserAllowedToControlTag(db.DefaultContext, protectedTags, c.name, c.userid) | ||||
| 			assert.NoError(t, err) | ||||
| 			assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n) | ||||
| 		} | ||||
| @@ -157,7 +158,7 @@ func TestIsUserAllowedToControlTag(t *testing.T) { | ||||
| 		} | ||||
|  | ||||
| 		for n, c := range cases { | ||||
| 			isAllowed, err := git_model.IsUserAllowedToControlTag(protectedTags, c.name, c.userid) | ||||
| 			isAllowed, err := git_model.IsUserAllowedToControlTag(db.DefaultContext, protectedTags, c.name, c.userid) | ||||
| 			assert.NoError(t, err) | ||||
| 			assert.Equal(t, c.allowed, isAllowed, "case %d: error should match", n) | ||||
| 		} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user