Skip to content
Open
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
32 changes: 23 additions & 9 deletions pkg/repository/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/henderiw/logger/log"
)

Expand Down Expand Up @@ -179,16 +180,19 @@ func fetchAll(ctx context.Context, repo *git.Repository) error {
},
Tags: git.AllTags,
}
err := repo.Fetch(fetchOptions)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Debug("Repository already up-to-date")
return nil
return doGitWithAuth(ctx, func(auth transport.AuthMethod) error {
fetchOptions.Auth = auth
err := repo.Fetch(fetchOptions)
if err != nil {
if err == git.NoErrAlreadyUpToDate {
log.Debug("Repository already up-to-date")
return nil
}
log.Error("Failed to fetch updates", "error", err)
return fmt.Errorf("failed to fetch updates: %v", err)
}
log.Error("Failed to fetch updates", "error", err)
return fmt.Errorf("failed to fetch updates: %v", err)
}
return nil
return nil
})
}

func resetToRemoteHead(ctx context.Context, repo *git.Repository, branch plumbing.ReferenceName) error {
Expand Down Expand Up @@ -334,6 +338,16 @@ func doGitWithAuth(ctx context.Context, op func(transport.AuthMethod) error) err
// getAuthMethod fetches the credentials for authenticating to git. It caches the
// credentials between calls and refresh credentials when the tokens have expired.
func getAuthMethod(_ context.Context, _ bool) (transport.AuthMethod, error) {
username := os.Getenv("GIT_USERNAME")
password := os.Getenv("GIT_PASSWORD")

if username != "" && password != "" {
return &http.BasicAuth{
Username: username,
Password: password,
}, nil
}

// If no secret is provided, we try without any auth.
return nil, nil
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/repository/git/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package git

import (
"os"

"github.com/go-git/go-git/v5/plumbing"
)

const (
//DefaultMainReferenceName plumbing.ReferenceName = "refs/heads/main"
OriginName string = "origin"
MainBranch BranchName = "main"

BranchPrefixInRemoteRepo = "refs/remotes/" + OriginName + "/"
BranchPrefixInLocalRepo = "refs/heads/"
Expand Down Expand Up @@ -52,6 +53,8 @@ var (
config.RefSpec(tagsPrefixInLocalRepo + "*:" + tagsPrefixInRemoteRepo + "*"),
}
*/

MainBranch BranchName = BranchName(os.Getenv("GIT_MAIN_BRANCH"))
)

// BranchName represents a relative branch name (i.e. 'main', 'drafts/bucket/v1')
Expand Down
4 changes: 2 additions & 2 deletions pkg/repository/repogit/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ func (r *repo) PushBranch(branch string) error {
config.RefSpec("+" + lgit.BranchName(branch).BranchInRemote() + "*:" + lgit.BranchName(branch).BranchInLocal() + "*"),
},
Auth: &http.BasicAuth{
Username: "henderiw",
Password: "your-access-token",
Username: os.Getenv("GIT_USERNAME"),
Password: os.Getenv("GIT_PASSWORD"),
},
})
}