Treat `.ghe.com` domain as hosted environment (#2480)
Co-authored-by: Nikola Jokic <jokicnikola07@gmail.com>
This commit is contained in:
		
							parent
							
								
									a804bf8b00
								
							
						
					
					
						commit
						8fa4520376
					
				|  | @ -3,6 +3,7 @@ package actions | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/url" | 	"net/url" | ||||||
|  | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
|  | @ -34,9 +35,7 @@ func ParseGitHubConfigFromURL(in string) (*GitHubConfig, error) { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	isHosted := u.Host == "github.com" || | 	isHosted := isHostedGitHubURL(u) | ||||||
| 		u.Host == "www.github.com" || |  | ||||||
| 		u.Host == "github.localhost" |  | ||||||
| 
 | 
 | ||||||
| 	configURL := &GitHubConfig{ | 	configURL := &GitHubConfig{ | ||||||
| 		ConfigURL: u, | 		ConfigURL: u, | ||||||
|  | @ -76,23 +75,35 @@ func ParseGitHubConfigFromURL(in string) (*GitHubConfig, error) { | ||||||
| func (c *GitHubConfig) GitHubAPIURL(path string) *url.URL { | func (c *GitHubConfig) GitHubAPIURL(path string) *url.URL { | ||||||
| 	result := &url.URL{ | 	result := &url.URL{ | ||||||
| 		Scheme: c.ConfigURL.Scheme, | 		Scheme: c.ConfigURL.Scheme, | ||||||
|  | 		Host:   c.ConfigURL.Host, // default for Enterprise mode
 | ||||||
|  | 		Path:   "/api/v3",        // default for Enterprise mode
 | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	switch c.ConfigURL.Host { | 	isHosted := isHostedGitHubURL(c.ConfigURL) | ||||||
| 	// Hosted
 |  | ||||||
| 	case "github.com", "github.localhost": |  | ||||||
| 		result.Host = fmt.Sprintf("api.%s", c.ConfigURL.Host) |  | ||||||
| 	// re-routing www.github.com to api.github.com
 |  | ||||||
| 	case "www.github.com": |  | ||||||
| 		result.Host = "api.github.com" |  | ||||||
| 
 | 
 | ||||||
| 	// Enterprise
 | 	if isHosted { | ||||||
| 	default: | 		result.Host = fmt.Sprintf("api.%s", c.ConfigURL.Host) | ||||||
| 		result.Host = c.ConfigURL.Host | 		result.Path = "" | ||||||
| 		result.Path = "/api/v3" | 
 | ||||||
|  | 		if strings.EqualFold("www.github.com", c.ConfigURL.Host) { | ||||||
|  | 			// re-routing www.github.com to api.github.com
 | ||||||
|  | 			result.Host = "api.github.com" | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	result.Path += path | 	result.Path += path | ||||||
| 
 | 
 | ||||||
| 	return result | 	return result | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func isHostedGitHubURL(u *url.URL) bool { | ||||||
|  | 	_, forceGhes := os.LookupEnv("GITHUB_ACTIONS_FORCE_GHES") | ||||||
|  | 	if forceGhes { | ||||||
|  | 		return false | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return strings.EqualFold(u.Host, "github.com") || | ||||||
|  | 		strings.EqualFold(u.Host, "www.github.com") || | ||||||
|  | 		strings.EqualFold(u.Host, "github.localhost") || | ||||||
|  | 		strings.HasSuffix(u.Host, ".ghe.com") | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -3,6 +3,7 @@ package actions_test | ||||||
| import ( | import ( | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"net/url" | 	"net/url" | ||||||
|  | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
|  | @ -117,6 +118,16 @@ func TestGitHubConfig(t *testing.T) { | ||||||
| 					IsHosted:     false, | 					IsHosted:     false, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  | 			{ | ||||||
|  | 				configURL: "https://my-ghes.ghe.com/org/", | ||||||
|  | 				expected: &actions.GitHubConfig{ | ||||||
|  | 					Scope:        actions.GitHubScopeOrganization, | ||||||
|  | 					Enterprise:   "", | ||||||
|  | 					Organization: "org", | ||||||
|  | 					Repository:   "", | ||||||
|  | 					IsHosted:     true, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		for _, test := range tests { | 		for _, test := range tests { | ||||||
|  | @ -151,9 +162,35 @@ func TestGitHubConfig_GitHubAPIURL(t *testing.T) { | ||||||
| 	t.Run("when hosted", func(t *testing.T) { | 	t.Run("when hosted", func(t *testing.T) { | ||||||
| 		config, err := actions.ParseGitHubConfigFromURL("https://github.com/org/repo") | 		config, err := actions.ParseGitHubConfigFromURL("https://github.com/org/repo") | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
|  | 		assert.True(t, config.IsHosted) | ||||||
| 
 | 
 | ||||||
| 		result := config.GitHubAPIURL("/some/path") | 		result := config.GitHubAPIURL("/some/path") | ||||||
| 		assert.Equal(t, "https://api.github.com/some/path", result.String()) | 		assert.Equal(t, "https://api.github.com/some/path", result.String()) | ||||||
| 	}) | 	}) | ||||||
| 	t.Run("when not hosted", func(t *testing.T) {}) | 	t.Run("when hosted with ghe.com", func(t *testing.T) { | ||||||
|  | 		config, err := actions.ParseGitHubConfigFromURL("https://github.ghe.com/org/repo") | ||||||
|  | 		require.NoError(t, err) | ||||||
|  | 		assert.True(t, config.IsHosted) | ||||||
|  | 
 | ||||||
|  | 		result := config.GitHubAPIURL("/some/path") | ||||||
|  | 		assert.Equal(t, "https://api.github.ghe.com/some/path", result.String()) | ||||||
|  | 	}) | ||||||
|  | 	t.Run("when not hosted", func(t *testing.T) { | ||||||
|  | 		config, err := actions.ParseGitHubConfigFromURL("https://ghes.com/org/repo") | ||||||
|  | 		require.NoError(t, err) | ||||||
|  | 		assert.False(t, config.IsHosted) | ||||||
|  | 
 | ||||||
|  | 		result := config.GitHubAPIURL("/some/path") | ||||||
|  | 		assert.Equal(t, "https://ghes.com/api/v3/some/path", result.String()) | ||||||
|  | 	}) | ||||||
|  | 	t.Run("when not hosted with ghe.com", func(t *testing.T) { | ||||||
|  | 		os.Setenv("GITHUB_ACTIONS_FORCE_GHES", "1") | ||||||
|  | 		defer os.Unsetenv("GITHUB_ACTIONS_FORCE_GHES") | ||||||
|  | 		config, err := actions.ParseGitHubConfigFromURL("https://test.ghe.com/org/repo") | ||||||
|  | 		require.NoError(t, err) | ||||||
|  | 		assert.False(t, config.IsHosted) | ||||||
|  | 
 | ||||||
|  | 		result := config.GitHubAPIURL("/some/path") | ||||||
|  | 		assert.Equal(t, "https://test.ghe.com/api/v3/some/path", result.String()) | ||||||
|  | 	}) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue