Skip to content

Commit

Permalink
Merge pull request #4 from PRODYNA/2-implement-sync-invite-if-user-is…
Browse files Browse the repository at this point in the history
…-member-of-the-group

First code to find users to invite
  • Loading branch information
dkrizic authored May 23, 2024
2 parents a07662d + 7edc5b0 commit c02c481
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 5 additions & 0 deletions github/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
50 changes: 49 additions & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"github.com/prodyna/sync-enterprise/azure"
"github.com/prodyna/sync-enterprise/github"
"log/slog"
"strings"
)

type ActionType int

const (
// Delete represents a delete action
Delete ActionType = iota
Invite ActionType = iota
)

// Action represents a delete action
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit c02c481

Please sign in to comment.