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

use gitea NewClient to catch errors #427

Merged
merged 1 commit into from
Oct 24, 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
10 changes: 6 additions & 4 deletions pkg/controllers/localbuild/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ func (r *LocalbuildReconciler) setGiteaToken(ctx context.Context, secret corev1.
}

func getGiteaToken(ctx context.Context, baseUrl, username, password string) (string, error) {

giteaClient := gitea.NewClientWithHTTP(baseUrl, util.GetHttpClient())
giteaClient.SetBasicAuth(username, password)
giteaClient.SetContext(ctx)
giteaClient, err := gitea.NewClient(baseUrl, gitea.SetHTTPClient(util.GetHttpClient()),
gitea.SetBasicAuth(username, password), gitea.SetContext(ctx),
)
if err != nil {
return "", fmt.Errorf("creating gitea client: %w", err)
}
tokens, resp, err := giteaClient.ListAccessTokens(gitea.ListAccessTokensOptions{})
if err != nil {
return "", fmt.Errorf("listing gitea access tokens. status: %s error : %w", resp.Status, err)
Expand Down
15 changes: 15 additions & 0 deletions pkg/controllers/localbuild/gitea_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package localbuild

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGiteaInternalBaseUrl(t *testing.T) {
Expand All @@ -21,3 +26,13 @@ func TestGiteaInternalBaseUrl(t *testing.T) {
s = giteaInternalBaseUrl(c)
assert.Equal(t, "http://cnoe.localtest.me:8080/gitea", s)
}

func TestGetGiteaToken(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second * 35)
}))
defer ts.Close()
ctx := context.Background()
_, err := getGiteaToken(ctx, ts.URL, "", "")
require.Error(t, err)
}
Loading