From ad6da651bf3980572db65e1e5afb1b587b3d9389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Darko=20Kri=C5=BEi=C4=87?= Date: Wed, 22 May 2024 16:50:21 +0200 Subject: [PATCH] Code that works --- github/delete.go | 35 +++++++++++++++++++---------------- github/user.go | 20 ++++++++++++++++---- sync/sync.go | 8 ++++++-- 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/github/delete.go b/github/delete.go index ac3c59c..344fd17 100644 --- a/github/delete.go +++ b/github/delete.go @@ -7,25 +7,28 @@ import ( "log/slog" ) -func (g GitHub) DeleteUser(ctx context.Context, login string) error { - slog.Info("Deleting user", "login", login, "enterprise", g.config.Enterprise) +func (g GitHub) DeleteUser(ctx context.Context, userId string) error { + enterpriseId := g.enterpriseId + slog.Info("Deleting user", "userId", userId, "enterprise", g.config.Enterprise, "enterpriseId", g.enterpriseId) var mutation struct { RemoveEnterpriseMember struct { - Input struct { - ClientMutationId string - EnterpriseId string - userId string + ClientMutationId string + Enterprise struct { + ID string } - } `graphql:"removeEnterpriseMember(input: $input)"` + User struct { + ID string + } + Viewer struct { + ID string + } + } `graphql:"removeEnterpriseMember(input:$input)"` } - input := map[string]interface{}{ - "input": map[string]interface{}{ - "clientMutationId": "delete-user", - "enterpriseId": g.config.Enterprise, - "userId": login, - }, + input := githubv4.RemoveEnterpriseMemberInput{ + EnterpriseID: githubv4.ID(enterpriseId), + UserID: githubv4.ID(userId), } src := oauth2.StaticTokenSource( @@ -36,10 +39,10 @@ func (g GitHub) DeleteUser(ctx context.Context, login string) error { err := client.Mutate(ctx, &mutation, input, nil) if err != nil { - slog.Warn("Unable to delete user", "login", login, "enterprise", g.config.Enterprise, "error", err) - return err + slog.Warn("Unable to delete user", "userId", userId, "enterprise", g.config.Enterprise, "error", err) + return nil } - slog.Info("User deleted", "login", login, "enterprise", g.config.Enterprise) + slog.Info("User deleted", "userId", userId, "enterprise", g.config.Enterprise) return nil } diff --git a/github/user.go b/github/user.go index 93c9af6..7050d2c 100644 --- a/github/user.go +++ b/github/user.go @@ -15,12 +15,14 @@ type Config struct { } type GitHub struct { - config Config - client *github.Client - userlist GitHubUsers + config Config + client *github.Client + userlist GitHubUsers + enterpriseId string } type GitHubUser struct { + ID string Login string Email string } @@ -36,7 +38,7 @@ func New(ctx context.Context, config Config) (*GitHub, error) { return &gh, nil } -func (g GitHub) Users(ctx context.Context) ([]GitHubUser, error) { +func (g *GitHub) Users(ctx context.Context) ([]GitHubUser, error) { if g.userlist == nil { err := g.loadMembers(ctx) if err != nil { @@ -62,6 +64,7 @@ func (g *GitHub) loadMembers(ctx context.Context) error { var query struct { Enterprise struct { + Id string Slug string Name string OwnerInfo struct { @@ -74,6 +77,7 @@ func (g *GitHub) loadMembers(ctx context.Context) error { Edges []struct { Node struct { User struct { + ID string Login string Name string ContributionsCollection struct { @@ -108,11 +112,15 @@ func (g *GitHub) loadMembers(ctx context.Context) error { return err } + g.enterpriseId = query.Enterprise.Id + for _, e := range query.Enterprise.OwnerInfo.SamlIdentityProvider.ExternalIdentities.Edges { slog.Debug("GitHub user", + "id", e.Node.User.ID, "login", e.Node.User.Login, "email", e.Node.SamlIdentity.NameId) u := GitHubUser{ + ID: e.Node.User.ID, Login: e.Node.User.Login, Email: e.Node.SamlIdentity.NameId, } @@ -131,3 +139,7 @@ func (g *GitHub) loadMembers(ctx context.Context) error { slog.InfoContext(ctx, "Loaded userlist", "users", len(g.userlist)) return nil } + +func (g GitHub) EnterpriseId() string { + return g.enterpriseId +} diff --git a/sync/sync.go b/sync/sync.go index d28fea6..760de46 100644 --- a/sync/sync.go +++ b/sync/sync.go @@ -20,6 +20,7 @@ type Action struct { displayName string email string login string + id string } func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { @@ -44,6 +45,7 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { slog.Debug("User not in Azure", "login", githubUser.Login, "email", githubUser.Email) action := &Action{ actionType: Delete, + id: githubUser.ID, email: githubUser.Email, login: githubUser.Login, } @@ -70,11 +72,13 @@ func Sync(ctx context.Context, az azure.Azure, gh github.GitHub) (err error) { slog.Info("Deleting user", "login", a.login, + "userId", a.id, "email", a.email, "name", a.displayName) - err = gh.DeleteUser(ctx, a.login) + err = gh.DeleteUser(ctx, a.id) if err != nil { - return err + continue + // return err } } }