Skip to content

Commit

Permalink
Merge pull request #213 from inovex/rename-calendarhash
Browse files Browse the repository at this point in the history
refactor: rename GetCalendarID to GetCalendarHash
  • Loading branch information
MichaelEischer authored Jan 8, 2025
2 parents 6ea591b + b14b32c commit dd92780
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 68 deletions.
8 changes: 4 additions & 4 deletions internal/adapter/google/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type GoogleCalendarClient interface {
CreateEvent(ctx context.Context, event models.Event) error
UpdateEvent(ctx context.Context, event models.Event) error
DeleteEvent(ctx context.Context, event models.Event) error
GetCalendarID() string
GetCalendarHash() string
InitGoogleCalendarClient(calId string, log *log.Logger) error
}

Expand Down Expand Up @@ -226,12 +226,12 @@ func (c *CalendarAPI) Name() string {
return "Google Calendar"
}

// GetCalendarID calculates a unique ID for this adapter based on the current calendar.
// GetCalendarHash calculates a unique hash for this adapter based on the current calendar.
// This is used to distinguish between adapters in order to not overwrite or delete events
// which are maintained by different adapters.
// A simple use-case for this is if you have multiple google calendars as source adapters configured.
func (c *CalendarAPI) GetCalendarID() string {
return c.gcalClient.GetCalendarID()
func (c *CalendarAPI) GetCalendarHash() string {
return c.gcalClient.GetCalendarHash()
}

func (c *CalendarAPI) SetLogger(logger *log.Logger) {
Expand Down
8 changes: 4 additions & 4 deletions internal/adapter/google/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *GCalClient) ListEvents(ctx context.Context, starttime time.Time, endtim

var loadedEvents []models.Event
for _, event := range eventList.Items {
loadedEvents = append(loadedEvents, calendarEventToEvent(event, g.GetCalendarID()))
loadedEvents = append(loadedEvents, calendarEventToEvent(event, g.GetCalendarHash()))
}

// if the responses 'nextPageToken' is set, the result is paginated and more data to be loaded recursively
Expand All @@ -80,7 +80,7 @@ func (g *GCalClient) ListEvents(ctx context.Context, starttime time.Time, endtim
return nil, err
}
for _, pageEvent := range eventList.Items {
loadedEvents = append(loadedEvents, calendarEventToEvent(pageEvent, g.GetCalendarID()))
loadedEvents = append(loadedEvents, calendarEventToEvent(pageEvent, g.GetCalendarHash()))
}
}
return loadedEvents, nil
Expand Down Expand Up @@ -216,11 +216,11 @@ func (g *GCalClient) loadPages(listCall *calendar.EventsListCall, events *[]*cal
return g.loadPages(listCall, events, pageEvents.NextPageToken)
}

// GetCalendarID calculates a unique ID for this adapter based on the current calendar.
// GetCalendarHash calculates a unique ID for this adapter based on the current calendar.
// This is used to distinguish between adapters in order to not overwrite or delete events
// which are maintained by different adapters.
// A simple use-case for this is if you have multiple google calendars as source adapters configured.
func (g *GCalClient) GetCalendarID() string {
func (g *GCalClient) GetCalendarHash() string {
var id []byte

sum := sha1.Sum([]byte(g.CalendarId))
Expand Down
6 changes: 3 additions & 3 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type OutlookCalendarClient interface {
CreateEvent(ctx context.Context, event models.Event) error
UpdateEvent(ctx context.Context, event models.Event) error
DeleteEvent(ctx context.Context, event models.Event) error
GetCalendarID() string
GetCalendarHash() string
}

type CalendarAPI struct {
Expand Down Expand Up @@ -245,8 +245,8 @@ func (c *CalendarAPI) DeleteEvent(ctx context.Context, e models.Event) error {
return nil
}

func (c *CalendarAPI) GetCalendarID() string {
return c.outlookClient.GetCalendarID()
func (c *CalendarAPI) GetCalendarHash() string {
return c.outlookClient.GetCalendarHash()
}

func (c *CalendarAPI) Name() string {
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/outlook_http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (o *OutlookClient) ListEvents(ctx context.Context, start time.Time, end tim

var events []models.Event
for _, evt := range eventList.Events {
evt, err := o.outlookEventToEvent(evt, o.GetCalendarID())
evt, err := o.outlookEventToEvent(evt, o.GetCalendarHash())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func (o *OutlookClient) DeleteEvent(ctx context.Context, event models.Event) err
return nil
}

func (o OutlookClient) GetCalendarID() string {
func (o OutlookClient) GetCalendarHash() string {
var id []byte
sum := sha1.Sum([]byte(o.CalendarID))
id = append(id, sum[:]...)
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/sink_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ func (a SinkAdapter) EventsInTimeframe(ctx context.Context, start time.Time, end
return a.client.EventsInTimeframe(ctx, start, end)
}

func (a SinkAdapter) GetCalendarID() string {
return a.client.GetCalendarID()
func (a SinkAdapter) GetCalendarHash() string {
return a.client.GetCalendarHash()
}
4 changes: 2 additions & 2 deletions internal/adapter/source_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (a SourceAdapter) Name() string {
func (a SourceAdapter) CalendarID() string {
return a.calendarID
}
func (a SourceAdapter) GetCalendarID() string {
return a.client.GetCalendarID()
func (a SourceAdapter) GetCalendarHash() string {
return a.client.GetCalendarHash()
}

func (a SourceAdapter) EventsInTimeframe(ctx context.Context, start time.Time, end time.Time) ([]models.Event, error) {
Expand Down
9 changes: 2 additions & 7 deletions internal/adapter/zep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@ type CalendarAPI struct {
// Assert that the expected interfaces are implemented
var _ port.Configurable = &CalendarAPI{}

func (zep *CalendarAPI) GetCalendarID() string {
return zep.generateCalendarID()
}

func (zep *CalendarAPI) generateCalendarID() string {
func (zep *CalendarAPI) GetCalendarHash() string {
var id []byte
components := []string{zep.username, zep.homeSet, zep.calendarID}

sum := sha1.Sum([]byte(strings.Join(components, "")))
id = append(id, sum[:]...)
return base64.URLEncoding.EncodeToString(id)

}

func (zep *CalendarAPI) Name() string {
Expand Down Expand Up @@ -109,7 +104,7 @@ func (zep *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time,
StartTime: v.Start,
EndTime: v.End,
Accepted: true,
Metadata: models.NewEventMetadata(v.ID, "", zep.GetCalendarID()),
Metadata: models.NewEventMetadata(v.ID, "", zep.GetCalendarHash()),
})
}

Expand Down
2 changes: 1 addition & 1 deletion internal/models/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Metadata struct {
SyncID string `json:"SyncID"`
// OriginalEventUri is an URI which points to the original event which was synced. This is usually an URL.
OriginalEventUri string `json:"OriginalEventUri"`
// SourceID contains the ID of the source which this event was imported from
// SourceID contains the unique hash of the source which this event was imported from
SourceID string `json:"SourceID"`
}

Expand Down
10 changes: 5 additions & 5 deletions internal/sync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (p Controller) CleanUp(ctx context.Context, start time.Time, end time.Time)
for _, event := range sink {
// Check if the sink event was synced by us, if there's no metadata the event may
// be there because we were invited or because it is not managed by us
if event.Metadata.SourceID == p.source.GetCalendarID() {
if event.Metadata.SourceID == p.source.GetCalendarHash() {
// redefine to let the closure capture individual variables
event := event
tasks = append(tasks, func() error {
Expand Down Expand Up @@ -201,18 +201,18 @@ func (p Controller) diffEvents(sourceEvents []models.Event, sinkEvents []models.
// - Run sync from calendar B to calendar A. This will copy (and thereby resurrect) the event.
//
// Solution: Ignore events the originate from the sink, but no longer exist there.
if event.Metadata.SourceID == p.sink.GetCalendarID() {
if event.Metadata.SourceID == p.sink.GetCalendarHash() {
p.logger.Info("skipping event as it originates from the sink, but no longer exists there", logFields(event)...)
continue
}
p.logger.Info("new event, needs sync", logFields(event)...)
createEvents = append(createEvents, event)

case sinkEvent.Metadata.SourceID != p.source.GetCalendarID():
case sinkEvent.Metadata.SourceID != p.source.GetCalendarHash():
p.logger.Info("event was not synced by this source adapter, skipping", logFields(event)...)

// Only update the event if the event differs AND we synced it prior and set the correct metadata
case !models.IsSameEvent(event, sinkEvent) && sinkEvent.Metadata.SourceID == p.source.GetCalendarID():
case !models.IsSameEvent(event, sinkEvent) && sinkEvent.Metadata.SourceID == p.source.GetCalendarHash():
p.logger.Info("event content changed, needs sync", logFields(event)...)
updateEvents = append(updateEvents, sinkEvent.Overwrite(event))

Expand All @@ -231,7 +231,7 @@ func (p Controller) diffEvents(sourceEvents []models.Event, sinkEvents []models.
case exists:
// Nothing to do

case event.Metadata.SourceID == p.source.GetCalendarID():
case event.Metadata.SourceID == p.source.GetCalendarHash():
p.logger.Info("sinkEvent is not (anymore) in sourceEvents, marked for removal", logFields(event)...)
deleteEvents = append(deleteEvents, event)

Expand Down
28 changes: 14 additions & 14 deletions internal/sync/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func (suite *ControllerTestSuite) TestDryRun() {
suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(sourceEvents, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(sinkEvents, nil)
suite.sink.On("DeleteEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.source.On("GetCalendarID").Return("sourceID")
suite.sink.On("GetCalendarHash").Return("sinkID")
suite.source.On("GetCalendarHash").Return("sourceID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, true)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -176,8 +176,8 @@ func (suite *ControllerTestSuite) TestCleanUp() {
suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(sourceEvents, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(sinkEvents, nil)
suite.sink.On("DeleteEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.source.On("GetCalendarID").Return("sourceID")
suite.sink.On("GetCalendarHash").Return("sinkID")
suite.source.On("GetCalendarHash").Return("sourceID")

err := suite.controller.CleanUp(ctx, startTime, endTime)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -222,7 +222,7 @@ func (suite *ControllerTestSuite) TestCreateEventsEmptySink() {
suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(eventsToCreate, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(nil, nil)
suite.sink.On("CreateEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.sink.On("GetCalendarHash").Return("sinkID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, false)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -307,8 +307,8 @@ func (suite *ControllerTestSuite) TestDeleteEventsNotInSink() {
suite.sink.On("DeleteEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
// UpdateEvent gets called because the remaining event in the sink will get updated because there are no transformers configured
suite.sink.On("UpdateEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.source.On("GetCalendarID").Return("sourceID")
suite.sink.On("GetCalendarHash").Return("sinkID")
suite.source.On("GetCalendarHash").Return("sourceID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, false)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -344,8 +344,8 @@ func (suite *ControllerTestSuite) TestDoNotResurrectEvents() {

suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(sourceEvents, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(sinkEvents, nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.source.On("GetCalendarID").Return("sourceID")
suite.sink.On("GetCalendarHash").Return("sinkID")
suite.source.On("GetCalendarHash").Return("sourceID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, false)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -478,8 +478,8 @@ func (suite *ControllerTestSuite) TestUpdateEventsPrefilledSink() {
suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(sourceEvents, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(sinkEvents, nil)
suite.sink.On("UpdateEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.source.On("GetCalendarID").Return("sourceID")
suite.sink.On("GetCalendarHash").Return("sinkID")
suite.source.On("GetCalendarHash").Return("sourceID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, false)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -525,7 +525,7 @@ func (suite *ControllerTestSuite) TestCreateEventsDeclined() {
suite.source.On("EventsInTimeframe", ctx, startTime, endTime).Return(eventsToCreate, nil)
suite.sink.On("EventsInTimeframe", ctx, startTime, endTime).Return(nil, nil)
suite.sink.On("CreateEvent", ctx, mock.AnythingOfType("models.Event")).Return(nil)
suite.sink.On("GetCalendarID").Return("sinkID")
suite.sink.On("GetCalendarHash").Return("sinkID")

err := suite.controller.SynchroniseTimeframe(ctx, startTime, endTime, false)
assert.NoError(suite.T(), err)
Expand Down Expand Up @@ -769,10 +769,10 @@ func TestController_diffEvents(t *testing.T) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var source mocks.Source
source.On("GetCalendarID").Return("sourceID")
source.On("GetCalendarHash").Return("sourceID")

var sink mocks.Sink
sink.On("GetCalendarID").Return("sinkID")
sink.On("GetCalendarHash").Return("sinkID")

var controller = Controller{
source: &source,
Expand Down
22 changes: 11 additions & 11 deletions internal/sync/mocks/Sink.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions internal/sync/mocks/Source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit dd92780

Please sign in to comment.