Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for ZEP source #215

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
14 changes: 10 additions & 4 deletions internal/adapter/google/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,24 @@ type CalendarAPI struct {
// Assert that the expected interfaces are implemented
var _ port.Configurable = &CalendarAPI{}
var _ port.LogSetter = &CalendarAPI{}
var _ port.CalendarIDSetter = &CalendarAPI{}
var _ port.OAuth2Adapter = &CalendarAPI{}

func (c *CalendarAPI) SetCalendarID(calendarID string) error {
if calendarID == "" {
return fmt.Errorf("adapter (%s) 'calendar' cannot be empty", c.Name())
}
c.calendarID = calendarID
return nil
}

func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credentials, storage auth.Storage, bindPort uint) error {
// Google Adapter does not need the tenantId
switch {
case credentials.Client.Id == "":
return fmt.Errorf("%s adapter oAuth2 'clientID' cannot be empty", c.Name())
case credentials.Client.Secret == "":
return fmt.Errorf("oAuth2 adapter (%s) 'clientSecret' cannot be empty", c.Name())
case credentials.CalendarId == "":
return fmt.Errorf("oAuth2 adapter (%s) 'calendar' cannot be empty", c.Name())
}

oauthListener, err := auth.NewOAuthHandler(oauth2.Config{
Expand All @@ -72,10 +79,9 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
}

c.oAuthHandler = oauthListener
c.calendarID = credentials.CalendarId
c.storage = storage

storedAuth, err := c.storage.ReadCalendarAuth(credentials.CalendarId)
storedAuth, err := c.storage.ReadCalendarAuth(c.calendarID)
if err != nil {
return err
}
Expand Down
14 changes: 9 additions & 5 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,25 @@ type CalendarAPI struct {
// Assert that the expected interfaces are implemented
var _ port.Configurable = &CalendarAPI{}
var _ port.LogSetter = &CalendarAPI{}
var _ port.CalendarIDSetter = &CalendarAPI{}
var _ port.OAuth2Adapter = &CalendarAPI{}

func (c *CalendarAPI) SetCalendarID(calendarID string) error {
if calendarID == "" {
return fmt.Errorf("%s adapter 'calendar' cannot be empty", c.Name())
}
return nil
}

func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credentials, storage auth.Storage, bindPort uint) error {
// Outlook Adapter does not need the clientKey
switch {
case credentials.Client.Id == "":
return fmt.Errorf("%s adapter oAuth2 'clientId' cannot be empty", c.Name())
case credentials.Tenant.Id == "":
return fmt.Errorf("%s adapter oAuth2 'tenantId' cannot be empty", c.Name())
case credentials.CalendarId == "":
return fmt.Errorf("%s adapter oAuth2 'calendar' cannot be empty", c.Name())
}

c.calendarID = credentials.CalendarId

endpoint := oauth2.Endpoint{
AuthURL: fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/v2.0/authorize", credentials.Tenant.Id),
TokenURL: fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/v2.0/token", credentials.Tenant.Id),
Expand All @@ -81,7 +85,7 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
c.storage = storage
c.oAuthConfig = &oAuthConfig

storedAuth, err := c.storage.ReadCalendarAuth(credentials.CalendarId)
storedAuth, err := c.storage.ReadCalendarAuth(c.calendarID)
if err != nil {
return err
}
Expand Down
5 changes: 5 additions & 0 deletions internal/adapter/port/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type LogSetter interface {
SetLogger(logger *log.Logger)
}

// CalendarIDSetter can be implemented by a struct to allow setting the calendar ID
type CalendarIDSetter interface {
timonegk marked this conversation as resolved.
Show resolved Hide resolved
SetCalendarID(calendarID string) error
}

// Configurable is an interface which defines how arbitrary configuration data can be passed
// to a struct which implements this interface. Clients should be configurable.
type Configurable interface {
Expand Down
7 changes: 6 additions & 1 deletion internal/adapter/sink_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser bo
c.SetLogger(logger)
}

if c, ok := client.(port.CalendarIDSetter); ok {
if err := c.SetCalendarID(config.Adapter().Calendar); err != nil {
return nil, err
}
}

if c, ok := client.(port.OAuth2Adapter); ok {
if err := c.SetupOauth2(ctx,
auth.Credentials{
Expand All @@ -55,7 +61,6 @@ func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser bo
Tenant: auth.Tenant{
Id: config.Adapter().OAuth.TenantID,
},
CalendarId: config.Adapter().Calendar,
},
storage,
bindPort,
Expand Down
7 changes: 6 additions & 1 deletion internal/adapter/source_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser
c.SetLogger(logger)
}

if c, ok := client.(port.CalendarIDSetter); ok {
if err := c.SetCalendarID(config.Adapter().Calendar); err != nil {
return nil, err
}
}

if c, ok := client.(port.OAuth2Adapter); ok {
if err := c.SetupOauth2(ctx,
auth.Credentials{
Expand All @@ -59,7 +65,6 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser
Tenant: auth.Tenant{
Id: config.Adapter().OAuth.TenantID,
},
CalendarId: config.Adapter().Calendar,
},
storage,
bindPort,
Expand Down
16 changes: 13 additions & 3 deletions internal/adapter/zep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ type CalendarAPI struct {

// Assert that the expected interfaces are implemented
var _ port.Configurable = &CalendarAPI{}
var _ port.CalendarIDSetter = &CalendarAPI{}

func (zep *CalendarAPI) SetCalendarID(calendarID string) error {
if calendarID == "" {
return fmt.Errorf("%s adapter 'calendar' cannot be empty", zep.Name())
}
zep.calendarID = calendarID
return nil
}

func (zep *CalendarAPI) GetCalendarHash() string {
var id []byte
Expand Down Expand Up @@ -89,20 +98,21 @@ func (zep *CalendarAPI) Initialize(ctx context.Context, openBrowser bool, config
}

func (zep *CalendarAPI) EventsInTimeframe(ctx context.Context, start time.Time, end time.Time) ([]models.Event, error) {
absences, err := zep.ListEvents(start, end)
events, err := zep.ListEvents(start, end)
if err != nil {
return nil, fmt.Errorf("could not get absences %w", err)
return nil, fmt.Errorf("could not get zep events %w", err)
}

var syncEvents []models.Event
for _, v := range absences {
for _, v := range events {
syncEvents = append(syncEvents,
models.Event{
ICalUID: v.ID,
Title: v.Summary,
Description: v.Description,
StartTime: v.Start,
EndTime: v.End,
AllDay: isAllDayEvent(v),
Accepted: true,
Metadata: models.NewEventMetadata(v.ID, "", zep.GetCalendarHash()),
})
Expand Down
14 changes: 13 additions & 1 deletion internal/adapter/zep/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

const (
DateFormat = "02.01.2006"
DateFormat = "02.01.2006 15:04"
)

// Event is a simplified representation for an event which has been entered in ZEP.
Expand All @@ -23,3 +23,15 @@ type Event struct {
func (a Event) String() string {
return fmt.Sprintf("%s | %s | %s | %s | %s | %s", a.ID, a.Start.Format(DateFormat), a.End.Format(DateFormat), a.Category, a.Summary, a.Description)
}

func isAllDayEvent(event Event) bool {
h, m, s := event.Start.Clock()
if h != 0 || m != 0 || s != 0 {
return false
}
h, m, s = event.End.Clock()
if h != 0 || m != 0 || s != 0 {
return false
}
return true
}
5 changes: 2 additions & 3 deletions internal/auth/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package auth

type Credentials struct {
Client Client
Tenant Tenant
CalendarId string
Client Client
Tenant Tenant
}

type Client struct {
Expand Down
Loading