Skip to content
Open
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
4 changes: 3 additions & 1 deletion backend/plugins/gh-copilot/api/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package api

import (
"strings"

"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
Expand Down Expand Up @@ -93,7 +95,7 @@ func validateConnection(connection *models.GhCopilotConnection) errors.Error {
if connection == nil {
return errors.BadInput.New("connection is required")
}
if connection.Organization == "" && !connection.HasEnterprise() {
if strings.TrimSpace(connection.Organization) == "" && !connection.HasEnterprise() {
return errors.BadInput.New("either enterprise or organization is required")
}
if connection.Token == "" {
Expand Down
50 changes: 50 additions & 0 deletions backend/plugins/gh-copilot/api/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,63 @@ limitations under the License.
package api

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"

helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/gh-copilot/models"
)

func decodeBody(t *testing.T, raw string) map[string]interface{} {
t.Helper()
var body map[string]interface{}
if err := json.Unmarshal([]byte(raw), &body); err != nil {
t.Fatalf("failed to unmarshal test payload: %v", err)
}
return body
}

func TestValidateConnection_EnterpriseSlugOnly_ViaDecode(t *testing.T) {
body := decodeBody(t, `{
"name": "my-copilot-conn",
"endpoint": "https://api.github.com",
"organization": "",
"enterprise": "my-company",
"token": "ghp_example",
"rateLimitPerHour": 5000
}`)

connection := &models.GhCopilotConnection{}
assert.NoError(t, helper.Decode(body, connection, vld))

connection.Normalize()
assert.Equal(t, "my-company", connection.Enterprise)
assert.True(t, connection.HasEnterprise())

err := validateConnection(connection)
assert.NoError(t, err)
}

func TestValidateConnection_EnterpriseSlugWithWhitespace_ViaDecode(t *testing.T) {
body := decodeBody(t, `{
"organization": " ",
"enterprise": " my-company ",
"token": "ghp_example"
}`)

connection := &models.GhCopilotConnection{}
assert.NoError(t, helper.Decode(body, connection, vld))

connection.Normalize()
assert.Equal(t, "my-company", connection.Enterprise)
assert.Equal(t, "", connection.Organization)

err := validateConnection(connection)
assert.NoError(t, err)
}

func TestValidateConnection_Success(t *testing.T) {
connection := &models.GhCopilotConnection{
GhCopilotConn: models.GhCopilotConn{
Expand Down
2 changes: 2 additions & 0 deletions backend/plugins/gh-copilot/models/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@ func (connection *GhCopilotConnection) Normalize() {
if connection.RateLimitPerHour <= 0 {
connection.RateLimitPerHour = DefaultRateLimitPerHour
}
connection.Organization = strings.TrimSpace(connection.Organization)
connection.Enterprise = strings.TrimSpace(connection.Enterprise)
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestConnection(ctx stdctx.Context, br corectx.BasicRes, connection *models.
hasOrg := strings.TrimSpace(connection.Organization) != ""

if !hasEnterprise && !hasOrg {
return nil, errors.BadInput.New("either enterprise or organization must be specified")
return nil, errors.BadInput.New("either enterprise or organization is required")
}

apiClient, err := helper.NewApiClientFromConnection(ctx, br, connection)
Expand Down
2 changes: 2 additions & 0 deletions config-ui/src/api/connection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const test = (
| 'adminApiKey'
| 'organization'
| 'organizationId'
| 'enterprise'
| 'customHeaders'
>
>,
Expand All @@ -79,6 +80,7 @@ export const testOld = (
| 'adminApiKey'
| 'organization'
| 'organizationId'
| 'enterprise'
| 'customHeaders'
>,
): Promise<IConnectionOldTestResult> => request(`/plugins/${plugin}/test`, { method: 'post', data: payload });
2 changes: 2 additions & 0 deletions config-ui/src/types/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface IConnectionAPI {
rateLimitPerHour?: number;
organization?: string;
organizationId?: string;
enterprise?: string;
customHeaders?: ICustomHeader[];
}

Expand Down Expand Up @@ -95,5 +96,6 @@ export interface IConnection {
rateLimitPerHour?: number;
organization?: string;
organizationId?: string;
enterprise?: string;
customHeaders?: ICustomHeader[];
}
Loading