diff --git a/github/user.go b/github/user.go index 7050d2c..3cb301e 100644 --- a/github/user.go +++ b/github/user.go @@ -143,3 +143,8 @@ func (g *GitHub) loadMembers(ctx context.Context) error { func (g GitHub) EnterpriseId() string { return g.enterpriseId } + +func (g GitHub) InviteUser(ctx context.Context, email string, name string) error { + slog.WarnContext(ctx, "Method not implemented", "email", email, "name", name, "enterprise", g.config.Enterprise) + return nil +} diff --git a/sync/sync.go b/sync/sync.go index f92f555..881a39f 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -5,6 +5,7 @@ import ( "github.com/prodyna/sync-enterprise/azure" "github.com/prodyna/sync-enterprise/github" "log/slog" + "strings" ) type ActionType int @@ -12,6 +13,7 @@ type ActionType int const ( // Delete represents a delete action Delete ActionType = iota + Invite ActionType = iota ) // Action represents a delete action @@ -27,6 +29,7 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { slog.Info("Syncing users") actions := []Action{} delete := 0 + invite := 0 stay := 0 // load github users @@ -60,8 +63,52 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { } } + slog.Info("Checking if Azure is is already in GitHub") + azureUsers, err := az.Users(ctx) + if err != nil { + return err + } + for _, azureUser := range azureUsers { + slog.Debug("Checking user", "email", azureUser.Email, "name", azureUser.DisplayName) + found := false + for _, githubUser := range githubUsers { + if strings.ToLower(githubUser.Email) == strings.ToLower(azureUser.Email) { + found = true + break + } + } + + if !found { + slog.Debug("User not in GitHub", "email", azureUser.Email, "name", azureUser.DisplayName) + action := &Action{ + actionType: Invite, + email: azureUser.Email, + displayName: azureUser.DisplayName, + } + invite++ + actions = append(actions, *action) + } + } + for _, a := range actions { - if a.actionType == Delete { + switch a.actionType { + case Invite: + if gh.DryRun() { + slog.Info("Dry-run, would invite user", + "email", a.email, + "name", a.displayName) + continue + } else { + slog.Info("Inviting user", + "email", a.email, + "name", a.displayName) + err = gh.InviteUser(ctx, a.email, a.displayName) + if err != nil { + continue + // return err + } + } + case Delete: if gh.DryRun() { slog.Info("Dry-run, would delete user", "login", a.login, @@ -85,6 +132,7 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { slog.Info("Sync finished", "delete", delete, + "invite", invite, "stay", stay) return nil