From c7be9fe02134df454dd7ca45f21a112c61d3cd31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Wed, 8 May 2024 22:50:33 +0200 Subject: [PATCH] Better list generation --- azure/azure.go | 11 +++++------ github/github.go | 5 ++++- sync/sync.go | 41 ++++++++++++++++++++++++++++------------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/azure/azure.go b/azure/azure.go index f7e8464..364efd5 100644 --- a/azure/azure.go +++ b/azure/azure.go @@ -78,7 +78,6 @@ func (az *Azure) Users(ctx context.Context) ([]AzureUser, error) { if err != nil { return nil, fmt.Errorf("error getting group members: %w", err) } - slog.Info("result", slog.Any("result", result)) pageIterator, err := msgraphgocore.NewPageIterator[*models.User](result, az.azclient.GetAdapter(), models.CreateUserCollectionResponseFromDiscriminatorValue) if err != nil { @@ -87,7 +86,7 @@ func (az *Azure) Users(ctx context.Context) ([]AzureUser, error) { err = pageIterator.Iterate(ctx, func(user *models.User) bool { if user != nil { - slog.Info("Azure group member", + slog.Debug("Azure group member", "email", *user.GetMail(), "displayName", *user.GetDisplayName()) users = append(users, AzureUser{ @@ -104,17 +103,17 @@ func (az *Azure) Users(ctx context.Context) ([]AzureUser, error) { return az.users, nil } -func (az *Azure) IsUserInGroup(ctx context.Context, email string) (bool, error) { +func (az *Azure) IsUserInGroup(ctx context.Context, email string) (isInGroup bool, displayName *string, err error) { users, err := az.Users(ctx) if err != nil { - return false, err + return false, nil, err } for _, user := range users { if user.Email == email { - return true, nil + return true, &user.DisplayName, nil } } - return false, nil + return false, nil, nil } diff --git a/github/github.go b/github/github.go index 900cfe2..5ccf2a6 100644 --- a/github/github.go +++ b/github/github.go @@ -46,7 +46,7 @@ func (g GitHub) Users(ctx context.Context) ([]GitHubUser, error) { return g.userlist, nil } -func (g GitHub) DeleteUser(user GitHubUser) error { +func (g GitHub) DeleteUser(login string) error { return nil } @@ -113,6 +113,9 @@ func (g *GitHub) loadMembers(ctx context.Context) error { } for _, e := range query.Enterprise.OwnerInfo.SamlIdentityProvider.ExternalIdentities.Edges { + slog.Debug("GitHub user", + "login", e.Node.User.Login, + "email", e.Node.SamlIdentity.NameId) u := GitHubUser{ Login: e.Node.User.Login, Email: e.Node.SamlIdentity.NameId, diff --git a/sync/sync.go b/sync/sync.go index fd58e43..b378cae 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -10,17 +10,16 @@ import ( type ActionType int const ( - // nothing represents no action - Nothing ActionType = iota // Delete represents a delete action Delete ActionType = iota ) // Action represents a delete action type Action struct { - actionType ActionType - azureUser azure.AzureUser - githubUser github.GitHubUser + actionType ActionType + displayName string + email string + login string } func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { @@ -34,19 +33,27 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { slog.Info("Checking if github users are in Azure group", "count", len(githubUsers), "group", az.Config.AzureGroup) for _, githubUser := range githubUsers { + slog.Debug("Checking user", "login", githubUser.Login, "email", githubUser.Email) // check if user is in azure - inAzure, err := az.IsUserInGroup(ctx, githubUser.Email) + inAzure, name, err := az.IsUserInGroup(ctx, githubUser.Email) if err != nil { return err } if !inAzure { - actions = append(actions, Action{ + slog.Debug("User not in Azure", "login", githubUser.Login, "email", githubUser.Email) + action := &Action{ actionType: Delete, - githubUser: githubUser, - }) + email: githubUser.Email, + login: githubUser.Login, + } + if name != nil { + action.displayName = *name + } + actions = append(actions, *action) delete++ } else { + slog.Debug("User in Azure", "login", githubUser.Login, "email", githubUser.Email, "name", *name) stay++ } } @@ -54,19 +61,27 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { for _, a := range actions { if a.actionType == Delete { if gh.DryRun() { - slog.Info("Would delete user", "login", a.githubUser.Login, "email", a.githubUser.Email) + slog.Info("Dry-run, would delete user", + "login", a.login, + "email", a.email, + "name", a.displayName) continue } - slog.Info("Deleting user", "user", "login", a.githubUser.Login, "email", a.githubUser.Email) - err = gh.DeleteUser(a.githubUser) + slog.Info("Deleting user", + "login", a.login, + "email", a.email, + "name", a.displayName) + err = gh.DeleteUser(a.login) if err != nil { return err } } } - slog.Info("Sync finished", "delete", delete, "leave", stay) + slog.Info("Sync finished", + "delete", delete, + "stay", stay) return nil }