Move versions/filelocker into dedicated subpackages, and consistent headers in http requests (#888)

- move filelocker logic into dedicated subpackage
- consistent useragent in requests

Reviewed-on: https://gitea.com/gitea/tea/pulls/888
Co-authored-by: techknowlogick <techknowlogick@gitea.com>
Co-committed-by: techknowlogick <techknowlogick@gitea.com>
This commit is contained in:
techknowlogick
2026-02-05 18:05:43 +00:00
committed by techknowlogick
parent 982adb4d02
commit 49a9032d8a
15 changed files with 277 additions and 127 deletions

View File

@@ -13,6 +13,7 @@ import (
"strings"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/httputil"
)
// Client provides direct HTTP access to Gitea API
@@ -30,9 +31,9 @@ func NewClient(login *config.Login) *Client {
}
httpClient := &http.Client{
Transport: &http.Transport{
Transport: httputil.WrapTransport(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: login.Insecure},
},
}),
}
return &Client{

View File

@@ -18,6 +18,7 @@ import (
"time"
"code.gitea.io/tea/modules/config"
"code.gitea.io/tea/modules/httputil"
"code.gitea.io/tea/modules/task"
"code.gitea.io/tea/modules/utils"
@@ -196,15 +197,11 @@ func performBrowserOAuthFlow(opts OAuthOptions) (serverURL string, token *oauth2
// createHTTPClient creates an HTTP client with optional insecure setting
func createHTTPClient(insecure bool) *http.Client {
client := &http.Client{}
if insecure {
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
return &http.Client{
Transport: httputil.WrapTransport(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}),
}
return client
}
// generateCodeVerifier creates a cryptographically random string for PKCE

View File

@@ -5,9 +5,10 @@ package config
import (
"fmt"
"os"
"sync"
"time"
"code.gitea.io/tea/modules/filelock"
)
const (
@@ -16,9 +17,6 @@ const (
// mutexPollInterval is how often to retry acquiring the in-process mutex.
mutexPollInterval = 10 * time.Millisecond
// fileLockPollInterval is how often to retry acquiring the file lock.
fileLockPollInterval = 50 * time.Millisecond
)
// configMutex protects in-process concurrent access to the config.
@@ -42,30 +40,20 @@ func acquireConfigLock(lockPath string, timeout time.Duration) (unlock func() er
}
// Mutex acquired, now try file lock
file, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600)
remaining := max(time.Until(deadline), 0)
locker := filelock.New(lockPath, remaining)
fileUnlock, err := locker.Acquire()
if err != nil {
configMutex.Unlock()
return nil, fmt.Errorf("failed to open lock file: %w", err)
}
// Try to acquire file lock with remaining timeout
remaining := max(time.Until(deadline), 0)
if err := lockFile(file, remaining); err != nil {
file.Close()
configMutex.Unlock()
return nil, fmt.Errorf("failed to acquire file lock: %w", err)
return nil, err
}
// Return unlock function
return func() error {
unlockErr := unlockFile(file)
closeErr := file.Close()
unlockErr := fileUnlock()
configMutex.Unlock()
if unlockErr != nil {
return unlockErr
}
return closeErr
return unlockErr
}, nil
}

View File

@@ -18,6 +18,7 @@ import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/debug"
"code.gitea.io/tea/modules/httputil"
"code.gitea.io/tea/modules/theme"
"code.gitea.io/tea/modules/utils"
"github.com/charmbracelet/huh"
@@ -290,9 +291,9 @@ func doOAuthRefresh(l *Login) (*oauth2.Token, error) {
ctx := context.Background()
httpClient := &http.Client{
Transport: &http.Transport{
Transport: httputil.WrapTransport(&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: l.Insecure},
},
}),
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
@@ -336,7 +337,7 @@ func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client {
options = append([]gitea.ClientOption{gitea.SetGiteaVersion("")}, options...)
}
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient), gitea.SetUserAgent(httputil.UserAgent()))
if debug.IsDebug() {
options = append(options, gitea.SetDebugMode())
}

View File

@@ -0,0 +1,70 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package filelock
import (
"fmt"
"os"
"time"
)
const (
// DefaultTimeout is the default timeout for acquiring a file lock.
DefaultTimeout = 5 * time.Second
// FileLockPollInterval is how often to retry acquiring the file lock.
FileLockPollInterval = 50 * time.Millisecond
)
// Locker provides file-based locking with timeout.
type Locker struct {
path string
timeout time.Duration
}
// New creates a Locker for the given lock file path.
func New(lockPath string, timeout time.Duration) *Locker {
return &Locker{
path: lockPath,
timeout: timeout,
}
}
// WithLock executes fn while holding the lock.
func (l *Locker) WithLock(fn func() error) (retErr error) {
unlock, err := l.Acquire()
if err != nil {
return err
}
defer func() {
if unlockErr := unlock(); unlockErr != nil && retErr == nil {
retErr = fmt.Errorf("failed to release file lock: %w", unlockErr)
}
}()
return fn()
}
// Acquire acquires the file lock and returns an unlock function.
// The caller must call the unlock function to release the lock.
func (l *Locker) Acquire() (unlock func() error, err error) {
file, err := os.OpenFile(l.path, os.O_CREATE|os.O_RDWR, 0o600)
if err != nil {
return nil, fmt.Errorf("failed to open lock file: %w", err)
}
if err := lockFile(file, l.timeout); err != nil {
file.Close()
return nil, fmt.Errorf("failed to acquire file lock: %w", err)
}
return func() error {
unlockErr := unlockFile(file)
closeErr := file.Close()
if unlockErr != nil {
return unlockErr
}
return closeErr
}, nil
}

View File

@@ -0,0 +1,92 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package filelock
import (
"os"
"path/filepath"
"sync"
"testing"
"time"
)
func TestLocker_WithLock(t *testing.T) {
tmpDir := t.TempDir()
lockPath := filepath.Join(tmpDir, "test.lock")
locker := New(lockPath, DefaultTimeout)
counter := 0
err := locker.WithLock(func() error {
counter++
return nil
})
if err != nil {
t.Fatalf("WithLock failed: %v", err)
}
if counter != 1 {
t.Errorf("Expected counter to be 1, got %d", counter)
}
// Lock file should have been created
if _, err := os.Stat(lockPath); os.IsNotExist(err) {
t.Error("Lock file should have been created")
}
}
func TestLocker_Acquire(t *testing.T) {
tmpDir := t.TempDir()
lockPath := filepath.Join(tmpDir, "test.lock")
locker := New(lockPath, DefaultTimeout)
unlock, err := locker.Acquire()
if err != nil {
t.Fatalf("Acquire failed: %v", err)
}
// Lock should be held
if unlock == nil {
t.Fatal("unlock function should not be nil")
}
// Release the lock
if err := unlock(); err != nil {
t.Fatalf("unlock failed: %v", err)
}
}
func TestLocker_ConcurrentAccess(t *testing.T) {
tmpDir := t.TempDir()
lockPath := filepath.Join(tmpDir, "test.lock")
locker := New(lockPath, 5*time.Second)
var wg sync.WaitGroup
counter := 0
numGoroutines := 10
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func() {
defer wg.Done()
err := locker.WithLock(func() error {
// Read-modify-write to check for race conditions
tmp := counter
time.Sleep(1 * time.Millisecond)
counter = tmp + 1
return nil
})
if err != nil {
t.Errorf("WithLock failed: %v", err)
}
}()
}
wg.Wait()
if counter != numGoroutines {
t.Errorf("Expected counter to be %d, got %d (possible race condition)", numGoroutines, counter)
}
}

View File

@@ -3,7 +3,7 @@
//go:build unix
package config
package filelock
import (
"fmt"
@@ -29,7 +29,7 @@ func lockFile(file *os.File, timeout time.Duration) error {
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for file lock")
}
time.Sleep(fileLockPollInterval)
time.Sleep(FileLockPollInterval)
}
}

View File

@@ -3,7 +3,7 @@
//go:build windows
package config
package filelock
import (
"fmt"
@@ -36,7 +36,7 @@ func lockFile(file *os.File, timeout time.Duration) error {
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for file lock")
}
time.Sleep(fileLockPollInterval)
time.Sleep(FileLockPollInterval)
}
}

View File

@@ -0,0 +1,38 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package httputil
import (
"fmt"
"net/http"
"runtime"
"code.gitea.io/tea/modules/version"
)
// UserAgent returns the standard User-Agent string for tea.
func UserAgent() string {
ua := fmt.Sprintf("tea/%s (%s/%s)", version.Version, runtime.GOOS, runtime.GOARCH)
if version.SDK != "" {
ua += fmt.Sprintf(" go-sdk/%s", version.SDK)
}
return ua
}
// WrapTransport wraps an http.RoundTripper to add the User-Agent header.
func WrapTransport(base http.RoundTripper) http.RoundTripper {
if base == nil {
base = http.DefaultTransport
}
return &userAgentTransport{base: base}
}
type userAgentTransport struct {
base http.RoundTripper
}
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", UserAgent())
return t.base.RoundTrip(req)
}

View File

@@ -0,0 +1,44 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package version
import (
"fmt"
"runtime"
"strings"
)
// Version holds the current tea version.
// This is set at build time via ldflags.
// If the Version is moved to another package or name changed,
// build flags in .goreleaser.yaml or Makefile need to be updated accordingly.
var Version = "development"
// Tags holds the build tags used
var Tags = ""
// SDK holds the sdk version from go.mod
var SDK = ""
// Format returns a human-readable version string including
// go version, build tags, and SDK version when available.
func Format() string {
s := fmt.Sprintf("Version: %s\tgolang: %s",
bold(Version),
strings.ReplaceAll(runtime.Version(), "go", ""))
if len(Tags) != 0 {
s += fmt.Sprintf("\tbuilt with: %s", strings.ReplaceAll(Tags, " ", ", "))
}
if len(SDK) != 0 {
s += fmt.Sprintf("\tgo-sdk: %s", SDK)
}
return s
}
func bold(t string) string {
return fmt.Sprintf("\033[1m%s\033[0m", t)
}