in hack/operatorhub/internal/github/github.go [153:275]
func (c *Client) cloneAndCreate(repo githubRepository) error {
orgRepo := fmt.Sprintf("%s/%s", repo.organization, repo.repository)
localTempDir := filepath.Join(repo.tempDir, repo.repository)
log.Printf("Cloning (%s) repository to temporary directory ", repo.url)
r, err := git.PlainClone(localTempDir, false, &git.CloneOptions{
URL: repo.url,
Auth: &git_http.BasicAuth{
Username: c.GitHubToken,
},
})
if err != nil {
return fmt.Errorf("cloning (%s): %w", orgRepo, err)
}
log.Println("✓")
log.Printf("Ensuring that (%s) repository has been forked ", orgRepo)
err = c.ensureFork(orgRepo)
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while ensuring fork exists: %w", err)
}
log.Println("✓")
log.Printf("Creating git remote for (%s) ", orgRepo)
remote, err := r.CreateRemote(&config.RemoteConfig{
Name: "fork",
URLs: []string{
fmt.Sprintf("https://github.com/%s/%s", c.GitHubUsername, repo.repository),
},
})
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while creating git remote: %w", err)
}
log.Println("✓")
err = c.syncFork(orgRepo, r, remote)
if err != nil {
return fmt.Errorf("while syncing fork with upstream: %w", err)
}
branchName := fmt.Sprintf("eck-%s-%s", repo.repository, c.GitTag)
log.Printf("Creating git branch (%s) for (%s) ", branchName, orgRepo)
err = r.CreateBranch(&config.Branch{
Name: branchName,
Remote: "fork",
Merge: plumbing.NewBranchReferenceName(branchName),
})
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while creating git branch: %w", err)
}
log.Println("✓")
log.Printf("Checking out new branch (%s) ", branchName)
var w *git.Worktree
w, err = r.Worktree()
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while retrieving a working tree from the git filesystem: %w", err)
}
err = w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branchName),
Create: true,
})
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while checking out branch (%s): %w", branchName, err)
}
log.Println("✓")
destDir := filepath.Join(localTempDir, "operators", repo.directoryName, c.GitTag)
srcDir := filepath.Join(c.PathToNewVersion, repo.repository, c.GitTag)
log.Printf("copying (%s) to (%s)", srcDir, destDir)
err = copy.Copy(srcDir, destDir)
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while copying source dir (%s) to new git cloned dir (%s): %w", srcDir, destDir, err)
}
log.Printf("Adding new data to git working tree ")
pathToAdd := filepath.Join("operators", repo.directoryName, c.GitTag)
if _, err = w.Add(pathToAdd); err != nil {
log.Println("ⅹ")
return fmt.Errorf("while adding destination directory (%s) to git working tree: %w", pathToAdd, err)
}
log.Println("✓")
log.Printf("Creating git commit ")
_, err = w.Commit(fmt.Sprintf("Update ECK to the latest version `%s`\n\nSigned-off-by: %s <%s>", c.GitTag, c.GitHubFullName, c.GitHubEmail), &git.CommitOptions{
Author: &object.Signature{
Name: c.GitHubFullName,
Email: c.GitHubEmail,
When: time.Now(),
},
})
if err != nil {
log.Println("ⅹ")
return fmt.Errorf("while commiting changes to git working tree: %w", err)
}
log.Println("✓")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
refSpec := fmt.Sprintf("+refs/heads/%[1]s:refs/heads/%[1]s", branchName)
err = remote.PushContext(ctx, &git.PushOptions{
RemoteName: "fork",
Auth: &git_http.BasicAuth{
Username: c.GitHubToken,
},
RefSpecs: []config.RefSpec{
config.RefSpec(refSpec),
},
})
if err != nil {
return fmt.Errorf("while pushing git refspec (%s) to remote: %w", refSpec, err)
}
return c.createPullRequest(repo, branchName)
}