Skip to content
Closed
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
39 changes: 36 additions & 3 deletions internal/cli/forge_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"sync"

"github.com/fullsend-ai/fullsend/internal/forge"
fj "github.com/fullsend-ai/fullsend/internal/forge/forgejo"
gl "github.com/fullsend-ai/fullsend/internal/forge/gitlab"
"github.com/fullsend-ai/fullsend/internal/repos"
"github.com/spf13/cobra"
)

var errGitLabTokenMissing = errors.New("no GitLab token found: set GITLAB_TOKEN or pass --gitlab-token")
var errForgejoTokenMissing = errors.New("no Forgejo token found: set FORGEJO_TOKEN or GITEA_TOKEN")

// resolveGitLabToken returns a GitLab personal or project access token
// from the environment. Unlike GitHub tokens, there is no `glab auth
Expand All @@ -24,15 +26,28 @@ func resolveGitLabToken() (string, error) {
return "", errGitLabTokenMissing
}

// resolveForgejoToken returns a Forgejo/Gitea access token from the
// environment (FORGEJO_TOKEN preferred, then GITEA_TOKEN).
func resolveForgejoToken() (string, error) {
if token := os.Getenv("FORGEJO_TOKEN"); token != "" {
return token, nil
}
if token := os.Getenv("GITEA_TOKEN"); token != "" {
return token, nil
}
return "", errForgejoTokenMissing
}

// newForgeClient creates a forge.Client for the given forge type.
// For GitHub, it delegates to newAuthenticatedGitHubClient (GH_TOKEN,
// GITHUB_TOKEN, gh auth token). For GitLab, it uses GITLAB_TOKEN or
// the provided gitlabToken override.
// the provided gitlabToken override. For Forgejo, it uses FORGEJO_TOKEN
// or GITEA_TOKEN (gitlabToken override is ignored).
//
// The baseURL parameter, when non-empty, sets the forge instance URL
// (from the manifest's forge section). It takes precedence over the
// GITLAB_API_URL / GITHUB_API_URL environment variables, which are
// kept as a fallback for callers that don't have a manifest yet.
// GITLAB_API_URL / GITHUB_API_URL / FORGEJO_API_URL environment variables,
// which are kept as a fallback for callers that don't have a manifest yet.
func newForgeClient(forgeName, gitlabToken, baseURL string, glOpts ...gl.Option) (forge.Client, error) {
switch forgeName {
case repos.ForgeGitLab:
Expand All @@ -54,6 +69,24 @@ func newForgeClient(forgeName, gitlabToken, baseURL string, glOpts ...gl.Option)
}
opts = append(opts, glOpts...)
return gl.New(token, opts...)
case repos.ForgeForgejo:
token, err := resolveForgejoToken()
if err != nil {
return nil, err
}
var opts []fj.Option
raw := baseURL
if raw == "" {
raw = os.Getenv("FORGEJO_API_URL")
}
if raw == "" {
raw = os.Getenv("GITEA_API_URL")
}
if raw == "" {
return nil, fmt.Errorf("forgejo API URL required: set FORGEJO_API_URL or GITEA_API_URL, or pass a manifest URL")
}
opts = append(opts, fj.WithBaseURL(raw))
return fj.New(token, opts...), nil
case repos.ForgeGitHub, "":
return newAuthenticatedGitHubClient("", baseURL)
default:
Expand Down
29 changes: 29 additions & 0 deletions internal/cli/forge_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"testing"

fj "github.com/fullsend-ai/fullsend/internal/forge/forgejo"
gl "github.com/fullsend-ai/fullsend/internal/forge/gitlab"
"github.com/fullsend-ai/fullsend/internal/repos"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -102,6 +103,34 @@ func TestNewForgeClient_Unsupported(t *testing.T) {
assert.Contains(t, err.Error(), "unsupported forge")
}

func TestNewForgeClient_Forgejo_FromEnv(t *testing.T) {
t.Setenv("FORGEJO_TOKEN", "fj-test-token")
t.Setenv("GITEA_TOKEN", "")
client, err := newForgeClient(repos.ForgeForgejo, "", "https://git.forge.internal")
require.NoError(t, err)
require.NotNil(t, client)
fjClient, ok := client.(*fj.LiveClient)
require.True(t, ok)
assert.Equal(t, "https://git.forge.internal/api/v1", fjClient.BaseURL())
}

func TestNewForgeClient_Forgejo_NoToken(t *testing.T) {
t.Setenv("FORGEJO_TOKEN", "")
t.Setenv("GITEA_TOKEN", "")
_, err := newForgeClient(repos.ForgeForgejo, "", "")
require.Error(t, err)
assert.Contains(t, err.Error(), "no Forgejo token found")
}

func TestNewForgeClient_Forgejo_RequiresURL(t *testing.T) {
t.Setenv("FORGEJO_TOKEN", "fj-test-token")
t.Setenv("FORGEJO_API_URL", "")
t.Setenv("GITEA_API_URL", "")
_, err := newForgeClient(repos.ForgeForgejo, "", "")
require.Error(t, err)
assert.Contains(t, err.Error(), "forgejo API URL required")
}

func TestNewForgeClientFactory_GitHub(t *testing.T) {
t.Setenv("GH_TOKEN", "ghp-test-token")
factory := newForgeClientFactory("", nil)
Expand Down
13 changes: 11 additions & 2 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,12 @@ func runAgent(ctx context.Context, agentName, fullsendDir, outputBase, targetRep
if roleErr := applyGitLabAgentCredentials(agentName, h.Role, os.Getenv, setFlagEnv, printer); roleErr != nil {
return roleErr
}
} else if forgePlatform == repos.ForgeForgejo {
// Forgejo has no GitHub App mint endpoint; use FORGEJO_TOKEN / GITEA_TOKEN directly.
mintCleanup = func() {}
Comment on lines +1050 to +1052

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

6. Forgejo scripts lack push credentials 🔗 Cross-repo conflict ≡ Correctness

runAgent skips minting for Forgejo but does not resolve FORGEJO_TOKEN or GITEA_TOKEN and
export the credential as PUSH_TOKEN to the runner environment. When only the documented Forgejo
credential is configured, the agents repository’s code and fix post-scripts reach their required
PUSH_TOKEN check without a token, while the sandbox may instead inherit an ambient GitHub token.
Agent Prompt
## Issue description
Forgejo client authentication can use `FORGEJO_TOKEN` or `GITEA_TOKEN`, but the agent run does not make that credential available as the `PUSH_TOKEN` required by its post-scripts.

## Fix Focus Areas
- internal/cli/run.go[1050-1055]
- internal/cli/forge_client.go[29-38]
- /cross_repos/agents/harness/code.yaml[89-94]
- /cross_repos/agents/scripts/post-code.sh[3627-3631]

## Recommended Fix
Add Forgejo agent credential setup analogous to `applyGitLabAgentCredentials`: resolve `FORGEJO_TOKEN` or `GITEA_TOKEN`, fail if neither is available, and use `setFlagEnv` to make the resolved credential available to runner scripts as `PUSH_TOKEN`. Coordinate this with Forgejo support in the agents repository, covering both the code and fix harnesses.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

if mintURL != "" {
printer.StepWarn("mint_url is ignored for forgejo; using FORGEJO_TOKEN/GITEA_TOKEN")
}
} else {
var mintErr error
minted, mintCleanup, mintErr = mintAgentTokenAtLevel(ctx, h.Role, mintURL, forgePlatform, runtimeLevel, printer)
Expand Down Expand Up @@ -7066,6 +7072,9 @@ func setupStatusNotifier(fullsendDir string, role string, forgePlatform string,
if forgePlatform == "gitlab" {
return setupStatusNotifierGitLab(notifyCfg, owner, repo, sOpts, printer)
}
if forgePlatform == repos.ForgeForgejo {
return nil, fmt.Errorf("status notifications are not supported for forgejo yet")
}
return setupStatusNotifierGitHub(notifyCfg, fsCfg, owner, repo, role, sOpts, printer)
}

Expand Down Expand Up @@ -7471,7 +7480,7 @@ var roleTokenVars = map[string][]tokenVar{
// the function, instead of at the call site, also means a test can pass
// an already-cancelled ctx directly and still observe the remint run.
func remintAgentTokenForPostScript(ctx context.Context, h *harness.Harness, mintURL, forgePlatform, currentLevel string, printer *ui.Printer) (func(), error) {
if forgePlatform == "gitlab" || mintURL == "" {
if forgePlatform == "gitlab" || forgePlatform == repos.ForgeForgejo || mintURL == "" {
return func() {}, nil
}
remintCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), remintForPostScriptTimeout)
Expand Down Expand Up @@ -7540,7 +7549,7 @@ func syncRunnerEnvTokens(h *harness.Harness) {
// No-op when levels match, mintURL is empty, or the forge is GitLab.
func maybeRemintAgentTokenForStage(ctx context.Context, h *harness.Harness, mintURL, forgePlatform, stage, currentLevel string, printer *ui.Printer) (func(), error) {
noop := func() {}
if h == nil || mintURL == "" || forgePlatform == "gitlab" {
if h == nil || mintURL == "" || forgePlatform == "gitlab" || forgePlatform == repos.ForgeForgejo {
return noop, nil
}
level := h.PrivilegeLevelForStage(stage)
Expand Down
8 changes: 7 additions & 1 deletion internal/forge/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// DetectForge identifies which forge platform a git remote URL points to.
// Returns "github" or "gitlab". Returns an error for unknown hosts
// Returns "github", "gitlab", or "forgejo". Returns an error for unknown hosts
// with a suggestion to use the --forge flag.
//
// Note: this detects the forge from a remote URL, which is distinct from
Expand All @@ -25,7 +25,13 @@ func DetectForge(remoteURL string) (string, error) {
return "github", nil
case "gitlab.com":
return "gitlab", nil
case "codeberg.org", "gitea.com":
return "forgejo", nil
default:
// Common self-hosted Forgejo/Gitea hostnames.
if strings.Contains(strings.ToLower(host), "forgejo") || strings.Contains(strings.ToLower(host), "gitea") {
return "forgejo", nil
}
return "", fmt.Errorf("unknown forge host %q: use --forge flag for self-hosted instances", host)
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/forge/detect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ func TestDetectForge(t *testing.T) {
remoteURL: "https://gitlab.com/org/repo.git",
want: "gitlab",
},
{
name: "forgejo codeberg",
remoteURL: "https://codeberg.org/org/repo.git",
want: "forgejo",
},
{
name: "forgejo self-hosted hostname",
remoteURL: "https://forgejo.example.com/org/repo.git",
want: "forgejo",
},
{
name: "HTTPS GitLab without .git",
remoteURL: "https://gitlab.com/org/repo",
Expand Down
37 changes: 37 additions & 0 deletions internal/forge/forgejo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Forgejo / Gitea `forge.Client`

Implementation of `forge.Client` for Forgejo (and Gitea-compatible) API v1.

ForgeOps backlog: **FACTORY-FULLSEND-FORGEJO-CLIENT-001** (ADR-0020 deferred item).

## Usage

```go
c := forgejo.New(os.Getenv("FORGEJO_TOKEN"), forgejo.WithBaseURL("https://codeberg.org"))
```

CLI wiring (`internal/cli/forge_client.go`) resolves:

| Env | Role |
|---|---|
| `FORGEJO_TOKEN` / `GITEA_TOKEN` | Access token |
| `FORGEJO_API_URL` / `GITEA_API_URL` | Required API base (or manifest URL) |

`forge.DetectForge` returns `"forgejo"` for `codeberg.org`, `gitea.com`, and hosts containing `forgejo`/`gitea`. Self-hosted hosts without those markers need `--forge forgejo`.

## Coverage

- Core REST methods (repos, issues, PRs, labels, branches, contents) are live.
- High-value: forks, multi-file commits (sequential/non-atomic), cross-repo PRs, issue comment CRUD,
PR reviews/commits/file diffs, branch protection reads, Actions secrets create/exists/delete,
`IsInstallationToken` → false, `GetTokenScopes` → nil (no OAuth scope enum).
- Actions workflows/runs/jobs/artifacts/variables, issue/comment reactions, PR authorization context,
and best-effort review threads (per review comment) are live via `actions_reactions.go`.
- Remaining methods (mostly GitLab CI / org-level Actions / annotations) return `forge.ErrNotSupported` via `stubs.go`.
- Live smoke: `go test ./internal/forge/forgejo/ -tags live_forgejo` with `FORGEJO_TOKEN` + `FORGEJO_API_URL` set.

## Non-goals

- Full repos.yaml install / converge parity for Forgejo
- GitLab pipeline APIs (stay stubs)
- GitHub GraphQL review-thread parity (Forgejo maps comments → threads)
Loading
Loading