Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit: fmt gitee.go #38

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions auth/gitee.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var contentType = "Content-Type"
var verifyLog = "verifyUser"
var appendPathAccessToken = "?access_token="

const formatLogString = "%s | %s"

type giteeUser struct {
Permission string `json:"permission"`
}
Expand Down Expand Up @@ -102,14 +104,15 @@ func GiteeAuth() func(UserInRepo) error {
// CheckRepoOwner checks whether the owner of a repo is allowed to use lfs server
func CheckRepoOwner(userInRepo UserInRepo) error {
path := fmt.Sprintf(
"https://gitee.com/api/v5/repos/%s/%s",
"https://gitee.com/api/v5/repos/%s/%s%s",
userInRepo.Owner,
userInRepo.Repo,
appendPathAccessToken,
)
if userInRepo.Token != "" {
path += fmt.Sprintf(appendPathAccessToken + userInRepo.Token)
path += userInRepo.Token
} else {
path += fmt.Sprintf(appendPathAccessToken + defaultToken)
path += defaultToken
}
headers := http.Header{contentType: []string{"application/json;charset=UTF-8"}}
repo := new(Repo)
Expand Down Expand Up @@ -161,22 +164,23 @@ func getToken(username, password string) (string, error) {
// verifyUser verifies user permission in repo by access_token and operation
func verifyUser(userInRepo UserInRepo) error {
path := fmt.Sprintf(
"https://gitee.com/api/v5/repos/%s/%s/collaborators/%s/permission",
"https://gitee.com/api/v5/repos/%s/%s/collaborators/%s/permission%s",
userInRepo.Owner,
userInRepo.Repo,
userInRepo.Username,
appendPathAccessToken,
)
if userInRepo.Token != "" {
path += fmt.Sprintf(appendPathAccessToken + userInRepo.Token)
path += userInRepo.Token
} else {
path += fmt.Sprintf(appendPathAccessToken + defaultToken)
path += defaultToken
}
headers := http.Header{contentType: []string{"application/json;charset=UTF-8"}}
giteeUser := new(giteeUser)
err := getParsedResponse("GET", path, headers, nil, &giteeUser)
if err != nil {
msg := err.Error() + ": verify user permission failed"
logrus.Error(fmt.Sprintf(verifyLog + " | " + msg))
logrus.Error(fmt.Sprintf(formatLogString, verifyLog, msg))
return errors.New(msg)
}

Expand All @@ -186,7 +190,7 @@ func verifyUser(userInRepo UserInRepo) error {
return verifyUserDownload(giteeUser, userInRepo)
} else {
msg := "system_error: unknow operation"
logrus.Error(fmt.Sprintf(verifyLog + " | " + msg))
logrus.Error(fmt.Sprintf(formatLogString, verifyLog, msg))
return errors.New(msg)
}
}
Expand All @@ -202,7 +206,7 @@ func verifyUserUpload(giteeUser *giteeUser, userInRepo UserInRepo) error {
remindMsg := " \n如果您正在向fork仓库上传大文件,请确认您已使用如下命令修改了本地仓库的配置:" +
"\n`git config --local lfs.url https://artifacts.openeuler.openatom.cn/{owner}/{repo}`" +
",\n其中{owner}/{repo}请改为您fork之后的仓库的名称"
logrus.Error(fmt.Sprintf(verifyLog + " | " + msg))
logrus.Error(fmt.Sprintf(formatLogString, verifyLog, msg))
return errors.New(msg + remindMsg)
}

Expand All @@ -213,6 +217,6 @@ func verifyUserDownload(giteeUser *giteeUser, userInRepo UserInRepo) error {
}
}
msg := fmt.Sprintf("forbidden: user %s has no permission to download", userInRepo.Username)
logrus.Error(fmt.Sprintf(verifyLog + " | " + msg))
logrus.Error(fmt.Sprintf(formatLogString, verifyLog, msg))
return errors.New(msg)
}
1 change: 1 addition & 0 deletions auth/gitee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *SuiteGitee) TestCheckRepoOwner() {
userInRepo = UserInRepo{
Repo: "repo",
Owner: "owner",
Token: s.cfg.DefaultToken,
}
err = CheckRepoOwner(userInRepo)
assert.NotNil(s.T(), err)
Expand Down
Loading