Skip to content

Commit

Permalink
feat: reassociate gh account (backend)
Browse files Browse the repository at this point in the history
Signed-off-by: Jerrico Dela Cruz <94591636+jerricotandelacruz@users.noreply.github.com>
  • Loading branch information
jerricotandelacruz committed Nov 19, 2024
1 parent 26fe3e1 commit d382891
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/goapp/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -702,6 +743,10 @@ type GetRepositoryProjectsResult struct {
Projects []Project
}

type GetUserByLoginResult struct {
User
}

// Structs
type Member struct {
Id string
Expand All @@ -722,3 +767,9 @@ type Project struct {
CreatedAt time.Time
UpdatedAt time.Time
}

type User struct {
Id string
DatabaseId int64
Login string
}
2 changes: 1 addition & 1 deletion src/goapp/routes/api/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions src/goapp/routes/login/github/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit d382891

Please sign in to comment.