mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-10-15 19:42:06 +00:00
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 <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - bug fixes - [PR](https://code.forgejo.org/forgejo/runner/pulls/1083): <!--number 1083 --><!--line 0 --><!--description Zml4OiBkb24ndCBwcmVwZW5kIHNlcnZlciBVUkwgd2l0aCBodHRwczovLyBpZiBpdCdzIGFuIGVtcHR5IHN0cmluZw==-->fix: don't prepend server URL with https:// if it's an empty string<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/1083 Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org> Co-authored-by: Andrew Cassidy <drewcassidy@me.com> Co-committed-by: Andrew Cassidy <drewcassidy@me.com>
This commit is contained in:
parent
e25d6b3c91
commit
2d359067f6
2 changed files with 35 additions and 12 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue