From 2d359067f617b9f8af389dc79eecea10200428bd Mon Sep 17 00:00:00 2001 From: Andrew Cassidy Date: Sun, 12 Oct 2025 08:53:45 +0000 Subject: [PATCH] fix: don't prepend server URL with https:// if it's an empty string (#1083) An attempt to address #1074 in a way that makes everyone happy Right now, any server URL that doesnt start with `http://` or `https://` automatically gets `https://` prepended to it. When no instance is set while running `forgejo-runner exec` this results in the `{FORGEJO,GITHUB}_SERVER_URL` environment variables being set to simply "https://". Any action using the GitHub actions toolkit chokes on this as an invalid URL and fails the run with a nondescript error. This PR instead leaves empty strings alone, which the actions toolkit seems much happier with Alternate fixes: * default the `--forgejo-instance` flag to a valid but unresolved url like 'http://example.com' so the actions don't crash * don't set the environment variables at all when the server URL is an empty string. This causes the actions toolkit to assume GitHub.com as a fallback - bug fixes - [PR](https://code.forgejo.org/forgejo/runner/pulls/1083): fix: don't prepend server URL with https:// if it's an empty string Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/1083 Reviewed-by: earl-warren Co-authored-by: Andrew Cassidy Co-committed-by: Andrew Cassidy --- act/runner/run_context.go | 14 ++------------ act/runner/run_context_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/act/runner/run_context.go b/act/runner/run_context.go index ff551322..9e4c2981 100644 --- a/act/runner/run_context.go +++ b/act/runner/run_context.go @@ -1208,7 +1208,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext ghc.RetentionDays = preset.RetentionDays instance := rc.Config.GitHubInstance - if !strings.HasPrefix(instance, "http://") && + if instance != "" && !strings.HasPrefix(instance, "http://") && !strings.HasPrefix(instance, "https://") { instance = "https://" + instance } @@ -1251,7 +1251,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext { // Adapt to Gitea instance := rc.Config.GitHubInstance - if !strings.HasPrefix(instance, "http://") && + if instance != "" && !strings.HasPrefix(instance, "http://") && !strings.HasPrefix(instance, "https://") { instance = "https://" + instance } @@ -1353,16 +1353,6 @@ func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubCon set("SERVER_URL", github.ServerURL) set("API_URL", github.APIURL) - { // Adapt to Forgejo - instance := rc.Config.GitHubInstance - if !strings.HasPrefix(instance, "http://") && - !strings.HasPrefix(instance, "https://") { - instance = "https://" + instance - } - set("SERVER_URL", instance) - set("API_URL", instance+"/api/v1") - } - if rc.Config.ArtifactServerPath != "" { setActionRuntimeVars(rc, env) } diff --git a/act/runner/run_context_test.go b/act/runner/run_context_test.go index 110afb45..4e11ba62 100644 --- a/act/runner/run_context_test.go +++ b/act/runner/run_context_test.go @@ -280,6 +280,39 @@ func TestRunContext_GetBindsAndMounts(t *testing.T) { }) } +func TestRunContext_GetGithubContextURL(t *testing.T) { + table := []struct { + instance string + serverURL string + APIURL string + }{ + {instance: "", serverURL: "", APIURL: "/api/v1"}, + {instance: "example.com", serverURL: "https://example.com", APIURL: "https://example.com/api/v1"}, + {instance: "http://example.com", serverURL: "http://example.com", APIURL: "http://example.com/api/v1"}, + {instance: "https://example.com", serverURL: "https://example.com", APIURL: "https://example.com/api/v1"}, + } + for _, data := range table { + t.Run(data.instance, func(t *testing.T) { + rc := &RunContext{ + EventJSON: "{}", + Config: &Config{ + GitHubInstance: data.instance, + }, + Run: &model.Run{ + Workflow: &model.Workflow{ + Name: "GitHubContextTest", + }, + }, + } + + ghc := rc.getGithubContext(t.Context()) + + assert.Equal(t, data.serverURL, ghc.ServerURL) + assert.Equal(t, data.APIURL, ghc.APIURL) + }) + } +} + func TestRunContext_GetGithubContextRef(t *testing.T) { table := []struct { event string