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

Commit

Permalink
Merge pull request #10 from PRODYNA/8-actually-delete-persons
Browse files Browse the repository at this point in the history
8 actually delete persons
  • Loading branch information
dkrizic authored May 22, 2024
2 parents b34ccd3 + ad6da65 commit e1c9b55
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
35 changes: 19 additions & 16 deletions github/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
20 changes: 16 additions & 4 deletions github/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
}
8 changes: 6 additions & 2 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
}
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit e1c9b55

Please sign in to comment.