2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00: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 08:49:39 +00:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2015-07-25 08:49:39 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2022-08-26 19:42:41 +00:00
|
|
|
"io"
|
2015-07-25 08:49:39 +00:00
|
|
|
"net/http"
|
2015-07-26 23:52:18 +00:00
|
|
|
"net/url"
|
2022-09-05 15:08:51 +00:00
|
|
|
"strconv"
|
2015-07-25 08:49:39 +00:00
|
|
|
"strings"
|
2022-08-24 13:46:11 +00:00
|
|
|
"time"
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2022-01-03 15:35:46 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/xanzy/go-gitlab"
|
2022-08-26 19:42:41 +00:00
|
|
|
"golang.org/x/oauth2"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-11-07 07:04:33 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/server"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/forge"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/forge/common"
|
|
|
|
forge_types "go.woodpecker-ci.org/woodpecker/server/forge/types"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/server/store"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/shared/utils"
|
2015-07-25 08:49:39 +00:00
|
|
|
)
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
const (
|
2021-12-28 16:02:49 +00:00
|
|
|
defaultScope = "api"
|
|
|
|
perPage = 100
|
2021-10-03 12:42:47 +00:00
|
|
|
)
|
2016-05-01 23:30:00 +00:00
|
|
|
|
|
|
|
// Opts defines configuration options.
|
|
|
|
type Opts struct {
|
2021-10-03 12:42:47 +00:00
|
|
|
URL string // Gitlab server url.
|
|
|
|
ClientID string // Oauth2 client id.
|
|
|
|
ClientSecret string // Oauth2 client secret.
|
|
|
|
SkipVerify bool // Skip ssl verification.
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Gitlab implements "Forge" interface
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
type GitLab struct {
|
2023-05-31 16:30:41 +00:00
|
|
|
url string
|
2021-10-03 12:42:47 +00:00
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
SkipVerify bool
|
|
|
|
HideArchives bool
|
|
|
|
Search bool
|
2016-05-01 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// New returns a Forge implementation that integrates with Gitlab, an open
|
2016-05-01 23:30:00 +00:00
|
|
|
// source Git service. See https://gitlab.com
|
2022-11-04 23:35:06 +00:00
|
|
|
func New(opts Opts) (forge.Forge, error) {
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
return &GitLab{
|
2023-05-31 16:30:41 +00:00
|
|
|
url: opts.URL,
|
2021-10-03 12:42:47 +00:00
|
|
|
ClientID: opts.ClientID,
|
|
|
|
ClientSecret: opts.ClientSecret,
|
|
|
|
SkipVerify: opts.SkipVerify,
|
2023-09-06 19:40:25 +00:00
|
|
|
HideArchives: true,
|
2016-05-01 23:30:00 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2022-06-17 18:14:01 +00:00
|
|
|
// Name returns the string name of this driver
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Name() string {
|
2022-06-17 18:14:01 +00:00
|
|
|
return "gitlab"
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:30:41 +00:00
|
|
|
// URL returns the root url of a configured forge
|
|
|
|
func (g *GitLab) URL() string {
|
|
|
|
return g.url
|
|
|
|
}
|
|
|
|
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) oauth2Config(ctx context.Context) (*oauth2.Config, context.Context) {
|
2022-08-24 13:46:11 +00:00
|
|
|
return &oauth2.Config{
|
2022-08-26 19:42:41 +00:00
|
|
|
ClientID: g.ClientID,
|
|
|
|
ClientSecret: g.ClientSecret,
|
|
|
|
Endpoint: oauth2.Endpoint{
|
2023-05-31 16:30:41 +00:00
|
|
|
AuthURL: fmt.Sprintf("%s/oauth/authorize", g.url),
|
|
|
|
TokenURL: fmt.Sprintf("%s/oauth/token", g.url),
|
2022-08-26 19:42:41 +00:00
|
|
|
},
|
|
|
|
Scopes: []string{defaultScope},
|
2023-09-22 14:43:31 +00:00
|
|
|
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
|
2022-08-26 19:42:41 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: g.SkipVerify},
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
}})
|
2022-08-24 13:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login authenticates the session and returns the
|
2022-11-04 23:35:06 +00:00
|
|
|
// forge user details.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Login(ctx context.Context, res http.ResponseWriter, req *http.Request) (*model.User, error) {
|
2022-08-26 19:42:41 +00:00
|
|
|
config, oauth2Ctx := g.oauth2Config(ctx)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-12-19 05:42:56 +00:00
|
|
|
// get the OAuth errors
|
|
|
|
if err := req.FormValue("error"); err != "" {
|
2022-11-06 11:44:04 +00:00
|
|
|
return nil, &forge_types.AuthError{
|
2016-12-19 16:22:11 +00:00
|
|
|
Err: err,
|
|
|
|
Description: req.FormValue("error_description"),
|
|
|
|
URI: req.FormValue("error_uri"),
|
2016-12-19 05:42:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
// get the OAuth code
|
2022-01-05 20:50:23 +00:00
|
|
|
code := req.FormValue("code")
|
2015-09-30 01:21:17 +00:00
|
|
|
if len(code) == 0 {
|
2022-08-26 19:42:41 +00:00
|
|
|
http.Redirect(res, req, config.AuthCodeURL("woodpecker"), http.StatusSeeOther)
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, nil
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 19:42:41 +00:00
|
|
|
token, err := config.Exchange(oauth2Ctx, code)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
2023-02-01 23:08:02 +00:00
|
|
|
return nil, fmt.Errorf("Error exchanging token. %w", err)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, token.AccessToken, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
login, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2016-05-01 23:30:00 +00:00
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
2016-02-14 00:38:31 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
user := &model.User{
|
2023-05-11 03:19:35 +00:00
|
|
|
Login: login.Username,
|
|
|
|
Email: login.Email,
|
|
|
|
Avatar: login.AvatarURL,
|
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(login.ID)),
|
|
|
|
Token: token.AccessToken,
|
|
|
|
Secret: token.RefreshToken,
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
if !strings.HasPrefix(user.Avatar, "http") {
|
2023-05-31 16:30:41 +00:00
|
|
|
user.Avatar = g.url + "/" + login.AvatarURL
|
2015-08-10 07:15:33 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return user, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 13:46:11 +00:00
|
|
|
// Refresh refreshes the Gitlab oauth2 access token. If the token is
|
|
|
|
// refreshed the user is updated and a true value is returned.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Refresh(ctx context.Context, user *model.User) (bool, error) {
|
2022-08-26 19:42:41 +00:00
|
|
|
config, oauth2Ctx := g.oauth2Config(ctx)
|
2022-08-24 13:46:11 +00:00
|
|
|
config.RedirectURL = ""
|
|
|
|
|
2022-08-26 19:42:41 +00:00
|
|
|
source := config.TokenSource(oauth2Ctx, &oauth2.Token{
|
|
|
|
AccessToken: user.Token,
|
|
|
|
RefreshToken: user.Secret,
|
|
|
|
Expiry: time.Unix(user.Expiry, 0),
|
|
|
|
})
|
2022-08-24 13:46:11 +00:00
|
|
|
|
2022-08-26 19:42:41 +00:00
|
|
|
token, err := source.Token()
|
|
|
|
if err != nil || len(token.AccessToken) == 0 {
|
2022-08-24 13:46:11 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:42:41 +00:00
|
|
|
user.Token = token.AccessToken
|
|
|
|
user.Secret = token.RefreshToken
|
|
|
|
user.Expiry = token.Expiry.UTC().Unix()
|
2022-08-24 13:46:11 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Auth authenticates the session and returns the forge user login for the given token
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Auth(ctx context.Context, token, _ string) (string, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, token, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
login, _, err := client.Users.CurrentUser(gitlab.WithContext(ctx))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return login.Username, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Teams fetches a list of team memberships from the forge.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Teams(ctx context.Context, user *model.User) ([]*model.Team, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2016-05-01 23:30:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
teams := make([]*model.Team, 0, perPage)
|
|
|
|
|
|
|
|
for i := 1; true; i++ {
|
|
|
|
batch, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{Page: i, PerPage: perPage},
|
2023-11-20 07:23:01 +00:00
|
|
|
AllAvailable: gitlab.Ptr(false),
|
|
|
|
MinAccessLevel: gitlab.Ptr(gitlab.DeveloperPermissions), // TODO: check what's best here
|
2021-10-03 12:42:47 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range batch {
|
|
|
|
teams = append(teams, &model.Team{
|
|
|
|
Login: batch[i].Name,
|
|
|
|
Avatar: batch[i].AvatarURL,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
}
|
2021-10-03 12:42:47 +00:00
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return teams, nil
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// getProject fetches the named repository from the forge.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) getProject(ctx context.Context, client *gitlab.Client, owner, name string) (*gitlab.Project, error) {
|
2021-10-03 12:42:47 +00:00
|
|
|
repo, _, err := client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, name), nil, gitlab.WithContext(ctx))
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-01-31 23:22:44 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return repo, nil
|
|
|
|
}
|
2016-01-31 23:22:44 +00:00
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Repo fetches the repository from the forge.
|
2022-11-15 14:01:23 +00:00
|
|
|
func (g *GitLab) Repo(ctx context.Context, user *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
if remoteID.IsValid() {
|
|
|
|
intID, err := strconv.ParseInt(string(remoteID), 10, 64)
|
2022-09-05 15:08:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_repo, _, err := client.Projects.GetProject(int(intID), nil, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
return g.convertGitLabRepo(_repo)
|
2022-09-05 15:08:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, owner, name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
2015-07-26 22:29:51 +00:00
|
|
|
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
return g.convertGitLabRepo(_repo)
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Repos fetches a list of repos from the forge.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Repos(ctx context.Context, user *model.User) ([]*model.Repo, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2021-10-03 12:42:47 +00:00
|
|
|
return nil, err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
repos := make([]*model.Repo, 0, perPage)
|
|
|
|
opts := &gitlab.ListProjectsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{PerPage: perPage},
|
2023-11-20 07:23:01 +00:00
|
|
|
MinAccessLevel: gitlab.Ptr(gitlab.DeveloperPermissions), // TODO: check what's best here
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
if g.HideArchives {
|
2023-11-20 07:23:01 +00:00
|
|
|
opts.Archived = gitlab.Ptr(false)
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
2017-07-14 19:58:38 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
for i := 1; true; i++ {
|
|
|
|
opts.Page = i
|
|
|
|
batch, _, err := client.Projects.ListProjects(opts, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-31 22:55:59 +00:00
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
for i := range batch {
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
repo, err := g.convertGitLabRepo(batch[i])
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-03 15:35:46 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
repos = append(repos, repo)
|
2017-07-14 19:58:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2016-01-31 22:55:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
func (g *GitLab) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, r, u)
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, token, g.SkipVerify)
|
2023-03-19 09:43:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, r.Owner, r.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state := "open"
|
|
|
|
pullRequests, _, err := client.MergeRequests.ListProjectMergeRequests(_repo.ID, &gitlab.ListProjectMergeRequestsOptions{
|
2023-04-30 01:40:13 +00:00
|
|
|
ListOptions: gitlab.ListOptions{Page: p.Page, PerPage: p.PerPage},
|
2023-03-19 09:43:57 +00:00
|
|
|
State: &state,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result := make([]*model.PullRequest, len(pullRequests))
|
|
|
|
for i := range pullRequests {
|
|
|
|
result[i] = &model.PullRequest{
|
2023-11-25 23:52:52 +00:00
|
|
|
Index: model.ForgeRemoteID(strconv.Itoa(pullRequests[i].ID)),
|
2023-03-19 09:43:57 +00:00
|
|
|
Title: pullRequests[i].Title,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// File fetches a file from the forge repository and returns in string format.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) File(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, fileName string) ([]byte, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2017-03-18 11:25:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
file, _, err := client.RepositoryFiles.GetRawFile(_repo.ID, fileName, &gitlab.GetRawFileOptions{Ref: &pipeline.Commit}, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
return file, err
|
2017-03-18 11:25:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
// Dir fetches a folder from the forge repository
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Dir(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, path string) ([]*forge_types.FileMeta, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
files := make([]*forge_types.FileMeta, 0, perPage)
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts := &gitlab.ListTreeOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{PerPage: perPage},
|
|
|
|
Path: &path,
|
2022-10-18 01:24:12 +00:00
|
|
|
Ref: &pipeline.Commit,
|
2023-11-20 07:23:01 +00:00
|
|
|
Recursive: gitlab.Ptr(false),
|
2021-10-03 12:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; true; i++ {
|
|
|
|
opts.Page = 1
|
2021-12-01 13:22:06 +00:00
|
|
|
batch, _, err := client.Repositories.ListTree(_repo.ID, opts, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range batch {
|
|
|
|
if batch[i].Type != "blob" { // no file
|
|
|
|
continue
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
data, err := g.File(ctx, user, repo, pipeline, batch[i].Path)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-06 11:44:04 +00:00
|
|
|
files = append(files, &forge_types.FileMeta{
|
2021-10-03 12:42:47 +00:00
|
|
|
Name: batch[i].Path,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(batch) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 07:16:15 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return files, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Status sends the commit status back to gitlab.
|
2023-06-27 16:01:18 +00:00
|
|
|
func (g *GitLab) Status(ctx context.Context, user *model.User, repo *model.Repo, pipeline *model.Pipeline, workflow *model.Workflow) error {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
_, _, err = client.Commits.SetCommitStatus(_repo.ID, pipeline.Commit, &gitlab.SetCommitStatusOptions{
|
2023-06-27 16:01:18 +00:00
|
|
|
State: getStatus(workflow.State),
|
2023-11-20 07:23:01 +00:00
|
|
|
Description: gitlab.Ptr(common.GetPipelineStatusDescription(workflow.State)),
|
|
|
|
TargetURL: gitlab.Ptr(common.GetPipelineStatusURL(repo, pipeline, workflow)),
|
|
|
|
Context: gitlab.Ptr(common.GetPipelineStatusContext(repo, pipeline, workflow)),
|
2021-10-03 12:42:47 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2016-05-01 23:30:00 +00:00
|
|
|
|
|
|
|
// Netrc returns a netrc file capable of authenticating Gitlab requests and
|
|
|
|
// cloning Gitlab repositories. The netrc will use the global machine account
|
|
|
|
// when configured.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
|
2022-01-31 14:38:00 +00:00
|
|
|
login := ""
|
|
|
|
token := ""
|
|
|
|
|
|
|
|
if u != nil {
|
|
|
|
login = "oauth2"
|
|
|
|
token = u.Token
|
2015-09-02 03:42:18 +00:00
|
|
|
}
|
2022-01-31 14:38:00 +00:00
|
|
|
|
2022-02-26 01:54:15 +00:00
|
|
|
host, err := common.ExtractHostFromCloneURL(r.Clone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-01 23:30:00 +00:00
|
|
|
return &model.Netrc{
|
2022-01-31 14:38:00 +00:00
|
|
|
Login: login,
|
|
|
|
Password: token,
|
2022-02-26 01:54:15 +00:00
|
|
|
Machine: host,
|
2016-05-01 23:30:00 +00:00
|
|
|
}, nil
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) getTokenAndWebURL(link string) (token, webURL string, err error) {
|
2021-12-19 00:12:09 +00:00
|
|
|
uri, err := url.Parse(link)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
token = uri.Query().Get("access_token")
|
2023-10-07 14:41:25 +00:00
|
|
|
webURL = fmt.Sprintf("%s://%s/%s", uri.Scheme, uri.Host, strings.TrimPrefix(uri.Path, "/"))
|
2021-12-19 00:12:09 +00:00
|
|
|
return token, webURL, nil
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:49:39 +00:00
|
|
|
// Activate activates a repository by adding a Post-commit hook and
|
|
|
|
// a Public Deploy key, if applicable.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Activate(ctx context.Context, user *model.User, repo *model.Repo, link string) error {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2015-07-25 08:49:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-19 00:12:09 +00:00
|
|
|
token, webURL, err := g.getTokenAndWebURL(link)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
if len(token) == 0 {
|
|
|
|
return fmt.Errorf("no token found")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = client.Projects.AddProjectHook(_repo.ID, &gitlab.AddProjectHookOptions{
|
2023-11-20 07:23:01 +00:00
|
|
|
URL: gitlab.Ptr(webURL),
|
|
|
|
Token: gitlab.Ptr(token),
|
|
|
|
PushEvents: gitlab.Ptr(true),
|
|
|
|
TagPushEvents: gitlab.Ptr(true),
|
|
|
|
MergeRequestsEvents: gitlab.Ptr(true),
|
|
|
|
DeploymentEvents: gitlab.Ptr(true),
|
|
|
|
EnableSSLVerification: gitlab.Ptr(!g.SkipVerify),
|
2021-10-03 12:42:47 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
2021-12-19 00:12:09 +00:00
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
return err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deactivate removes a repository by removing all the post-commit hooks
|
|
|
|
// which are equal to link and removing the SSH deploy key.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Deactivate(ctx context.Context, user *model.User, repo *model.Repo, link string) error {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2015-07-30 00:46:22 +00:00
|
|
|
if err != nil {
|
2015-07-28 06:38:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-25 08:49:39 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-19 00:12:09 +00:00
|
|
|
|
|
|
|
_, webURL, err := g.getTokenAndWebURL(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hookID := -1
|
|
|
|
listProjectHooksOptions := &gitlab.ListProjectHooksOptions{
|
|
|
|
PerPage: 10,
|
|
|
|
Page: 1,
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
hooks, resp, err := client.Projects.ListProjectHooks(_repo.ID, listProjectHooksOptions, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hook := range hooks {
|
|
|
|
if hook.URL == webURL {
|
|
|
|
hookID = hook.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit the loop when we've seen all pages
|
|
|
|
if resp.CurrentPage >= resp.TotalPages {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the page number to get the next page
|
|
|
|
listProjectHooksOptions.Page = resp.NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
if hookID == -1 {
|
|
|
|
return fmt.Errorf("could not find hook to delete")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = client.Projects.DeleteProjectHook(_repo.ID, hookID, gitlab.WithContext(ctx))
|
2021-10-03 12:42:47 +00:00
|
|
|
|
|
|
|
return err
|
2015-07-25 08:49:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 00:47:55 +00:00
|
|
|
// Branches returns the names of all branches for the named repository.
|
2023-04-30 01:40:13 +00:00
|
|
|
func (g *GitLab) Branches(ctx context.Context, user *model.User, repo *model.Repo, p *model.ListOptions) ([]string, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, repo, user)
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, token, g.SkipVerify)
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
gitlabBranches, _, err := client.Branches.ListBranches(_repo.ID,
|
|
|
|
&gitlab.ListBranchesOptions{ListOptions: gitlab.ListOptions{Page: p.Page, PerPage: p.PerPage}},
|
|
|
|
gitlab.WithContext(ctx))
|
2021-10-27 00:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
branches := make([]string, 0)
|
|
|
|
for _, branch := range gitlabBranches {
|
|
|
|
branches = append(branches, branch.Name)
|
|
|
|
}
|
|
|
|
return branches, nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 07:12:17 +00:00
|
|
|
// BranchHead returns the sha of the head (latest commit) of the specified branch
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (string, error) {
|
2023-06-03 01:03:06 +00:00
|
|
|
token := common.UserToken(ctx, r, u)
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, token, g.SkipVerify)
|
2022-08-31 22:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, r.Owner, r.Name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, _, err := client.Branches.GetBranch(_repo.ID, branch, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Commit.ID, nil
|
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
// Hook parses the post-commit hook from the Request body
|
2015-07-25 08:49:39 +00:00
|
|
|
// and returns the required data in a standard format.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) Hook(ctx context.Context, req *http.Request) (*model.Repo, *model.Pipeline, error) {
|
2015-07-25 23:22:16 +00:00
|
|
|
defer req.Body.Close()
|
2022-08-26 19:42:41 +00:00
|
|
|
payload, err := io.ReadAll(req.Body)
|
2015-07-25 23:22:16 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
return nil, nil, err
|
2015-07-25 23:22:16 +00:00
|
|
|
}
|
|
|
|
|
2023-07-14 00:03:54 +00:00
|
|
|
eventType := gitlab.WebhookEventType(req)
|
|
|
|
parsed, err := gitlab.ParseWebhook(eventType, payload)
|
2021-10-03 12:42:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2015-12-12 17:09:59 +00:00
|
|
|
}
|
|
|
|
|
2021-10-03 12:42:47 +00:00
|
|
|
switch event := parsed.(type) {
|
|
|
|
case *gitlab.MergeEvent:
|
2022-10-18 01:24:12 +00:00
|
|
|
mergeIID, repo, pipeline, err := convertMergeRequestHook(event, req)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
if pipeline, err = g.loadChangedFilesFromMergeRequest(ctx, repo, pipeline, mergeIID); err != nil {
|
2022-01-17 22:46:59 +00:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return repo, pipeline, nil
|
2021-10-03 12:42:47 +00:00
|
|
|
case *gitlab.PushEvent:
|
2022-01-17 22:46:59 +00:00
|
|
|
return convertPushHook(event)
|
2021-10-03 12:42:47 +00:00
|
|
|
case *gitlab.TagEvent:
|
2022-01-17 22:46:59 +00:00
|
|
|
return convertTagHook(event)
|
2015-12-12 17:09:59 +00:00
|
|
|
default:
|
2023-09-01 15:02:21 +00:00
|
|
|
return nil, nil, &forge_types.ErrIgnoreEvent{Event: string(eventType)}
|
2015-12-12 17:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2022-07-25 01:09:35 +00:00
|
|
|
// OrgMembership returns if user is member of organization and if user
|
|
|
|
// is admin/owner in this organization.
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) OrgMembership(ctx context.Context, u *model.User, owner string) (*model.OrgPerm, error) {
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, u.Token, g.SkipVerify)
|
2022-07-25 01:09:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 100,
|
|
|
|
},
|
2023-11-20 07:23:01 +00:00
|
|
|
Search: gitlab.Ptr(owner),
|
2022-07-25 01:09:35 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var gid int
|
|
|
|
for _, group := range groups {
|
|
|
|
if group.Name == owner {
|
|
|
|
gid = group.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if gid == 0 {
|
|
|
|
return &model.OrgPerm{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &gitlab.ListGroupMembersOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; true; i++ {
|
|
|
|
opts.Page = i
|
|
|
|
members, _, err := client.Groups.ListAllGroupMembers(gid, opts, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, member := range members {
|
|
|
|
if member.Username == u.Login {
|
|
|
|
return &model.OrgPerm{Member: true, Admin: member.AccessLevel >= gitlab.OwnerPermissions}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(members) < opts.PerPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.OrgPerm{}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
func (g *GitLab) Org(ctx context.Context, u *model.User, owner string) (*model.Org, error) {
|
|
|
|
client, err := newClient(g.url, u.Token, g.SkipVerify)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 1,
|
|
|
|
},
|
2023-11-20 07:23:01 +00:00
|
|
|
Username: gitlab.Ptr(owner),
|
2023-07-21 17:45:32 +00:00
|
|
|
})
|
|
|
|
if len(users) == 1 && err == nil {
|
|
|
|
return &model.Org{
|
|
|
|
Name: users[0].Username,
|
|
|
|
IsUser: true,
|
|
|
|
Private: users[0].PrivateProfile,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
groups, _, err := client.Groups.ListGroups(&gitlab.ListGroupsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: 1,
|
|
|
|
PerPage: 1,
|
|
|
|
},
|
2023-11-20 07:23:01 +00:00
|
|
|
Search: gitlab.Ptr(owner),
|
2023-07-21 17:45:32 +00:00
|
|
|
}, gitlab.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(groups) != 1 {
|
|
|
|
return nil, fmt.Errorf("could not find org %s", owner)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &model.Org{
|
2023-08-03 15:30:56 +00:00
|
|
|
Name: groups[0].FullPath,
|
2023-07-21 17:45:32 +00:00
|
|
|
Private: groups[0].Visibility != gitlab.PublicVisibility,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
Fix spelling: gitlab (#1411)
This is most of the GitLab changes that I dropped from #1405.
As before, I'm happy to adjust things...
<details><summary>Problematic Changes</summary>
Fwiw, this is the part that causes the tests to break (I don't
understand why, but I'm leaving this change out):
```patch
commit 703cbe3ed398bf32535120ead733b80aa145c8db
Author: Josh Soref <2119212+jsoref@users.noreply.github.com>
Date: Tue Nov 8 17:09:06 2022 -0500
event?! -- this seems broken
diff --git a/server/forge/gitlab/testdata/hooks.go b/server/forge/gitlab/testdata/hooks.go
index 7d39306..e394afc 100644
--- a/server/forge/gitlab/testdata/hooks.go
+++ b/server/forge/gitlab/testdata/hooks.go
@@ -27,7 +27,7 @@ var (
ServiceHookHeaders = http.Header{
"Content-Type": []string{"application/json"},
"User-Agent": []string{"GitLab/14.3.0"},
- "X-Gitlab-Event": []string{"Service Hook"},
+ "X-GitLab-Event": []string{"Service Hook"},
}
)
diff --git a/shared/token/token.go b/shared/token/token.go
index 3f15537..191e5ee 100644
--- a/shared/token/token.go
+++ b/shared/token/token.go
@@ -64,7 +64,7 @@ func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
return parse(bearer, fn)
}
- token = r.Header.Get("X-Gitlab-Token")
+ token = r.Header.Get("X-GitLab-Token")
if len(token) != 0 {
return parse(token, fn)
}
```
</details>
Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-11-09 16:16:17 +00:00
|
|
|
func (g *GitLab) loadChangedFilesFromMergeRequest(ctx context.Context, tmpRepo *model.Repo, pipeline *model.Pipeline, mergeIID int) (*model.Pipeline, error) {
|
2022-01-17 22:46:59 +00:00
|
|
|
_store, ok := store.TryFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
log.Error().Msg("could not get store from context")
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:01:23 +00:00
|
|
|
repo, err := _store.GetRepoNameFallback(tmpRepo.ForgeRemoteID, tmpRepo.FullName)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := _store.GetUser(repo.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-31 16:30:41 +00:00
|
|
|
client, err := newClient(g.url, user.Token, g.SkipVerify)
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_repo, err := g.getProject(ctx, client, repo.Owner, repo.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-06 15:33:06 +00:00
|
|
|
changes, _, err := client.MergeRequests.ListMergeRequestDiffs(_repo.ID, mergeIID, &gitlab.ListMergeRequestDiffsOptions{}, gitlab.WithContext(ctx))
|
2022-01-17 22:46:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-30 07:23:11 +00:00
|
|
|
files := make([]string, 0, len(changes)*2)
|
|
|
|
for _, file := range changes {
|
2022-01-17 22:46:59 +00:00
|
|
|
files = append(files, file.NewPath, file.OldPath)
|
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.ChangedFiles = utils.DedupStrings(files)
|
2022-01-17 22:46:59 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline, nil
|
2022-01-17 22:46:59 +00:00
|
|
|
}
|