Skip to content

Commit d1bf590

Browse files
committed
Cleanup branch
1 parent 0a9571c commit d1bf590

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

tools/flakeguard/cmd/make_pr.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"time"
1111

1212
"github.com/go-git/go-git/v5"
13-
"github.com/go-git/go-git/v5/config"
1413
"github.com/go-git/go-git/v5/plumbing"
1514
"github.com/google/go-github/v72/github"
1615
"github.com/spf13/cobra"
@@ -88,28 +87,18 @@ func makePR(cmd *cobra.Command, args []string) error {
8887
}
8988
fmt.Println(" ✅")
9089

91-
// Create and checkout new branch
92-
branchName := fmt.Sprintf("flakeguard-skip-%s", time.Now().Format("20060102150405"))
93-
err = targetRepoWorktree.Checkout(&git.CheckoutOptions{
94-
Branch: plumbing.NewBranchReferenceName(branchName),
95-
Create: true,
96-
})
97-
if err != nil {
98-
return fmt.Errorf("failed to checkout new branch: %w", err)
99-
}
100-
101-
// Push the new branch to GitHub before making any commits
102-
fmt.Printf("Pushing new branch '%s' to GitHub, tap your yubikey if it's blinking...", branchName)
103-
err = repo.Push(&git.PushOptions{
104-
RemoteName: "origin",
105-
RefSpecs: []config.RefSpec{
106-
config.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName)),
107-
},
108-
})
109-
if err != nil {
110-
return fmt.Errorf("failed to push new branch to GitHub: %w", err)
111-
}
112-
fmt.Println(" ✅")
90+
// Cleanup default branch when we're done
91+
defer func() {
92+
fmt.Printf("Cleaning up branch %s...", defaultBranch)
93+
err = targetRepoWorktree.Checkout(&git.CheckoutOptions{
94+
Branch: plumbing.NewBranchReferenceName(defaultBranch),
95+
Force: true,
96+
})
97+
if err != nil {
98+
fmt.Printf("Failed to checkout default branch: %v\n", err)
99+
}
100+
fmt.Println(" ✅")
101+
}()
113102

114103
if len(currentlyFlakyEntries) == 0 {
115104
fmt.Println("No flaky tests found!")
@@ -132,6 +121,7 @@ func makePR(cmd *cobra.Command, args []string) error {
132121
return fmt.Errorf("failed to modify code to skip tests: %w", err)
133122
}
134123

124+
branchName := fmt.Sprintf("flakeguard-skip-%s", time.Now().Format("20060102150405"))
135125
fmt.Print("Committing changes, tap your yubikey if it's blinking...")
136126
sha, err := flake_git.MakeSignedCommit(repoPath, fmt.Sprintf("Skips flaky %d tests: %s", len(testsToSkip), strings.Join(jiraTickets, ", ")), branchName, githubToken)
137127
if err != nil {

tools/flakeguard/git/git.go

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,37 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
279279
return "", err
280280
}
281281

282+
// Create the branch on GitHub using REST API
283+
ctx := context.Background()
284+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken})
285+
tc := oauth2.NewClient(ctx, ts)
286+
restClient := github.NewClient(tc)
287+
288+
owner, repoName, _, err := GetOwnerRepoDefaultBranchFromLocalRepo(repoPath)
289+
if err != nil {
290+
return "", fmt.Errorf("failed to get repo info: %w", err)
291+
}
292+
293+
// Get the reference of the current HEAD (which should be the base branch)
294+
headRef, err := repo.Head()
295+
if err != nil {
296+
return "", fmt.Errorf("failed to get HEAD reference: %w", err)
297+
}
298+
baseSHA := headRef.Hash().String()
299+
300+
// Create new branch reference on GitHub
301+
newRef := &github.Reference{
302+
Ref: github.Ptr("refs/heads/" + branch),
303+
Object: &github.GitObject{
304+
SHA: github.Ptr(baseSHA),
305+
},
306+
}
307+
308+
_, _, err = restClient.Git.CreateRef(ctx, owner, repoName, newRef)
309+
if err != nil {
310+
return "", fmt.Errorf("failed to create branch on GitHub: %w", err)
311+
}
312+
282313
// Inspired by https://github.com/planetscale/ghcommit/tree/main
283314

284315
// process added / modified files:
@@ -358,17 +389,8 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
358389
body = splitMsg[1]
359390
}
360391

361-
owner, repoName, _, err := GetOwnerRepoDefaultBranchFromLocalRepo(repoPath)
362-
if err != nil {
363-
return "", err
364-
}
365-
366-
// Get HEAD reference to get the current commit hash
367-
headRef, err := repo.Head()
368-
if err != nil {
369-
return "", fmt.Errorf("failed to get HEAD reference: %w", err)
370-
}
371-
expectedHeadOid := headRef.Hash().String()
392+
// Use the baseSHA from the branch creation as the expected head OID
393+
expectedHeadOid := baseSHA
372394
// create the $input struct for the graphQL createCommitOnBranch mutation request:
373395
input := githubv4.CreateCommitOnBranchInput{
374396
Branch: githubv4.CommittableBranch{
@@ -387,7 +409,7 @@ func MakeSignedCommit(repoPath, commitMessage, branch, githubToken string) (stri
387409
}
388410

389411
if err := graphqlClient.Mutate(context.Background(), &m, input, nil); err != nil {
390-
return "", err
412+
return "", fmt.Errorf("failed to create commit: %w", err)
391413
}
392414

393415
return m.CreateCommitOnBranch.Commit.OID, nil

0 commit comments

Comments
 (0)