Skip to content

Commit 91b944b

Browse files
authored
feat: update endpoint name and title (frain-dev#1945)
1 parent a29ca18 commit 91b944b

37 files changed

+544
-109
lines changed

api/dashboard_integration_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ func (s *DashboardIntegrationTestSuite) TestGetDashboardSummary() {
367367
endpoint := &datastore.Endpoint{
368368
UID: "abc",
369369
ProjectID: s.DefaultProject.UID,
370-
Title: "test-app",
370+
Name: "test-app",
371371
Secrets: datastore.Secrets{},
372372
SupportEmail: "test@suport.com",
373373
CreatedAt: time.Now(),
@@ -691,7 +691,7 @@ func (s *EndpointIntegrationTestSuite) Test_GetEndpoint_ValidEndpoint() {
691691
dbEndpoint, err := endpointRepo.FindEndpointByID(context.Background(), endpointID, s.DefaultProject.UID)
692692
require.NoError(s.T(), err)
693693
require.Equal(s.T(), endpoint.UID, dbEndpoint.UID)
694-
require.Equal(s.T(), endpoint.Title, dbEndpoint.Title)
694+
require.Equal(s.T(), endpoint.Name, dbEndpoint.Name)
695695
}
696696

697697
func (s *EndpointIntegrationTestSuite) Test_GetEndpoints_ValidEndpoints() {
@@ -759,8 +759,8 @@ func (s *EndpointIntegrationTestSuite) Test_CreateEndpoint() {
759759
endpointRepo := postgres.NewEndpointRepo(s.ConvoyApp.A.DB, s.ConvoyApp.A.Cache)
760760
dbEndpoint, err := endpointRepo.FindEndpointByID(context.Background(), endpoint.UID, s.DefaultProject.UID)
761761
require.NoError(s.T(), err)
762-
require.Equal(s.T(), endpointTitle, dbEndpoint.Title)
763-
require.Equal(s.T(), endpointURL, dbEndpoint.TargetURL)
762+
require.Equal(s.T(), endpointTitle, dbEndpoint.Name)
763+
require.Equal(s.T(), endpointURL, dbEndpoint.Url)
764764
}
765765

766766
func (s *EndpointIntegrationTestSuite) Test_CreateEndpoint_NoName() {
@@ -849,9 +849,9 @@ func (s *EndpointIntegrationTestSuite) Test_UpdateEndpoint() {
849849
dbEndpoint, err := endpointRepo.FindEndpointByID(context.Background(), endpointID, s.DefaultProject.UID)
850850
require.NoError(s.T(), err)
851851
require.Equal(s.T(), endpoint.UID, dbEndpoint.UID)
852-
require.Equal(s.T(), title, dbEndpoint.Title)
852+
require.Equal(s.T(), title, dbEndpoint.Name)
853853
require.Equal(s.T(), supportEmail, dbEndpoint.SupportEmail)
854-
require.Equal(s.T(), endpointURL, dbEndpoint.TargetURL)
854+
require.Equal(s.T(), endpointURL, dbEndpoint.Url)
855855
}
856856

857857
func (s *EndpointIntegrationTestSuite) Test_DeleteEndpoint() {
@@ -920,8 +920,8 @@ func (s *EndpointIntegrationTestSuite) Test_CreateEndpoint_With_Custom_Authentic
920920
var endpoint datastore.Endpoint
921921
parseResponse(s.T(), w.Result(), &endpoint)
922922

923-
require.Equal(s.T(), title, endpoint.Title)
924-
require.Equal(s.T(), endpointURL, endpoint.TargetURL)
923+
require.Equal(s.T(), title, endpoint.Name)
924+
require.Equal(s.T(), endpointURL, endpoint.Url)
925925
require.Equal(s.T(), datastore.EndpointAuthenticationType("api_key"), endpoint.Authentication.Type)
926926
require.Equal(s.T(), "x-api-key", endpoint.Authentication.ApiKey.HeaderName)
927927
require.Equal(s.T(), "testapikey", endpoint.Authentication.ApiKey.HeaderValue)
@@ -1121,7 +1121,7 @@ func (s *EventIntegrationTestSuite) Test_CreateEndpointEvent_With_App_ID_Valid_E
11211121
// Create an Endpoint with an app ID
11221122
endpoint := &datastore.Endpoint{
11231123
UID: endpointID,
1124-
Title: fmt.Sprintf("TestEndpoint-%s", endpointID),
1124+
Name: fmt.Sprintf("TestEndpoint-%s", endpointID),
11251125
ProjectID: s.DefaultProject.UID,
11261126
AppID: appID,
11271127
Secrets: datastore.Secrets{

api/migrations.go

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package api
22

33
import (
44
v20240101 "github.com/frain-dev/convoy/api/migrations/v20240101"
5+
v20240306 "github.com/frain-dev/convoy/api/migrations/v20240306"
56
"github.com/subomi/requestmigrations"
67
)
78

@@ -14,4 +15,10 @@ var migrations = requestmigrations.MigrationStore{
1415
&v20240101.UpdateEndpointRequestMigration{},
1516
&v20240101.UpdateEndpointResponseMigration{},
1617
},
18+
"2024-03-06": requestmigrations.Migrations{
19+
&v20240306.CreateEndpointResponseMigration{},
20+
&v20240306.GetEndpointResponseMigration{},
21+
&v20240306.GetEndpointsResponseMigration{},
22+
&v20240306.UpdateEndpointResponseMigration{},
23+
},
1724
}

api/migrations/v20240306/common.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package v20240306
2+
3+
import (
4+
"github.com/fatih/structs"
5+
)
6+
7+
func migrateEndpoint(oldPayload, newPayload interface{}) error {
8+
oldStruct := structs.New(oldPayload)
9+
newStruct := structs.New(newPayload)
10+
11+
var err error
12+
for _, f := range oldStruct.Fields() {
13+
if f.IsZero() {
14+
continue
15+
}
16+
17+
value := f.Value()
18+
jsonTag := f.Tag("json")
19+
20+
switch jsonTag {
21+
case "url":
22+
err = newStruct.Field("TargetURL").Set(value)
23+
if err != nil {
24+
return err
25+
}
26+
case "name":
27+
err = newStruct.Field("Title").Set(value)
28+
if err != nil {
29+
return err
30+
}
31+
default:
32+
err = newStruct.Field(f.Name()).Set(value)
33+
if err != nil {
34+
return err
35+
}
36+
}
37+
}
38+
39+
return nil
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package v20240306
2+
3+
import (
4+
"encoding/json"
5+
"github.com/frain-dev/convoy/datastore"
6+
"net/http"
7+
"time"
8+
9+
"github.com/frain-dev/convoy/api/models"
10+
"github.com/frain-dev/convoy/util"
11+
"gopkg.in/guregu/null.v4"
12+
)
13+
14+
type oldEndpointResponse struct {
15+
UID string `json:"uid" db:"id"`
16+
ProjectID string `json:"project_id" db:"project_id"`
17+
OwnerID string `json:"owner_id,omitempty" db:"owner_id"`
18+
TargetURL string `json:"target_url" db:"target_url"`
19+
Title string `json:"title" db:"title"`
20+
Secrets datastore.Secrets `json:"secrets" db:"secrets"`
21+
AdvancedSignatures bool `json:"advanced_signatures" db:"advanced_signatures"`
22+
Description string `json:"description" db:"description"`
23+
SlackWebhookURL string `json:"slack_webhook_url,omitempty" db:"slack_webhook_url"`
24+
SupportEmail string `json:"support_email,omitempty" db:"support_email"`
25+
AppID string `json:"-" db:"app_id"` // Deprecated but necessary for backward compatibility
26+
27+
HttpTimeout uint64 `json:"http_timeout" db:"http_timeout"`
28+
RateLimit int `json:"rate_limit" db:"rate_limit"`
29+
Events int64 `json:"events,omitempty" db:"event_count"`
30+
Status datastore.EndpointStatus `json:"status" db:"status"`
31+
32+
RateLimitDuration uint64 `json:"rate_limit_duration" db:"rate_limit_duration"`
33+
Authentication *datastore.EndpointAuthentication `json:"authentication" db:"authentication"`
34+
35+
CreatedAt time.Time `json:"created_at,omitempty" db:"created_at,omitempty" swaggertype:"string"`
36+
UpdatedAt time.Time `json:"updated_at,omitempty" db:"updated_at,omitempty" swaggertype:"string"`
37+
DeletedAt null.Time `json:"deleted_at,omitempty" db:"deleted_at" swaggertype:"string"`
38+
}
39+
40+
type CreateEndpointResponseMigration struct{}
41+
42+
func (c *CreateEndpointResponseMigration) Migrate(b []byte, h http.Header) ([]byte, http.Header, error) {
43+
var serverResponse util.ServerResponse
44+
err := json.Unmarshal(b, &serverResponse)
45+
if err != nil {
46+
return nil, nil, err
47+
}
48+
49+
if len(serverResponse.Data) == 0 {
50+
// nothing to transform.
51+
return b, h, nil
52+
}
53+
54+
var endpointResp *models.EndpointResponse
55+
err = json.Unmarshal(serverResponse.Data, &endpointResp)
56+
if err != nil {
57+
return nil, nil, err
58+
}
59+
60+
endpoint := endpointResp.Endpoint
61+
62+
var old oldEndpointResponse
63+
err = migrateEndpoint(&endpoint, &old)
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
b, err = json.Marshal(old)
69+
if err != nil {
70+
return nil, nil, err
71+
}
72+
73+
serverResponse.Data = b
74+
75+
sb, err := json.Marshal(serverResponse)
76+
if err != nil {
77+
return nil, nil, err
78+
}
79+
80+
return sb, h, nil
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package v20240306
2+
3+
import (
4+
"encoding/json"
5+
"github.com/frain-dev/convoy/datastore"
6+
"github.com/frain-dev/convoy/util"
7+
"net/http"
8+
"testing"
9+
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func Test_Migrate(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
payload *datastore.Endpoint
17+
}{
18+
{
19+
name: "should_migrate_name_and_url",
20+
payload: &datastore.Endpoint{
21+
Name: "test-endpoint",
22+
Url: "https://google.com",
23+
},
24+
},
25+
}
26+
27+
for _, tc := range tests {
28+
t.Run(tc.name, func(t *testing.T) {
29+
var header http.Header = map[string][]string{"X-Convoy-version": {"2024-01-01"}}
30+
31+
payloadBytes, err := json.Marshal(tc.payload)
32+
require.NoError(t, err)
33+
34+
serv := &util.ServerResponse{
35+
Status: true,
36+
Data: payloadBytes,
37+
}
38+
39+
body, err := json.Marshal(serv)
40+
require.NoError(t, err)
41+
42+
migration := GetEndpointResponseMigration{}
43+
res, _, err := migration.Migrate(body, header)
44+
require.NoError(t, err)
45+
46+
var endpoint map[string]any
47+
err = json.Unmarshal(res, &endpoint)
48+
require.NoError(t, err)
49+
50+
require.Equal(t, endpoint["data"].(map[string]any)["target_url"], tc.payload.Url)
51+
require.Equal(t, endpoint["data"].(map[string]any)["title"], tc.payload.Name)
52+
})
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package v20240306
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/frain-dev/convoy/api/models"
9+
"github.com/frain-dev/convoy/util"
10+
)
11+
12+
type GetEndpointResponseMigration struct{}
13+
14+
func (c *GetEndpointResponseMigration) Migrate(b []byte, h http.Header) ([]byte, http.Header, error) {
15+
var serverResponse util.ServerResponse
16+
err := json.Unmarshal(b, &serverResponse)
17+
if err != nil {
18+
return nil, nil, err
19+
}
20+
21+
if len(serverResponse.Data) == 0 {
22+
// nothing to transform.
23+
return b, h, nil
24+
}
25+
26+
var endpointResp *models.EndpointResponse
27+
err = json.Unmarshal(serverResponse.Data, &endpointResp)
28+
if err != nil {
29+
return nil, nil, err
30+
}
31+
32+
endpoint := endpointResp.Endpoint
33+
34+
var old oldEndpointResponse
35+
err = migrateEndpoint(&endpoint, &old)
36+
if err != nil {
37+
fmt.Printf("err: %+v\n", err)
38+
return nil, nil, err
39+
}
40+
41+
b, err = json.Marshal(old)
42+
if err != nil {
43+
fmt.Printf("err2: %+v\n", err)
44+
return nil, nil, err
45+
}
46+
47+
serverResponse.Data = b
48+
49+
sb, err := json.Marshal(serverResponse)
50+
if err != nil {
51+
fmt.Printf("err3: %+v\n", err)
52+
return nil, nil, err
53+
}
54+
55+
return sb, h, nil
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package v20240306
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/frain-dev/convoy/api/models"
8+
"github.com/frain-dev/convoy/util"
9+
)
10+
11+
type GetEndpointsResponseMigration struct{}
12+
13+
func (g *GetEndpointsResponseMigration) Migrate(b []byte, h http.Header) ([]byte, http.Header, error) {
14+
var serverResponse util.ServerResponse
15+
err := json.Unmarshal(b, &serverResponse)
16+
if err != nil {
17+
return nil, nil, err
18+
}
19+
20+
var pResp models.PagedResponse
21+
err = json.Unmarshal(serverResponse.Data, &pResp)
22+
if err != nil {
23+
return nil, nil, err
24+
}
25+
26+
if pResp.Content == nil {
27+
// nothing to transform.
28+
return b, h, nil
29+
}
30+
31+
endpoints, ok := pResp.Content.([]any)
32+
if !ok {
33+
// invalid type.
34+
return b, h, nil
35+
}
36+
37+
var res []oldEndpointResponse
38+
39+
for _, endpointPayload := range endpoints {
40+
endpointBytes, err := json.Marshal(endpointPayload)
41+
if err != nil {
42+
return nil, nil, err
43+
}
44+
45+
var endpointResp models.EndpointResponse
46+
err = json.Unmarshal(endpointBytes, &endpointResp)
47+
if err != nil {
48+
return nil, nil, err
49+
}
50+
51+
var old oldEndpointResponse
52+
endpoint := endpointResp.Endpoint
53+
54+
err = migrateEndpoint(&endpoint, &old)
55+
if err != nil {
56+
return nil, nil, err
57+
}
58+
59+
res = append(res, old)
60+
}
61+
62+
pResp.Content = res
63+
b, err = json.Marshal(pResp)
64+
if err != nil {
65+
return nil, nil, err
66+
}
67+
68+
serverResponse.Data = json.RawMessage(b)
69+
sb, err := json.Marshal(serverResponse)
70+
if err != nil {
71+
return nil, nil, err
72+
}
73+
74+
return sb, h, nil
75+
}

0 commit comments

Comments
 (0)