From d3119d24e5bfdf0ee54e367230d68a11c15032cf Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Thu, 25 Jan 2024 08:16:40 -0500 Subject: [PATCH 01/21] Fetch the base by ref, and the head by PR ref Same idea as the previous change, but correct(tm). Signed-off-by: Andrew Mason --- go/pull_request.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index a96487f..45d953f 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -609,8 +609,8 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( remote := pr.GetBase().GetRepo().GetCloneURL() ref := pr.GetBase().GetRef() - if err := vitess.FetchRef(ctx, "origin", fmt.Sprintf("refs/pull/%d/head", pr.GetNumber())); err != nil { - return nil, errors.Wrapf(err, "Failed to fetch Pull Request %s/%s#%d to %s for %s", vitess.Owner, vitess.Name, pr.GetNumber(), op, pr.GetHTMLURL()) + if err := vitess.FetchRef(ctx, remote, ref); err != nil { + return nil, errors.Wrapf(err, "Failed to fetch %s:%s to %s for %s", remote, ref, op, pr.GetHTMLURL()) } if err := vitess.Checkout(ctx, "FETCH_HEAD"); err != nil { @@ -636,13 +636,11 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( } // 4. Switch vitess repo to the PR's head ref. - ref = pr.GetHead().GetRef() - if err := vitess.Checkout(ctx, ref); err != nil { - return nil, errors.Wrapf(err, "Failed to checkout %s in %s/%s to %s for %s", ref, vitess.Owner, vitess.Name, op, pr.GetHTMLURL()) + if err := vitess.FetchRef(ctx, remote, fmt.Sprintf("refs/pull/%d/head", pr.GetNumber())); err != nil { + return nil, errors.Wrapf(err, "Failed to fetch Pull Request %s/%s#%d to %s for %s", vitess.Owner, vitess.Name, pr.GetNumber(), op, pr.GetHTMLURL()) } - - if err := vitess.Pull(ctx); err != nil { - return nil, errors.Wrapf(err, "Failed to pull %s/%s:%s to %s for %s", vitess.Owner, vitess.Name, ref, op, pr.GetHTMLURL()) + if err := vitess.Checkout(ctx, "FETCH_HEAD"); err != nil { + return nil, errors.Wrapf(err, "Failed to checkout %s in %s/%s to %s for %s", ref, vitess.Owner, vitess.Name, op, pr.GetHTMLURL()) } // 5. Run the sync script again with `COBRADOC_VERSION_PAIRS=$(headref):$(docsVersion)`. From c25a9667316e3ce694f3ab03f6197e8474f9e6ed Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 11:20:29 -0500 Subject: [PATCH 02/21] diff-tree stuff Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 57 ++++++++++++++++++++++++++++++++++++++ go/git/diff_tree_test.go | 60 ++++++++++++++++++++++++++++++++++++++++ go/git/repo.go | 15 ++++++++++ 3 files changed, 132 insertions(+) create mode 100644 go/git/diff_tree.go create mode 100644 go/git/diff_tree_test.go diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go new file mode 100644 index 0000000..be89611 --- /dev/null +++ b/go/git/diff_tree.go @@ -0,0 +1,57 @@ +package git + +import ( + "os" + "path/filepath" + "regexp" + + "github.com/google/go-github/v53/github" +) + +/* +:100644 000000 5716ca5987cbf97d6bb54920bea6adde242d87e6 0000000000000000000000000000000000000000 D bar/bar.txt +:000000 100644 0000000000000000000000000000000000000000 76018072e09c5d31c8c6e3113b8aa0fe625195ca A baz.txt +:100644 100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 b210800439ffe3f2db0d47d9aab1969b38a770a5 M foo.txt +*/ + +var diffTreeEntryRegexp = regexp.MustCompile(`^:(?P\d{6}) (?P\d{6}) (?P[a-f0-9]{40}) (?P[a-f0-9]{40}) [A-Z]\W(?P.*)$`) + +// See https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree +func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, *github.Blob, error) { + match := diffTreeEntryRegexp.FindStringSubmatch(line) + if match == nil { + return nil, nil, nil + } + + oldMode := match[1] + newMode := match[2] + // oldSHA := match[3] + // newSHA := match[4] + path := match[5] + + entry := github.TreeEntry{ + Path: &path, + Mode: &newMode, + Type: github.String("blob"), + } + + if newMode == "000000" { + // File deleted. + entry.Mode = &oldMode // GitHub API suggests sending 000000 will result in an error, and we're deleting the file anyway. + entry.SHA = nil + + return &entry, nil, nil + } + + content, err := os.ReadFile(filepath.Join(basedir, path)) + if err != nil { + return nil, nil, err + } + + entry.Content = github.String(string(content)) + + return &entry, &github.Blob{ + Content: entry.Content, + Encoding: github.String("utf-8"), + }, nil +} diff --git a/go/git/diff_tree_test.go b/go/git/diff_tree_test.go new file mode 100644 index 0000000..eea66c7 --- /dev/null +++ b/go/git/diff_tree_test.go @@ -0,0 +1,60 @@ +package git + +import ( + "testing" + + "github.com/google/go-github/v53/github" +) + +func TestParseDiffTreeEntry(t *testing.T) { + // TODO: create fake filesystem + // baz.txt contains: baz + // foo.txt contains foo\nfoo2 after the edit + tcases := []struct { + name string + in string + want *github.TreeEntry + wantErr bool + }{ + { + name: "deleted file", + in: ":100644 000000 5716ca5987cbf97d6bb54920bea6adde242d87e6 0000000000000000000000000000000000000000 D bar/bar.txt", + want: &github.TreeEntry{ + SHA: nil, // Indicates deletion + Path: github.String("bar/bar.txt"), + Mode: github.String("100644"), + Type: github.String("blob"), + }, + }, + { + name: "created file", + in: ":000000 100644 0000000000000000000000000000000000000000 76018072e09c5d31c8c6e3113b8aa0fe625195ca A baz.txt", + want: &github.TreeEntry{ + Path: github.String("baz.txt"), + Mode: github.String("100644"), + Type: github.String("blob"), + Content: github.String("baz"), + }, + }, + { + name: "modified file", + in: ":100644 100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 b210800439ffe3f2db0d47d9aab1969b38a770a5 M foo.txt", + want: &github.TreeEntry{ + Path: github.String("foo.txt"), + Mode: github.String("100644"), + Type: github.String("blob"), + Content: github.String("foo"), + }, + }, + } + + for _, tc := range tcases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + // entry, err := ParseDiffTreeEntry(tc.in, "TODO") + if tc.wantErr { + // TODO: + } + }) + } +} diff --git a/go/git/repo.go b/go/git/repo.go index 9b54e56..b2228ed 100644 --- a/go/git/repo.go +++ b/go/git/repo.go @@ -108,6 +108,21 @@ func (r *Repo) Commit(ctx context.Context, msg string, opts CommitOpts) error { return err } +type DiffTreeOpts struct { + Recursive bool +} + +func (r *Repo) DiffTree(ctx context.Context, baseTreeIsh string, headTreeIsh string, opts DiffTreeOpts) ([]byte, error) { + args := []string{"diff-tree"} + if opts.Recursive { + args = append(args, "-r") + } + + args = append(args, baseTreeIsh, headTreeIsh) + + return shell.NewContext(ctx, "git", args...).Output() +} + func (r *Repo) Fetch(ctx context.Context, remote string) error { return r.fetch(ctx, remote) } From fb96d01f183ca23124dc82489c6eec749d232072 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 11:22:45 -0500 Subject: [PATCH 03/21] convert a diff-tree into a github API blob+trees to commit with auth ctx Signed-off-by: Andrew Mason --- go/pull_request.go | 132 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 119 insertions(+), 13 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index 45d953f..0f6b7d4 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "bytes" "context" "encoding/json" "fmt" @@ -536,6 +537,15 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( // based on `prod`. branch := "prod" headBranch := fmt.Sprintf("cobradocs-preview-for-%d", prInfo.num) + headRef := fmt.Sprintf("refs/heads/%s", headBranch) + + prodBranch, _, err := client.Repositories.GetBranch(ctx, website.Owner, website.Name, branch, false) + if err != nil { + return nil, errors.Wrapf(err, "Failed get production branch on %s/%s to preview cobradocs on Pull Request %d", website.Owner, website.Name, pr.GetNumber()) + } + + baseTree := prodBranch.GetCommit().Commit.Tree.GetSHA() + parent := prodBranch.GetCommit().GetSHA() op := "generate cobradocs preview" var openPR *github.PullRequest @@ -627,14 +637,30 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( return nil, errors.Wrapf(err, "Failed to run cobradocs sync script against %s:%s to %s for %s", remote, ref, op, pr.GetHTMLURL()) } - // Amend the commit to change the author to the bot. - if err := website.Commit(ctx, fmt.Sprintf("generate cobradocs against %s:%s", remote, ref), git.CommitOpts{ - Author: botCommitAuthor, - Amend: true, - }); err != nil { - return nil, errors.Wrapf(err, "Failed to amend commit author to %s for %s", op, pr.GetHTMLURL()) + tree, commit, err := h.writeAndCommitTree( + ctx, + client, + website, + pr, + "prod", + "HEAD", + baseTree, + parent, + fmt.Sprintf("Generate cobradocs before preview against %s:%s", remote, ref), + op, + ) + if err != nil { + return nil, err } + // // Amend the commit to change the author to the bot. + // if err := website.Commit(ctx, fmt.Sprintf("generate cobradocs against %s:%s", remote, ref), git.CommitOpts{ + // Author: botCommitAuthor, + // Amend: true, + // }); err != nil { + // return nil, errors.Wrapf(err, "Failed to amend commit author to %s for %s", op, pr.GetHTMLURL()) + // } + // 4. Switch vitess repo to the PR's head ref. if err := vitess.FetchRef(ctx, remote, fmt.Sprintf("refs/pull/%d/head", pr.GetNumber())); err != nil { return nil, errors.Wrapf(err, "Failed to fetch Pull Request %s/%s#%d to %s for %s", vitess.Owner, vitess.Name, pr.GetNumber(), op, pr.GetHTMLURL()) @@ -661,15 +687,37 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( return nil, errors.Wrapf(err, "Failed to amend commit author to %s for %s", op, pr.GetHTMLURL()) } - // 6. Force push. - if err := website.Push(ctx, git.PushOpts{ - Remote: "origin", - Refs: []string{headBranch}, - Force: true, - }); err != nil { - return nil, errors.Wrapf(err, "Failed to push %s to %s for %s", headBranch, op, pr.GetHTMLURL()) + _, commit, err = h.writeAndCommitTree( + ctx, + client, + website, + pr, + "HEAD~1", + "HEAD", + tree.GetSHA(), + commit.GetSHA(), + fmt.Sprintf("Generate cobradocs after preview against %s:%s", remote, ref), + op, + ) + if err != nil { + return nil, err } + // 6. Force push. + client.Git.UpdateRef(ctx, website.Owner, website.Name, &github.Reference{ + Ref: &headRef, + Object: &github.GitObject{SHA: commit.SHA}, + }, true) + // + // // 6. Force push. + // if err := website.Push(ctx, git.PushOpts{ + // Remote: "origin", + // Refs: []string{headBranch}, + // Force: true, + // }); err != nil { + // return nil, errors.Wrapf(err, "Failed to push %s to %s for %s", headBranch, op, pr.GetHTMLURL()) + // } + switch openPR { case nil: // 7. Create PR with clear instructions that this is for preview purposes only @@ -703,6 +751,64 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( return openPR, nil } +func (h *PullRequestHandler) writeAndCommitTree( + ctx context.Context, + client *github.Client, + repo *git.Repo, + pr *github.PullRequest, + baseRef string, + headRef string, + baseTree string, + parentCommit string, + commitMsg string, + op string, +) (*github.Tree, *github.Commit, error) { + logger := zerolog.Ctx(ctx) + + out, err := repo.DiffTree(ctx, baseRef, headRef, git.DiffTreeOpts{Recursive: true}) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to diff-tree %s %s in %s/%s to %s for %s", baseRef, headRef, repo.Owner, repo.Name, op, pr.GetHTMLURL()) + } + + lines := bytes.Split(out, []byte{'\n'}) + logger.Debug().Msgf("Found %d entries in diff from %s to %s", len(lines), baseRef, headRef) + + var tree = &github.Tree{} + + for _, line := range lines { + entry, blob, err := git.ParseDiffTreeEntry(string(line), repo.LocalDir) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) + } + + _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) + } + + tree.Entries = append(tree.Entries, entry) + } + + tree, _, err = client.Git.CreateTree(ctx, repo.Owner, repo.Name, baseTree, tree.Entries) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to create tree based on %s to %s for %s", baseTree, op, pr.GetHTMLURL()) + } + + commit := &github.Commit{ + Message: &commitMsg, + Tree: tree, + Parents: []*github.Commit{ + {SHA: &parentCommit}, + }, + } + commit, _, err = client.Git.CreateCommit(ctx, repo.Owner, repo.Name, commit) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to create commit based on %s to %s for %s", parentCommit, op, pr.GetHTMLURL()) + } + + return tree, commit, nil +} + func (h *PullRequestHandler) updateDocs(ctx context.Context, event github.PullRequestEvent, prInfo prInformation) (err error) { installationID := githubapp.GetInstallationIDFromEvent(&event) client, err := h.NewInstallationClient(installationID) From 1eac9dfa29b7ba0a14607d91614dcc6741152a27 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 12:24:44 -0500 Subject: [PATCH 04/21] try fetching to avoid bad object errors Signed-off-by: Andrew Mason --- go/pull_request.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/go/pull_request.go b/go/pull_request.go index 0f6b7d4..538349e 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -549,6 +549,10 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( op := "generate cobradocs preview" var openPR *github.PullRequest + if err := website.FetchRef(ctx, "origin", branch); err != nil { + return nil, errors.Wrapf(err, "Failed to fetch origin/%s in %s/%s to %s for %s", branch, website.Owner, website.Name, op, pr.GetHTMLURL()) + } + if err := createAndCheckoutBranch(ctx, client, website, branch, headBranch, fmt.Sprintf("%s for %s", op, pr.GetHTMLURL())); err != nil { return nil, err } @@ -642,7 +646,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( client, website, pr, - "prod", + branch, "HEAD", baseTree, parent, From 41a9c4bff52ebd5362dfbbd44ee82e0d71e07f03 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 14:29:55 -0500 Subject: [PATCH 05/21] dumb dumb Signed-off-by: Andrew Mason --- go/git/repo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/git/repo.go b/go/git/repo.go index b2228ed..4115456 100644 --- a/go/git/repo.go +++ b/go/git/repo.go @@ -120,7 +120,7 @@ func (r *Repo) DiffTree(ctx context.Context, baseTreeIsh string, headTreeIsh str args = append(args, baseTreeIsh, headTreeIsh) - return shell.NewContext(ctx, "git", args...).Output() + return shell.NewContext(ctx, "git", args...).InDir(r.LocalDir).Output() } func (r *Repo) Fetch(ctx context.Context, remote string) error { From 634a90737ee059ccc262b3fb0e3b6efa9dca1078 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 14:34:34 -0500 Subject: [PATCH 06/21] no longer needed Signed-off-by: Andrew Mason --- go/pull_request.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index 538349e..a4f523a 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -549,10 +549,6 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( op := "generate cobradocs preview" var openPR *github.PullRequest - if err := website.FetchRef(ctx, "origin", branch); err != nil { - return nil, errors.Wrapf(err, "Failed to fetch origin/%s in %s/%s to %s for %s", branch, website.Owner, website.Name, op, pr.GetHTMLURL()) - } - if err := createAndCheckoutBranch(ctx, client, website, branch, headBranch, fmt.Sprintf("%s for %s", op, pr.GetHTMLURL())); err != nil { return nil, err } From dc676012327a50cb664ea15b478a220882f871cd Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Fri, 26 Jan 2024 14:38:44 -0500 Subject: [PATCH 07/21] no blob for deleted files Signed-off-by: Andrew Mason --- go/pull_request.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index a4f523a..e08b983 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -781,9 +781,11 @@ func (h *PullRequestHandler) writeAndCommitTree( return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) } - _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) - if err != nil { - return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) + if blob != nil { + _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) + if err != nil { + return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) + } } tree.Entries = append(tree.Entries, entry) From c9ee3dce9c0e5120c9f6ad78d95cd58e77f1a4d9 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 09:38:48 -0500 Subject: [PATCH 08/21] skip empty entries Signed-off-by: Andrew Mason --- go/pull_request.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/go/pull_request.go b/go/pull_request.go index e08b983..6819ca0 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -781,7 +781,14 @@ func (h *PullRequestHandler) writeAndCommitTree( return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) } + if entry == nil { + logger.Debug().Msgf("Invalid diff-tree line %s", line) + continue + } + if blob != nil { + logger.Debug().Msgf("Creating blob for %s", entry.GetPath()) + _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) From 8cd957baba492fbdb908612f7855ff5cc6e84c15 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 09:38:54 -0500 Subject: [PATCH 09/21] debug Signed-off-by: Andrew Mason --- go/pull_request.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/go/pull_request.go b/go/pull_request.go index 6819ca0..c21316f 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -798,6 +798,7 @@ func (h *PullRequestHandler) writeAndCommitTree( tree.Entries = append(tree.Entries, entry) } + logger.Debug().Msgf("Creating tree with %d entries based on %s", len(tree.Entries), baseTree) tree, _, err = client.Git.CreateTree(ctx, repo.Owner, repo.Name, baseTree, tree.Entries) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to create tree based on %s to %s for %s", baseTree, op, pr.GetHTMLURL()) @@ -810,6 +811,7 @@ func (h *PullRequestHandler) writeAndCommitTree( {SHA: &parentCommit}, }, } + logger.Debug().Msgf("Authoring commit %q with tree %s with parent %s", commitMsg, tree.GetSHA(), parentCommit) commit, _, err = client.Git.CreateCommit(ctx, repo.Owner, repo.Name, commit) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to create commit based on %s to %s for %s", parentCommit, op, pr.GetHTMLURL()) From cffaf529c4ed55f40ac39570dfbf1aeb6cb690ca Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 09:44:03 -0500 Subject: [PATCH 10/21] skip creating blobs Signed-off-by: Andrew Mason --- go/pull_request.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index c21316f..deb13ef 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -776,7 +776,11 @@ func (h *PullRequestHandler) writeAndCommitTree( var tree = &github.Tree{} for _, line := range lines { - entry, blob, err := git.ParseDiffTreeEntry(string(line), repo.LocalDir) + if len(line) == 0 { + continue + } + + entry, _, err := git.ParseDiffTreeEntry(string(line), repo.LocalDir) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) } @@ -786,14 +790,14 @@ func (h *PullRequestHandler) writeAndCommitTree( continue } - if blob != nil { - logger.Debug().Msgf("Creating blob for %s", entry.GetPath()) - - _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) - if err != nil { - return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) - } - } + // if blob != nil { + // logger.Debug().Msgf("Creating blob for %s", entry.GetPath()) + // + // _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) + // if err != nil { + // return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) + // } + // } tree.Entries = append(tree.Entries, entry) } From feab3105b3d396b70efe3a2b06a0e42f4b3fca50 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 10:59:59 -0500 Subject: [PATCH 11/21] clean up messages Signed-off-by: Andrew Mason --- go/pull_request.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index deb13ef..8f790a4 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -696,7 +696,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( "HEAD", tree.GetSHA(), commit.GetSHA(), - fmt.Sprintf("Generate cobradocs after preview against %s:%s", remote, ref), + fmt.Sprintf("Generate cobradocs after preview against %s:%s", remote, pr.GetHead().GetRef()), op, ) if err != nil { @@ -723,10 +723,10 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( // 7. Create PR with clear instructions that this is for preview purposes only // and must not be merged. newPR := &github.NewPullRequest{ - Title: github.String(fmt.Sprintf("[DO NOT MERGE] [cobradocs] preview cobradocs changes for %s/%s:%s", vitess.Owner, vitess.Name, ref)), + Title: github.String(fmt.Sprintf("[DO NOT MERGE] [cobradocs] preview cobradocs changes for %s/%s#%d", vitess.Owner, vitess.Name, prInfo.num)), Head: github.String(headBranch), Base: github.String(branch), - Body: github.String(fmt.Sprintf("## Description\nThis is an automated PR to update the released cobradocs with [%s/%s:%s](%s)", vitess.Owner, vitess.Name, ref, pr.GetHTMLURL())), + Body: github.String(fmt.Sprintf("## Description\nThis is an automated PR to preview changes to the the released cobradocs with %s", pr.GetHTMLURL())), MaintainerCanModify: github.Bool(true), } openPR, _, err = client.PullRequests.Create(ctx, website.Owner, website.Name, newPR) From 43adc449fa3d85d677f07f53ee75ae0defff991d Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:02:40 -0500 Subject: [PATCH 12/21] blobs no longer needed Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 13 +++++-------- go/pull_request.go | 11 +---------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go index be89611..bd04645 100644 --- a/go/git/diff_tree.go +++ b/go/git/diff_tree.go @@ -17,10 +17,10 @@ import ( var diffTreeEntryRegexp = regexp.MustCompile(`^:(?P\d{6}) (?P\d{6}) (?P[a-f0-9]{40}) (?P[a-f0-9]{40}) [A-Z]\W(?P.*)$`) // See https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree -func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, *github.Blob, error) { +func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, error) { match := diffTreeEntryRegexp.FindStringSubmatch(line) if match == nil { - return nil, nil, nil + return nil, nil } oldMode := match[1] @@ -40,18 +40,15 @@ func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, *github entry.Mode = &oldMode // GitHub API suggests sending 000000 will result in an error, and we're deleting the file anyway. entry.SHA = nil - return &entry, nil, nil + return &entry, nil } content, err := os.ReadFile(filepath.Join(basedir, path)) if err != nil { - return nil, nil, err + return nil, err } entry.Content = github.String(string(content)) - return &entry, &github.Blob{ - Content: entry.Content, - Encoding: github.String("utf-8"), - }, nil + return &entry, nil } diff --git a/go/pull_request.go b/go/pull_request.go index 8f790a4..c51a699 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -780,7 +780,7 @@ func (h *PullRequestHandler) writeAndCommitTree( continue } - entry, _, err := git.ParseDiffTreeEntry(string(line), repo.LocalDir) + entry, err := git.ParseDiffTreeEntry(string(line), repo.LocalDir) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) } @@ -790,15 +790,6 @@ func (h *PullRequestHandler) writeAndCommitTree( continue } - // if blob != nil { - // logger.Debug().Msgf("Creating blob for %s", entry.GetPath()) - // - // _, _, err = client.Git.CreateBlob(ctx, repo.Owner, repo.Name, blob) - // if err != nil { - // return nil, nil, errors.Wrapf(err, "Failed to create blob for %s to %s for %s", entry.GetPath(), op, pr.GetHTMLURL()) - // } - // } - tree.Entries = append(tree.Entries, entry) } From 7ce4175164730dccfcb31c319d89b18a51f047ea Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:02:49 -0500 Subject: [PATCH 13/21] remove old commented-out code Signed-off-by: Andrew Mason --- go/pull_request.go | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index c51a699..be891ab 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -653,14 +653,6 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( return nil, err } - // // Amend the commit to change the author to the bot. - // if err := website.Commit(ctx, fmt.Sprintf("generate cobradocs against %s:%s", remote, ref), git.CommitOpts{ - // Author: botCommitAuthor, - // Amend: true, - // }); err != nil { - // return nil, errors.Wrapf(err, "Failed to amend commit author to %s for %s", op, pr.GetHTMLURL()) - // } - // 4. Switch vitess repo to the PR's head ref. if err := vitess.FetchRef(ctx, remote, fmt.Sprintf("refs/pull/%d/head", pr.GetNumber())); err != nil { return nil, errors.Wrapf(err, "Failed to fetch Pull Request %s/%s#%d to %s for %s", vitess.Owner, vitess.Name, pr.GetNumber(), op, pr.GetHTMLURL()) @@ -679,14 +671,6 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( return nil, errors.Wrapf(err, "Failed to run cobradocs sync script against %s/%s:%s to %s for %s", vitess.Owner, vitess.Name, ref, op, pr.GetHTMLURL()) } - // Amend the commit to change the author to the bot. - if err := website.Commit(ctx, fmt.Sprintf("generate cobradocs against %s/%s:%s", website.Owner, website.Name, ref), git.CommitOpts{ - Author: botCommitAuthor, - Amend: true, - }); err != nil { - return nil, errors.Wrapf(err, "Failed to amend commit author to %s for %s", op, pr.GetHTMLURL()) - } - _, commit, err = h.writeAndCommitTree( ctx, client, @@ -708,15 +692,6 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( Ref: &headRef, Object: &github.GitObject{SHA: commit.SHA}, }, true) - // - // // 6. Force push. - // if err := website.Push(ctx, git.PushOpts{ - // Remote: "origin", - // Refs: []string{headBranch}, - // Force: true, - // }); err != nil { - // return nil, errors.Wrapf(err, "Failed to push %s to %s for %s", headBranch, op, pr.GetHTMLURL()) - // } switch openPR { case nil: From 80de4a9f148f14b01f706bcc81592cc28be8cd0d Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:12:40 -0500 Subject: [PATCH 14/21] slightly better commit messages Signed-off-by: Andrew Mason --- go/pull_request.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index be891ab..0014e21 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -646,7 +646,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( "HEAD", baseTree, parent, - fmt.Sprintf("Generate cobradocs before preview against %s:%s", remote, ref), + fmt.Sprintf("Generate cobradocs preview against %s:%s", remote, ref), op, ) if err != nil { @@ -680,7 +680,12 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( "HEAD", tree.GetSHA(), commit.GetSHA(), - fmt.Sprintf("Generate cobradocs after preview against %s:%s", remote, pr.GetHead().GetRef()), + fmt.Sprintf( + "Generate cobradocs preview against %s/%s:%s", + pr.GetHead().GetRepo().GetOwner(), + pr.GetHead().GetRepo().GetName(), + pr.GetHead().GetRef(), + ), op, ) if err != nil { From 0f06f55b9336bffada290f81e72988e4201e89d1 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:14:44 -0500 Subject: [PATCH 15/21] make parser fail when regex does not match Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 3 ++- go/pull_request.go | 5 ----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go index bd04645..df420c1 100644 --- a/go/git/diff_tree.go +++ b/go/git/diff_tree.go @@ -1,6 +1,7 @@ package git import ( + "fmt" "os" "path/filepath" "regexp" @@ -20,7 +21,7 @@ var diffTreeEntryRegexp = regexp.MustCompile(`^:(?P\d{6}) (?P\ func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, error) { match := diffTreeEntryRegexp.FindStringSubmatch(line) if match == nil { - return nil, nil + return nil, fmt.Errorf("invalid diff-tree line format %s", line) } oldMode := match[1] diff --git a/go/pull_request.go b/go/pull_request.go index 0014e21..b350730 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -765,11 +765,6 @@ func (h *PullRequestHandler) writeAndCommitTree( return nil, nil, errors.Wrapf(err, "Failed to parse diff-tree entry to %s for %s", op, pr.GetHTMLURL()) } - if entry == nil { - logger.Debug().Msgf("Invalid diff-tree line %s", line) - continue - } - tree.Entries = append(tree.Entries, entry) } From 6c6888d13cd0d4beef096e4cfc0393bc417ec55f Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:14:59 -0500 Subject: [PATCH 16/21] remove extra debug logging Signed-off-by: Andrew Mason --- go/pull_request.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/go/pull_request.go b/go/pull_request.go index b350730..7ea542a 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -743,15 +743,12 @@ func (h *PullRequestHandler) writeAndCommitTree( commitMsg string, op string, ) (*github.Tree, *github.Commit, error) { - logger := zerolog.Ctx(ctx) - out, err := repo.DiffTree(ctx, baseRef, headRef, git.DiffTreeOpts{Recursive: true}) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to diff-tree %s %s in %s/%s to %s for %s", baseRef, headRef, repo.Owner, repo.Name, op, pr.GetHTMLURL()) } lines := bytes.Split(out, []byte{'\n'}) - logger.Debug().Msgf("Found %d entries in diff from %s to %s", len(lines), baseRef, headRef) var tree = &github.Tree{} @@ -768,7 +765,6 @@ func (h *PullRequestHandler) writeAndCommitTree( tree.Entries = append(tree.Entries, entry) } - logger.Debug().Msgf("Creating tree with %d entries based on %s", len(tree.Entries), baseTree) tree, _, err = client.Git.CreateTree(ctx, repo.Owner, repo.Name, baseTree, tree.Entries) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to create tree based on %s to %s for %s", baseTree, op, pr.GetHTMLURL()) @@ -781,7 +777,7 @@ func (h *PullRequestHandler) writeAndCommitTree( {SHA: &parentCommit}, }, } - logger.Debug().Msgf("Authoring commit %q with tree %s with parent %s", commitMsg, tree.GetSHA(), parentCommit) + commit, _, err = client.Git.CreateCommit(ctx, repo.Owner, repo.Name, commit) if err != nil { return nil, nil, errors.Wrapf(err, "Failed to create commit based on %s to %s for %s", parentCommit, op, pr.GetHTMLURL()) From fee767bd8f62373edf9ea3efb032c6a5c93aa92d Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:23:55 -0500 Subject: [PATCH 17/21] fixup! slightly better commit messages Signed-off-by: Andrew Mason --- go/pull_request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/pull_request.go b/go/pull_request.go index 7ea542a..95f4d5f 100644 --- a/go/pull_request.go +++ b/go/pull_request.go @@ -682,7 +682,7 @@ func (h *PullRequestHandler) createCobraDocsPreviewPR( commit.GetSHA(), fmt.Sprintf( "Generate cobradocs preview against %s/%s:%s", - pr.GetHead().GetRepo().GetOwner(), + pr.GetHead().GetRepo().GetOwner().GetLogin(), pr.GetHead().GetRepo().GetName(), pr.GetHead().GetRef(), ), From 0878b786054fa12b2bf969ba837d0633942ae7da Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 11:37:19 -0500 Subject: [PATCH 18/21] add missing license headers Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 16 ++++++++++++++++ go/git/diff_tree_test.go | 16 ++++++++++++++++ go/shell/shell_darwin.go | 16 ++++++++++++++++ go/shell/shell_unix.go | 16 ++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go index df420c1..513fb96 100644 --- a/go/git/diff_tree.go +++ b/go/git/diff_tree.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package git import ( diff --git a/go/git/diff_tree_test.go b/go/git/diff_tree_test.go index eea66c7..d04f314 100644 --- a/go/git/diff_tree_test.go +++ b/go/git/diff_tree_test.go @@ -1,3 +1,19 @@ +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package git import ( diff --git a/go/shell/shell_darwin.go b/go/shell/shell_darwin.go index 769a9b8..bb9f150 100644 --- a/go/shell/shell_darwin.go +++ b/go/shell/shell_darwin.go @@ -1,5 +1,21 @@ //go:build darwin +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package shell func init() { diff --git a/go/shell/shell_unix.go b/go/shell/shell_unix.go index 924a719..aa2e779 100644 --- a/go/shell/shell_unix.go +++ b/go/shell/shell_unix.go @@ -1,5 +1,21 @@ //go:build unix && !darwin +/* +Copyright 2024 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package shell func init() { From 39aa8b8e94c2bce4aff4661ca682051eacd92cd1 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 14:20:05 -0500 Subject: [PATCH 19/21] more comments Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go index 513fb96..bf66aa3 100644 --- a/go/git/diff_tree.go +++ b/go/git/diff_tree.go @@ -26,14 +26,18 @@ import ( ) /* +Example output of `git diff-tree -r HEAD~1 HEAD` in a sample repo: + :100644 000000 5716ca5987cbf97d6bb54920bea6adde242d87e6 0000000000000000000000000000000000000000 D bar/bar.txt :000000 100644 0000000000000000000000000000000000000000 76018072e09c5d31c8c6e3113b8aa0fe625195ca A baz.txt :100644 100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 b210800439ffe3f2db0d47d9aab1969b38a770a5 M foo.txt */ - var diffTreeEntryRegexp = regexp.MustCompile(`^:(?P\d{6}) (?P\d{6}) (?P[a-f0-9]{40}) (?P[a-f0-9]{40}) [A-Z]\W(?P.*)$`) -// See https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree +// ParseDiffTreeEntry parses a single line from `git diff-tree A B` into a +// TreeEntry object suitable to pass to github's CreateTree method. +// +// See https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree. func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, error) { match := diffTreeEntryRegexp.FindStringSubmatch(line) if match == nil { From e0336583d699bf86a0e1b175d86de46558a81663 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Sat, 27 Jan 2024 14:29:21 -0500 Subject: [PATCH 20/21] add unit tests Signed-off-by: Andrew Mason --- go.mod | 5 +++++ go.sum | 17 +++++++++++++++++ go/git/diff_tree_test.go | 25 ++++++++++++++++++++----- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 8707f1f..2bbc4e0 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect github.com/bradleyfalzon/ghinstallation/v2 v2.5.0 // indirect github.com/cloudflare/circl v1.3.3 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-querystring v1.1.0 // indirect @@ -23,12 +24,16 @@ require ( github.com/mattn/go-colorable v0.1.12 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 // indirect github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect + github.com/stretchr/objx v0.5.0 // indirect + github.com/stretchr/testify v1.8.4 // indirect golang.org/x/crypto v0.10.0 // indirect golang.org/x/net v0.11.0 // indirect golang.org/x/oauth2 v0.9.0 // indirect golang.org/x/sys v0.9.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.28.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ed469bc..74c92de 100644 --- a/go.sum +++ b/go.sum @@ -9,6 +9,9 @@ github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtM github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= @@ -42,6 +45,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= @@ -51,6 +56,14 @@ github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 h1:nCBaIs5/R0HFP github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo= github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk= github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -117,3 +130,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/go/git/diff_tree_test.go b/go/git/diff_tree_test.go index d04f314..d1cc1d4 100644 --- a/go/git/diff_tree_test.go +++ b/go/git/diff_tree_test.go @@ -17,15 +17,20 @@ limitations under the License. package git import ( + "os" + "path/filepath" "testing" "github.com/google/go-github/v53/github" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestParseDiffTreeEntry(t *testing.T) { - // TODO: create fake filesystem - // baz.txt contains: baz - // foo.txt contains foo\nfoo2 after the edit + tmp := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(tmp, "baz.txt"), []byte("baz"), 0644)) + require.NoError(t, os.WriteFile(filepath.Join(tmp, "foo.txt"), []byte("foo"), 0644)) + tcases := []struct { name string in string @@ -62,15 +67,25 @@ func TestParseDiffTreeEntry(t *testing.T) { Content: github.String("foo"), }, }, + { + name: "empty line", + in: "", + want: nil, + wantErr: true, + }, } for _, tc := range tcases { tc := tc t.Run(tc.name, func(t *testing.T) { - // entry, err := ParseDiffTreeEntry(tc.in, "TODO") + entry, err := ParseDiffTreeEntry(tc.in, tmp) if tc.wantErr { - // TODO: + assert.Error(t, err) + return } + + require.NoError(t, err) + assert.Equal(t, tc.want, entry) }) } } From a77b323b8cc4138132603959488c01d3bca1fff3 Mon Sep 17 00:00:00 2001 From: Andrew Mason Date: Mon, 29 Jan 2024 10:47:34 -0500 Subject: [PATCH 21/21] Update go/git/diff_tree.go Co-authored-by: Florent Poinsard <35779988+frouioui@users.noreply.github.com> Signed-off-by: Andrew Mason --- go/git/diff_tree.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/go/git/diff_tree.go b/go/git/diff_tree.go index bf66aa3..26c6158 100644 --- a/go/git/diff_tree.go +++ b/go/git/diff_tree.go @@ -46,8 +46,6 @@ func ParseDiffTreeEntry(line string, basedir string) (*github.TreeEntry, error) oldMode := match[1] newMode := match[2] - // oldSHA := match[3] - // newSHA := match[4] path := match[5] entry := github.TreeEntry{