Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix how local cloning is performed #98

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 65 additions & 32 deletions internal/engine/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (g *GoliacLocalImpl) Clone(accesstoken, repositoryUrl, branch string) error
return err
}
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName("refs/remotes/origin/" + branch),
Branch: plumbing.NewBranchReferenceName(branch),
})
if err != nil {
return err
Expand Down Expand Up @@ -316,6 +316,30 @@ func (g *GoliacLocalImpl) ArchiveRepos(reposToArchiveList []string, accesstoken
if g.repo == nil {
return fmt.Errorf("git repository not cloned")
}

// Get the HEAD reference
headRef, err := g.repo.Head()
if err != nil {
return err
}

if headRef.Name() != plumbing.NewBranchReferenceName(branch) {
// If not on main, check out the main branch
worktree, err := g.repo.Worktree()
if err != nil {
return err
}

err = worktree.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: false,
Force: true,
})
if err != nil {
return err
}
}

w, err := g.repo.Worktree()
if err != nil {
return err
Expand Down Expand Up @@ -371,21 +395,12 @@ func (g *GoliacLocalImpl) ArchiveRepos(reposToArchiveList []string, accesstoken
return err
}

// Get the HEAD reference
headRef, err := g.repo.Head()
if err != nil {
return err
}

refSpec := fmt.Sprintf("%s:refs/heads/%s", headRef.Name(), branch)
err = g.repo.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: "x-access-token", // This can be anything except an empty string
Password: accesstoken,
},
Force: true,
RefSpecs: []goconfig.RefSpec{goconfig.RefSpec(refSpec)},
})

if err != nil {
Expand Down Expand Up @@ -447,6 +462,23 @@ func (g *GoliacLocalImpl) UpdateAndCommitCodeOwners(repoconfig *config.Repositor
return err
}

if headRef.Name() != plumbing.NewBranchReferenceName(branch) {
// If not on main, check out the main branch
worktree, err := g.repo.Worktree()
if err != nil {
return err
}

err = worktree.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branch),
Create: false,
Force: true,
})
if err != nil {
return err
}
}

err = utils.WriteFile(w.Filesystem, codeownerpath, []byte(newContent), 0644)
if err != nil {
return err
Expand All @@ -469,15 +501,12 @@ func (g *GoliacLocalImpl) UpdateAndCommitCodeOwners(repoconfig *config.Repositor
return err
}

refSpec := fmt.Sprintf("%s:refs/heads/%s", headRef.Name(), branch)
err = g.repo.Push(&git.PushOptions{
RemoteName: "origin",
Auth: &http.BasicAuth{
Username: "x-access-token", // This can be anything except an empty string
Password: accesstoken,
},
Force: true,
RefSpecs: []goconfig.RefSpec{goconfig.RefSpec(refSpec)},
})

if err != nil {
Expand Down Expand Up @@ -615,35 +644,43 @@ func (g *GoliacLocalImpl) SyncUsersAndTeams(repoconfig *config.RepositoryConfig,
if len(teamschanged) > 0 || len(deletedusers) > 0 || len(addedusers) > 0 {

logrus.Info("some users and/or teams must be commited")
if dryrun {
return nil
}

for _, u := range deletedusers {
logrus.WithFields(map[string]interface{}{"dryrun": dryrun, "author": "goliac", "command": "remove_user_from_repository"}).Infof("user: %s", u)
_, err = w.Remove(u)
if err != nil {
return err
if !dryrun {
_, err = w.Remove(u)
if err != nil {
return err
}
}
}

for _, u := range addedusers {
logrus.WithFields(map[string]interface{}{"dryrun": dryrun, "author": "goliac", "command": "add_user_to_repository"}).Infof("user: %s", u)
_, err = w.Add(u)
if err != nil {
return err
if !dryrun {
fmt.Println(u)
_, err = w.Add(u)
if err != nil {
return err
}
}
}

for _, t := range teamschanged {
logrus.WithFields(map[string]interface{}{"dryrun": dryrun, "author": "goliac", "command": "update_team_to_repository"}).Infof("team: %s", t)
_, err = w.Add(t)
if err != nil {
return err
if !dryrun {
_, err = w.Add(t)
if err != nil {
return err
}
}
}

commit, err := w.Commit("update teams and users", &git.CommitOptions{
if dryrun {
return nil
}

_, err = w.Commit("update teams and users", &git.CommitOptions{
Author: &object.Signature{
Name: "Goliac",
Email: config.Config.GoliacEmail,
Expand All @@ -655,19 +692,15 @@ func (g *GoliacLocalImpl) SyncUsersAndTeams(repoconfig *config.RepositoryConfig,
return err
}

_, err = g.repo.CommitObject(commit)
if err != nil {
return err
}

// 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,
RemoteName: "origin",
Auth: auth,
})

return err
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func TestGoliacLocalImpl(t *testing.T) {
}

// archive the repository 'repo1'
err = g.ArchiveRepos([]string{"repo1"}, "none", "main", "foobar")
err = g.ArchiveRepos([]string{"repo1"}, "none", "master", "foobar")
assert.Nil(t, err)

// check the content of the 'archived/repo1.yaml' file
Expand Down Expand Up @@ -699,7 +699,7 @@ func TestGoliacLocalImpl(t *testing.T) {
assert.NotNil(t, goliacConfig)

// update and commit the CODEOWNERS file
err = g.UpdateAndCommitCodeOwners(goliacConfig, false, "none", "main", "foobar")
err = g.UpdateAndCommitCodeOwners(goliacConfig, false, "none", "master", "foobar")
assert.Nil(t, err)

// check the content of the CODEOWNERS file
Expand Down