From d382891dc50d24cf13770345d05dba89a9997629 Mon Sep 17 00:00:00 2001 From: Jerrico Dela Cruz <94591636+jerricotandelacruz@users.noreply.github.com> Date: Tue, 19 Nov 2024 22:07:41 +0800 Subject: [PATCH] feat: reassociate gh account (backend) Signed-off-by: Jerrico Dela Cruz <94591636+jerricotandelacruz@users.noreply.github.com> --- src/goapp/pkg/github/github.go | 51 +++++++++++++++++++++++ src/goapp/routes/api/github.go | 2 +- src/goapp/routes/login/github/callback.go | 16 +++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/goapp/pkg/github/github.go b/src/goapp/pkg/github/github.go index aaea3e79..ba2313db 100644 --- a/src/goapp/pkg/github/github.go +++ b/src/goapp/pkg/github/github.go @@ -632,6 +632,39 @@ func GetRepositoryProjects(owner string, name string, token string) (*GetReposit return &result, nil } +func GetUserByLogin(login string, token string) (*GetUserByLoginResult, error) { + src := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + httpClient := oauth2.NewClient(context.Background(), src) + httpClient.Transport = &customTransport{Transport: httpClient.Transport} + + client := githubv4.NewClient(httpClient) + + var result GetUserByLoginResult + var queryResult GetUserByLoginQuery + + variables := map[string]interface{}{ + "login": githubv4.String(login), + } + err := client.Query(context.Background(), &queryResult, variables) + if err != nil { + return nil, err + } + + if queryResult.User.ID == nil { + return nil, fmt.Errorf("node ID of %s is not found", queryResult.User.Login) + } + + result.User = User{ + Id: queryResult.User.ID.(string), + DatabaseId: int64(queryResult.User.DatabaseId), + Login: string(queryResult.User.Login), + } + + return &result, nil +} + // Query structs type GetOrganizationsWithinEnterpriseQuery struct { Enterprise struct { @@ -683,6 +716,14 @@ type GetRepositoryProjectsQuery struct { } `graphql:"repository(owner: $owner, name: $name)"` } +type GetUserByLoginQuery struct { + User struct { + ID githubv4.ID + DatabaseId githubv4.Int + Login githubv4.String + } `graphql:"user(login: $login)"` +} + type PageInfo struct { EndCursor githubv4.String HasNextPage bool @@ -702,6 +743,10 @@ type GetRepositoryProjectsResult struct { Projects []Project } +type GetUserByLoginResult struct { + User +} + // Structs type Member struct { Id string @@ -722,3 +767,9 @@ type Project struct { CreatedAt time.Time UpdatedAt time.Time } + +type User struct { + Id string + DatabaseId int64 + Login string +} diff --git a/src/goapp/routes/api/github.go b/src/goapp/routes/api/github.go index 59048f1b..85fc7be5 100644 --- a/src/goapp/routes/api/github.go +++ b/src/goapp/routes/api/github.go @@ -547,7 +547,7 @@ func ProcessCleanupEnterpriseOrgs(enterpriseMembers *ghAPI.GetMembersByEnterpris muRAD.Lock() removeMembers = append(removeMembers, fmt.Sprintln(member.Username, " - ", member.Email)) muRAD.Unlock() - if ev.GetEnvVar("ENABLED_REMOVE_COLLABORATORS", "false") == "true" { + if ev.GetEnvVar("ENABLED_REMOVE_COLLABORATORS", "false") == "true" && ev.GetEnvVar("ENABLED_REMOVE_ENTERPRISE_MEMBER", "false") == "true" { token := os.Getenv("GH_TOKEN") enterpriseId := os.Getenv("GH_ENTERPRISE_ID") err := ghAPI.RemoveEnterpriseMember(token, enterpriseId, member.NodeId) diff --git a/src/goapp/routes/login/github/callback.go b/src/goapp/routes/login/github/callback.go index ef35e14b..98161c75 100644 --- a/src/goapp/routes/login/github/callback.go +++ b/src/goapp/routes/login/github/callback.go @@ -12,6 +12,7 @@ import ( auth "main/pkg/authentication" "main/pkg/email" + ev "main/pkg/envvar" db "main/pkg/ghmgmtdb" ghAPI "main/pkg/github" "main/pkg/msgraph" @@ -160,6 +161,21 @@ func GithubForceSaveHandler(w http.ResponseWriter, r *http.Request) { ghId := strconv.FormatFloat(p["id"].(float64), 'f', 0, 64) ghUser := fmt.Sprintf("%s", p["login"]) + if ev.GetEnvVar("ENABLED_REMOVE_ENTERPRISE_MEMBER", "false") == "true" { + user, err := ghAPI.GetUserByLogin(ghUser, os.Getenv("GH_TOKEN")) + if err != nil { + log.Println(err.Error()) + } + enterpriseToken := os.Getenv("GH_ENTERPRISE_TOKEN") + enterpriseId := os.Getenv("GH_ENTERPRISE_ID") + err = ghAPI.RemoveEnterpriseMember(enterpriseToken, enterpriseId, user.Id) + if err != nil { + log.Println(err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + result, err := db.UpdateUserGithub(userPrincipalName, ghId, ghUser, 1) if err != nil { log.Println(err.Error())