Skip to content

Commit

Permalink
Merge pull request #1232 from Avanade/1177-latest-associate-new-githu…
Browse files Browse the repository at this point in the history
…b-account-to-user

1177 latest associate new GitHub account to user
  • Loading branch information
iibuan authored Nov 19, 2024
2 parents a140f73 + 2e4fde4 commit 44e1c0f
Show file tree
Hide file tree
Showing 8 changed files with 6,630 additions and 6 deletions.
1 change: 1 addition & 0 deletions .bicep/webapp/parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"EXEMPTION": "",

"ENABLED_REMOVE_COLLABORATORS": "",
"ENABLED_REMOVE_ENTERPRISE_MEMBER": "",

"ORGANIZATION_NAME":"",
"REGIONAL_ORG_PREFIX": "",
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/setup-appservice-resource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ jobs:
parameters.appServiceSettings.value.COPILOT_GROUP_SLUG : ${{ vars.COPILOT_GROUP_SLUG }}
parameters.appServiceSettings.value.EXEMPTION : ${{ vars.EXEMPTION }}
parameters.appServiceSettings.value.ENABLED_REMOVE_COLLABORATORS : ${{ vars.ENABLED_REMOVE_COLLABORATORS }}
parameters.appServiceSettings.value.ENABLED_REMOVE_ENTERPRISE_MEMBER : ${{ vars.ENABLED_REMOVE_ENTERPRISE_MEMBER }}
parameters.appServiceSettings.value.ORGANIZATION_NAME : ${{ vars.ORGANIZATION_NAME }}
parameters.appServiceSettings.value.REGIONAL_ORG_PREFIX: ${{ vars.REGIONAL_ORG_PREFIX }}
parameters.appServiceSettings.value.LINK_INNERSOURCE_GENERAL_LEGAL_GUIDELINES : ${{ vars.LINK_INNERSOURCE_GENERAL_LEGAL_GUIDELINES }}
Expand Down
1 change: 1 addition & 0 deletions src/goapp/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ EXEMPTION=""
ENABLED_REMOVE_COLLABORATORS=<Defaults to false>
ENABLED_REPO_OWNER_SCAN=<Defaults to false>
ENABLED_INDEX_ORG_REPO=<Defaults to false>
ENABLED_REMOVE_ENTERPRISE_MEMBER=<Defaults to false>
ORGANIZATION_NAME=""
LINK_INNERSOURCE_GENERAL_LEGAL_GUIDELINES=""
LINK_INNERSOURCE_GENERAL_GUIDELINES=""
Expand Down
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
32 changes: 31 additions & 1 deletion 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 All @@ -185,7 +201,21 @@ func GithubForceSaveHandler(w http.ResponseWriter, r *http.Request) {
return
}

http.Redirect(w, r, "/", http.StatusSeeOther)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)

orgNames := struct {
InnerSourceOrgName string `json:"innersourceOrgName"`
}{
InnerSourceOrgName: os.Getenv("GH_ORG_INNERSOURCE"),
}

jsonResp, err := json.Marshal(orgNames)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Write(jsonResp)
}

func CheckMembership(userPrincipalName, ghusername string) {
Expand Down
Loading

0 comments on commit 44e1c0f

Please sign in to comment.