Skip to content

Commit

Permalink
bugfix: fixing usersync code
Browse files Browse the repository at this point in the history
  • Loading branch information
nzin-alayacare committed Sep 17, 2023
1 parent e762ad1 commit 330c56a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
9 changes: 9 additions & 0 deletions internal/engine/githubsaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Alayacare/goliac/internal/config"
"github.com/Alayacare/goliac/internal/entity"
"github.com/Alayacare/goliac/internal/github"
"github.com/sirupsen/logrus"
)

const listUsersFromGithubOrgSaml = `
Expand Down Expand Up @@ -102,6 +103,14 @@ func LoadUsersFromGithubOrgSaml(client github.GitHubClient) (map[string]*entity.
}

for _, c := range gResult.Data.Organization.SamlIdentityProvider.ExternalIdentities.Edges {
if c.Node.SamlIdentity.NameId == "" {
logrus.Debugf("Skipping user with empty NameId: %s", c.Node.User.Login)
continue
}
if c.Node.User.Login == "" {
logrus.Debugf("Skipping user with empty login: %s", c.Node.SamlIdentity.NameId)
continue
}
user := &entity.User{}
user.ApiVersion = "v1"
user.Kind = "User"
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/goliac_reconciliator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (m *GoliacLocalMock) RuleSets() map[string]*entity.RuleSet {
func (m *GoliacLocalMock) UpdateAndCommitCodeOwners(repoconfig *config.RepositoryConfig, dryrun bool, accesstoken string, branch string, tagname string) error {
return nil
}
func (m *GoliacLocalMock) SyncUsersAndTeams(repoconfig *config.RepositoryConfig, plugin UserSyncPlugin, dryrun bool) error {
func (m *GoliacLocalMock) SyncUsersAndTeams(repoconfig *config.RepositoryConfig, plugin UserSyncPlugin, accesstoken string, dryrun bool) error {
return nil
}
func (m *GoliacLocalMock) Close() {
Expand Down
20 changes: 14 additions & 6 deletions internal/engine/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type GoliacLocalGit interface {
// whenever someone create/delete a team, we must update the github CODEOWNERS
UpdateAndCommitCodeOwners(repoconfig *config.RepositoryConfig, dryrun bool, accesstoken string, branch string, tagname string) error
// whenever the users list is changing, reload users and teams, and commit them
SyncUsersAndTeams(repoconfig *config.RepositoryConfig, plugin UserSyncPlugin, dryrun bool) error
SyncUsersAndTeams(repoconfig *config.RepositoryConfig, plugin UserSyncPlugin, accesstoken string, dryrun bool) error
Close()

// Load and Validate from a local directory
Expand Down Expand Up @@ -439,7 +439,7 @@ func syncUsersViaUserPlugin(repoconfig *config.RepositoryConfig, fs afero.Fs, us
for username, user := range orgUsers {
if newuser, ok := newOrgUsers[username]; !ok {
// deleted user
deletedusers = append(deletedusers, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)))
deletedusers = append(deletedusers, filepath.Join("users", "org", fmt.Sprintf("%s.yaml", username)))
fs.Remove(filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)))
} else {
// check if user changed
Expand All @@ -457,7 +457,7 @@ func syncUsersViaUserPlugin(repoconfig *config.RepositoryConfig, fs afero.Fs, us
if err != nil {
return nil, nil, err
}
updatedusers = append(updatedusers, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)))
updatedusers = append(updatedusers, filepath.Join("users", "org", fmt.Sprintf("%s.yaml", username)))
}

delete(newOrgUsers, username)
Expand All @@ -477,12 +477,12 @@ func syncUsersViaUserPlugin(repoconfig *config.RepositoryConfig, fs afero.Fs, us
if err != nil {
return nil, nil, err
}
updatedusers = append(updatedusers, filepath.Join(rootDir, "users", "org", fmt.Sprintf("%s.yaml", username)))
updatedusers = append(updatedusers, filepath.Join("users", "org", fmt.Sprintf("%s.yaml", username)))
}
return deletedusers, updatedusers, nil
}

func (g *GoliacLocalImpl) SyncUsersAndTeams(repoconfig *config.RepositoryConfig, userplugin UserSyncPlugin, dryrun bool) error {
func (g *GoliacLocalImpl) SyncUsersAndTeams(repoconfig *config.RepositoryConfig, userplugin UserSyncPlugin, accesstoken string, dryrun bool) error {
if g.repo == nil {
return fmt.Errorf("git repository not cloned")
}
Expand Down Expand Up @@ -570,7 +570,15 @@ func (g *GoliacLocalImpl) SyncUsersAndTeams(repoconfig *config.RepositoryConfig,
return err
}

err = g.repo.Push(&git.PushOptions{})
// Now push the tag to the remote repository
auth := &http.BasicAuth{
Username: "x-access-token", // This can be anything except an empty string
Password: accesstoken,
}

err = g.repo.Push(&git.PushOptions{
Auth: auth,
})

return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (u *User) Validate(filename string) error {
}

if u.Spec.GithubID == "" {
return fmt.Errorf("data.githubID is empty for user filename %s", filename)
return fmt.Errorf("spec.githubID is empty for user filename %s", filename)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions internal/goliac.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ func (g *GoliacImpl) UsersUpdate(repositoryUrl, branch string) error {
return fmt.Errorf("unable to read goliac.yaml config file: %v", err)
}

userplugin, found := engine.GetUserSyncPlugin(g.repoconfig.UserSync.Plugin)
userplugin, found := engine.GetUserSyncPlugin(repoconfig.UserSync.Plugin)
if !found {
return fmt.Errorf("User Sync Plugin %s not found", g.repoconfig.UserSync.Plugin)
return fmt.Errorf("User Sync Plugin %s not found", repoconfig.UserSync.Plugin)
}

err = g.local.SyncUsersAndTeams(repoconfig, userplugin, false)
err = g.local.SyncUsersAndTeams(repoconfig, userplugin, accessToken, false)
return err
}

0 comments on commit 330c56a

Please sign in to comment.