2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-07-25 10:49:39 +02:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2021-10-03 14:42:47 +02:00
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
2015-07-25 10:49:39 +02:00
|
|
|
|
2021-10-03 14:42:47 +02:00
|
|
|
"github.com/xanzy/go-gitlab"
|
2015-07-25 10:49:39 +02:00
|
|
|
)
|
|
|
|
|
2016-02-01 01:49:52 +02:00
|
|
|
const (
|
|
|
|
gravatarBase = "https://www.gravatar.com/avatar"
|
|
|
|
)
|
|
|
|
|
2021-10-03 14:42:47 +02:00
|
|
|
// newClient is a helper function that returns a new GitHub
|
2015-07-25 10:49:39 +02:00
|
|
|
// client using the provided OAuth token.
|
2021-10-03 14:42:47 +02:00
|
|
|
func newClient(url, accessToken string, skipVerify bool) (*gitlab.Client, error) {
|
|
|
|
return gitlab.NewOAuthClient(accessToken, gitlab.WithBaseURL(url), gitlab.WithHTTPClient(&http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
},
|
|
|
|
}))
|
2015-07-25 10:49:39 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 14:42:47 +02:00
|
|
|
// isRead is a helper function that returns true if the
|
2015-07-25 10:49:39 +02:00
|
|
|
// user has Read-only access to the repository.
|
2021-10-03 14:42:47 +02:00
|
|
|
func isRead(proj *gitlab.Project) bool {
|
2022-01-05 22:50:23 +02:00
|
|
|
user := proj.Permissions.ProjectAccess
|
|
|
|
group := proj.Permissions.GroupAccess
|
2015-07-25 10:49:39 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case proj.Public:
|
|
|
|
return true
|
|
|
|
case user != nil && user.AccessLevel >= 20:
|
|
|
|
return true
|
|
|
|
case group != nil && group.AccessLevel >= 20:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 14:42:47 +02:00
|
|
|
// isWrite is a helper function that returns true if the
|
2015-07-25 10:49:39 +02:00
|
|
|
// user has Read-Write access to the repository.
|
2021-10-03 14:42:47 +02:00
|
|
|
func isWrite(proj *gitlab.Project) bool {
|
2022-01-05 22:50:23 +02:00
|
|
|
user := proj.Permissions.ProjectAccess
|
|
|
|
group := proj.Permissions.GroupAccess
|
2015-07-25 10:49:39 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case user != nil && user.AccessLevel >= 30:
|
|
|
|
return true
|
|
|
|
case group != nil && group.AccessLevel >= 30:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-03 14:42:47 +02:00
|
|
|
// isAdmin is a helper function that returns true if the
|
2015-07-25 10:49:39 +02:00
|
|
|
// user has Admin access to the repository.
|
2021-10-03 14:42:47 +02:00
|
|
|
func isAdmin(proj *gitlab.Project) bool {
|
2022-01-05 22:50:23 +02:00
|
|
|
user := proj.Permissions.ProjectAccess
|
|
|
|
group := proj.Permissions.GroupAccess
|
2015-07-25 10:49:39 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case user != nil && user.AccessLevel >= 40:
|
|
|
|
return true
|
|
|
|
case group != nil && group.AccessLevel >= 40:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|