From 60239288767564a19dbc3d380f5bf2519eeb0a69 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Mon, 18 May 2026 02:22:04 +0000 Subject: [PATCH] Fix token use with schemaless Gitea instance (#977) Fixes #973 ## Summary - Normalize schemaless `--gitea-instance` values before comparing clone URL hosts - Add regression tests for `GITEA_TOKEN` use with private action/reusable workflow clones on the same instance --------- Co-authored-by: silverwind <2021+silverwind@noreply.gitea.com> Co-authored-by: silverwind Reviewed-on: https://gitea.com/gitea/runner/pulls/977 Reviewed-by: Lunny Xiao Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com> Co-authored-by: Nicolas Co-committed-by: Nicolas --- act/runner/reusable_workflow.go | 5 +++ act/runner/reusable_workflow_test.go | 59 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/act/runner/reusable_workflow.go b/act/runner/reusable_workflow.go index 590ebc82..effb74db 100644 --- a/act/runner/reusable_workflow.go +++ b/act/runner/reusable_workflow.go @@ -308,6 +308,11 @@ func getGitCloneToken(conf *Config, cloneURL string) string { // 1. cloneURL is from the same Gitea instance that the runner is registered to // 2. the cloneURL does not have basic auth embedded func shouldCloneURLUseToken(instanceURL, cloneURL string) bool { + if !strings.HasPrefix(instanceURL, "http://") && + !strings.HasPrefix(instanceURL, "https://") { + instanceURL = "https://" + instanceURL + } + u1, err1 := url.Parse(instanceURL) u2, err2 := url.Parse(cloneURL) if err1 != nil || err2 != nil { diff --git a/act/runner/reusable_workflow_test.go b/act/runner/reusable_workflow_test.go index 8e618698..4e5d074c 100644 --- a/act/runner/reusable_workflow_test.go +++ b/act/runner/reusable_workflow_test.go @@ -123,6 +123,65 @@ func TestNewReusableWorkflowExecutorHoldsCloneLock(t *testing.T) { } } +func TestGetGitCloneTokenWithSchemalessGiteaInstance(t *testing.T) { + conf := &Config{ + GitHubInstance: "gitea.example.net", + Secrets: map[string]string{ + "GITEA_TOKEN": "token-value", + }, + } + + token := getGitCloneToken(conf, "https://gitea.example.net/actions/tools") + + require.Equal(t, "token-value", token) +} + +func TestShouldCloneURLUseToken(t *testing.T) { + tests := []struct { + name string + instanceURL string + cloneURL string + want bool + }{ + { + name: "same host with schemaless instance", + instanceURL: "gitea.example.net", + cloneURL: "https://gitea.example.net/actions/tools", + want: true, + }, + { + name: "same host with schemaless instance and port", + instanceURL: "gitea.example.net:3000", + cloneURL: "https://gitea.example.net:3000/actions/tools", + want: true, + }, + { + name: "different host", + instanceURL: "gitea.example.net", + cloneURL: "https://github.com/actions/tools", + want: false, + }, + { + name: "embedded basic auth", + instanceURL: "gitea.example.net", + cloneURL: "https://user:pass@gitea.example.net/actions/tools", + want: false, + }, + { + name: "invalid clone URL", + instanceURL: "gitea.example.net", + cloneURL: "://gitea.example.net/actions/tools", + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, shouldCloneURLUseToken(tt.instanceURL, tt.cloneURL)) + }) + } +} + func gitMust(t *testing.T, dir string, args ...string) { t.Helper() cmd := exec.Command("git", args...)