Skip to content

Commit

Permalink
use gitea NewClient to catch errors (#427)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Oct 24, 2024
1 parent 1c68879 commit 2902af3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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)
}

0 comments on commit 2902af3

Please sign in to comment.