Code Cleanup (#869)

- switch to golangci-lint for linting
- switch to gofmpt for formatting
- fix lint and fmt issues that came up from switch to new tools
- upgrade go-sdk to 0.23.2
- support pagination for listing tracked times
- remove `FixPullHeadSha` workaround (upstream fix has been merged for 5+ years at this point)
- standardize on US spelling (previously a mix of US&UK spelling)
- remove some unused code
- reduce some duplication in parsing state and issue type
- reduce some duplication in reading input for secrets and variables
- reduce some duplication with PR Review code
- report error for when yaml parsing fails
- various other misc cleanup

Reviewed-on: https://gitea.com/gitea/tea/pulls/869
Co-authored-by: techknowlogick <techknowlogick@gitea.com>
Co-committed-by: techknowlogick <techknowlogick@gitea.com>
This commit is contained in:
techknowlogick
2026-02-02 22:39:26 +00:00
committed by techknowlogick
parent ae740a66e8
commit 20da414145
62 changed files with 399 additions and 356 deletions

View File

@@ -83,14 +83,15 @@ func loadConfig() (err error) {
ymlPath := GetConfigPath()
exist, _ := utils.FileExist(ymlPath)
if exist {
bs, err := os.ReadFile(ymlPath)
if err != nil {
err = fmt.Errorf("Failed to read config file: %s", ymlPath)
bs, readErr := os.ReadFile(ymlPath)
if readErr != nil {
err = fmt.Errorf("failed to read config file %s: %w", ymlPath, readErr)
return
}
err = yaml.Unmarshal(bs, &config)
if err != nil {
err = fmt.Errorf("Failed to parse contents of config file: %s", ymlPath)
if unmarshalErr := yaml.Unmarshal(bs, &config); unmarshalErr != nil {
err = fmt.Errorf("failed to parse config file %s: %w", ymlPath, unmarshalErr)
return
}
}
})

View File

@@ -46,6 +46,12 @@ func (ctx *TeaContext) GetRemoteRepoHTMLURL() string {
return path.Join(ctx.Login.URL, ctx.Owner, ctx.Repo)
}
// IsInteractiveMode returns true if the command is running in interactive mode
// (no flags provided and stdout is a terminal)
func (ctx *TeaContext) IsInteractiveMode() bool {
return ctx.Command.NumFlags() == 0
}
// Ensure checks if requirements on the context are set, and terminates otherwise.
func (ctx *TeaContext) Ensure(req CtxRequirement) {
if req.LocalRepo && ctx.LocalRepo == nil {

View File

@@ -143,7 +143,7 @@ func (r TeaRepo) TeaFindBranchByName(branchName, repoURL string) (b *git_config.
defer iter.Close()
var remoteRefName git_plumbing.ReferenceName
var localRefName git_plumbing.ReferenceName
var remoteSearchingName = fmt.Sprintf("%s/%s", remoteName, branchName)
remoteSearchingName := fmt.Sprintf("%s/%s", remoteName, branchName)
err = iter.ForEach(func(ref *git_plumbing.Reference) error {
if ref.Name().IsRemote() && ref.Name().Short() == remoteSearchingName {
remoteRefName = ref.Name()

View File

@@ -37,7 +37,7 @@ func TestRepoFromPath_Worktree(t *testing.T) {
// Create an initial commit (required for worktree)
readmePath := filepath.Join(mainRepoPath, "README.md")
err = os.WriteFile(readmePath, []byte("# Test Repo\n"), 0644)
err = os.WriteFile(readmePath, []byte("# Test Repo\n"), 0o644)
assert.NoError(t, err)
cmd = exec.Command("git", "-C", mainRepoPath, "add", "README.md")
assert.NoError(t, cmd.Run())

View File

@@ -19,7 +19,7 @@ import (
// MergePull interactively creates a PR
func MergePull(ctx *context.TeaContext) error {
if ctx.LocalRepo == nil {
return fmt.Errorf("Must specify a PR index")
return fmt.Errorf("pull request index is required")
}
branch, _, err := ctx.LocalRepo.TeaGetCurrentBranchNameAndSHA()
@@ -51,9 +51,12 @@ func getPullIndex(ctx *context.TeaContext, branch string) (int64, error) {
// paginated fetch
var prs []*gitea.PullRequest
var err error
for {
var err error
prs, _, err = c.ListRepoPullRequests(ctx.Owner, ctx.Repo, opts)
if err != nil {
return 0, err
}
if len(prs) == 0 {
return 0, fmt.Errorf("No open PRs found")
}

View File

@@ -12,7 +12,7 @@ import (
// NotificationsList prints a listing of notification threads
func NotificationsList(news []*gitea.NotificationThread, output string, fields []string) {
var printables = make([]printable, len(news))
printables := make([]printable, len(news))
for i, x := range news {
printables[i] = &printableNotification{x}
}

View File

@@ -111,7 +111,6 @@ func formatReviews(pr *gitea.PullRequest, reviews []*gitea.PullReview) string {
reviewByUserOrTeam[fmt.Sprintf("team_%d", review.ReviewerTeam.ID)] = review
}
}
}
}
@@ -173,7 +172,7 @@ var PullFields = []string{
func printPulls(pulls []*gitea.PullRequest, output string, fields []string) {
labelMap := map[int64]string{}
var printables = make([]printable, len(pulls))
printables := make([]printable, len(pulls))
machineReadable := isMachineReadable(output)
for i, x := range pulls {
@@ -227,13 +226,13 @@ func (x printablePull) FormatField(field string, machineReadable bool) string {
}
return ""
case "labels":
var labels = make([]string, len(x.Labels))
labels := make([]string, len(x.Labels))
for i, l := range x.Labels {
labels[i] = (*x.formattedLabels)[l.ID]
}
return strings.Join(labels, " ")
case "assignees":
var assignees = make([]string, len(x.Assignees))
assignees := make([]string, len(x.Assignees))
for i, a := range x.Assignees {
assignees[i] = formatUserName(a)
}

View File

@@ -11,7 +11,7 @@ import (
// TrackedTimesList print list of tracked times to stdout
func TrackedTimesList(times []*gitea.TrackedTime, outputType string, fields []string, printTotal bool) {
var printables = make([]printable, len(times))
printables := make([]printable, len(times))
var totalDuration int64
for i, t := range times {
totalDuration += t.Time

View File

@@ -53,7 +53,7 @@ func UserDetails(user *gitea.User) {
// UserList prints a listing of the users
func UserList(user []*gitea.User, output string, fields []string) {
var printables = make([]printable, len(user))
printables := make([]printable, len(user))
for i, u := range user {
printables[i] = &printableUser{u}
}

View File

@@ -51,7 +51,7 @@ func CreateLogin(name, token, user, passwd, otp, scopes, sshKey, giteaURL, sshCe
// checks ...
// ... if we have a url
if len(giteaURL) == 0 {
return fmt.Errorf("You have to input Gitea server URL")
return fmt.Errorf("Gitea server URL is required")
}
// ... if there already exist a login with same name

View File

@@ -15,7 +15,6 @@ import (
// CreateMilestone creates a milestone in the given repo and prints the result
func CreateMilestone(login *config.Login, repoOwner, repoName, title, description string, deadline *time.Time, state gitea.StateType) error {
// title is required
if len(title) == 0 {
return fmt.Errorf("Title is required")

View File

@@ -9,7 +9,6 @@ import (
"code.gitea.io/sdk/gitea"
"code.gitea.io/tea/modules/config"
local_git "code.gitea.io/tea/modules/git"
"code.gitea.io/tea/modules/workaround"
"github.com/go-git/go-git/v5"
git_config "github.com/go-git/go-git/v5/config"
@@ -29,9 +28,6 @@ func PullCheckout(
if err != nil {
return fmt.Errorf("couldn't fetch PR: %s", err)
}
if err := workaround.FixPullHeadSha(client, pr); err != nil {
return err
}
// FIXME: should use ctx.LocalRepo..?
localRepo, err := local_git.RepoForWorkdir()

View File

@@ -8,7 +8,6 @@ import (
"code.gitea.io/tea/modules/config"
local_git "code.gitea.io/tea/modules/git"
"code.gitea.io/tea/modules/workaround"
"code.gitea.io/sdk/gitea"
git_config "github.com/go-git/go-git/v5/config"
@@ -33,9 +32,6 @@ func PullClean(login *config.Login, repoOwner, repoName string, index int64, ign
if err != nil {
return err
}
if err := workaround.FixPullHeadSha(client, pr); err != nil {
return err
}
if pr.State == gitea.StateOpen {
return fmt.Errorf("PR is still open, won't delete branches")
@@ -96,15 +92,15 @@ call me again with the --ignore-sha flag`, remoteBranch)
if !remoteDeleted && pr.Head.Repository.Permissions.Push {
fmt.Printf("Deleting remote branch %s\n", remoteBranch)
url, err := r.TeaRemoteURL(branch.Remote)
if err != nil {
return err
url, urlErr := r.TeaRemoteURL(branch.Remote)
if urlErr != nil {
return urlErr
}
auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback)
if err != nil {
return err
auth, authErr := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback)
if authErr != nil {
return authErr
}
err = r.TeaDeleteRemoteBranch(branch.Remote, remoteBranch, auth)
return r.TeaDeleteRemoteBranch(branch.Remote, remoteBranch, auth)
}
return err
return nil
}

View File

@@ -40,7 +40,7 @@ func RepoClone(
return nil, err
}
// default path behaviour as native git
// default path behavior as native git
if path == "" {
path = repoName
}

87
modules/utils/input.go Normal file
View File

@@ -0,0 +1,87 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package utils
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"syscall"
"github.com/urfave/cli/v3"
"golang.org/x/term"
)
// ReadValueOptions contains options for reading a value from various sources
type ReadValueOptions struct {
// ResourceName is the name of the resource (e.g., "secret", "variable")
ResourceName string
// PromptMsg is the message to display when prompting interactively
PromptMsg string
// Hidden determines if the input should be hidden (for secrets/passwords)
Hidden bool
// AllowEmpty determines if empty values are allowed
AllowEmpty bool
}
// ReadValue reads a value from various sources in the following priority order:
// 1. From a file specified by --file flag
// 2. From stdin if --stdin flag is set
// 3. From command arguments (second argument)
// 4. Interactive prompt
func ReadValue(cmd *cli.Command, opts ReadValueOptions) (string, error) {
var value string
// 1. Read from file
if filePath := cmd.String("file"); filePath != "" {
content, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
value = strings.TrimSpace(string(content))
} else if cmd.Bool("stdin") {
// 2. Read from stdin
content, err := io.ReadAll(os.Stdin)
if err != nil {
return "", fmt.Errorf("failed to read from stdin: %w", err)
}
value = strings.TrimSpace(string(content))
} else if cmd.Args().Len() >= 2 {
// 3. Use provided argument
value = cmd.Args().Get(1)
} else {
// 4. Interactive prompt
if opts.PromptMsg == "" {
opts.PromptMsg = fmt.Sprintf("Enter %s value", opts.ResourceName)
}
fmt.Printf("%s: ", opts.PromptMsg)
if opts.Hidden {
// Hidden input for secrets/passwords
byteValue, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", fmt.Errorf("failed to read %s value: %w", opts.ResourceName, err)
}
fmt.Println() // Add newline after hidden input
value = string(byteValue)
} else {
// Regular visible input - read entire line including spaces
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return "", fmt.Errorf("failed to read %s value: %w", opts.ResourceName, err)
}
value = strings.TrimSpace(input)
}
}
// Validate non-empty if required
if !opts.AllowEmpty && value == "" {
return "", fmt.Errorf("%s value cannot be empty", opts.ResourceName)
}
return value, nil
}

View File

@@ -62,9 +62,9 @@ func AbsPathWithExpansion(p string) (string, error) {
}
if p == "~" {
return u.HomeDir, nil
} else if strings.HasPrefix(p, "~/") {
return filepath.Join(u.HomeDir, p[2:]), nil
} else {
return filepath.Abs(p)
}
if strings.HasPrefix(p, "~/") {
return filepath.Join(u.HomeDir, p[2:]), nil
}
return filepath.Abs(p)
}

View File

@@ -1,29 +0,0 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package workaround
import (
"fmt"
"code.gitea.io/sdk/gitea"
)
// FixPullHeadSha is a workaround for https://github.com/go-gitea/gitea/issues/12675
// When no head sha is available, this is because the branch got deleted in the base repo.
// pr.Head.Ref points in this case not to the head repo branch name, but the base repo ref,
// which stays available to resolve the commit sha.
func FixPullHeadSha(client *gitea.Client, pr *gitea.PullRequest) error {
owner := pr.Base.Repository.Owner.UserName
repo := pr.Base.Repository.Name
if pr.Head != nil && pr.Head.Sha == "" {
refs, _, err := client.GetRepoRefs(owner, repo, pr.Head.Ref)
if err != nil {
return err
} else if len(refs) == 0 {
return fmt.Errorf("unable to resolve PR ref '%s'", pr.Head.Ref)
}
pr.Head.Sha = refs[0].Object.SHA
}
return nil
}