Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Better list generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed May 8, 2024
1 parent 6c15af7 commit c7be9fe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
11 changes: 5 additions & 6 deletions azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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
}
5 changes: 4 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand Down
41 changes: 28 additions & 13 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -34,39 +33,55 @@ 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++
}
}

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
}

0 comments on commit c7be9fe

Please sign in to comment.