From 8c59b7b4d520ba592b5e9dcc6ad23f867ec46809 Mon Sep 17 00:00:00 2001 From: Orlando Valverde <4467518+septum@users.noreply.github.com> Date: Thu, 29 Aug 2024 11:46:32 -0600 Subject: [PATCH] backend: Add delete file method to GitHub (#3100) --- backend/mock/service/githubmock/githubmock.go | 8 ++ backend/service/github/github.go | 14 ++++ backend/service/github/github_test.go | 73 +++++++++++++++++++ backend/service/github/iface.go | 1 + 4 files changed, 96 insertions(+) diff --git a/backend/mock/service/githubmock/githubmock.go b/backend/mock/service/githubmock/githubmock.go index d01dae344d..93f8d91f57 100644 --- a/backend/mock/service/githubmock/githubmock.go +++ b/backend/mock/service/githubmock/githubmock.go @@ -25,6 +25,14 @@ func (s svc) GetFile(ctx context.Context, ref *github.RemoteRef, path string) (* panic("implement me") } +func (s svc) DeleteFile(ctx context.Context, ref *github.RemoteRef, path, sha, message string) (*githubv3.RepositoryContentResponse, error) { + return &githubv3.RepositoryContentResponse{ + Commit: githubv3.Commit{ + SHA: githubv3.String("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"), + }, + }, nil +} + func (s svc) GetDirectory(ctx context.Context, ref *github.RemoteRef, path string) (*github.Directory, error) { panic("implement me") } diff --git a/backend/service/github/github.go b/backend/service/github/github.go index a96286dd32..d2821f9ca8 100644 --- a/backend/service/github/github.go +++ b/backend/service/github/github.go @@ -126,6 +126,7 @@ type Client interface { GetOrgMembership(ctx context.Context, user, org string) (*githubv3.Membership, error) GetUser(ctx context.Context, username string) (*githubv3.User, error) GetPullRequest(ctx context.Context, owner, repo string, number int) (*githubv3.PullRequest, error) + DeleteFile(ctx context.Context, ref *RemoteRef, path, sha, message string) (*githubv3.RepositoryContentResponse, error) } // This func can be used to create comments for PRs or Issues @@ -601,3 +602,16 @@ func (s *svc) GetRepository(ctx context.Context, repo *RemoteRef) (*Repository, return r, nil } + +func (s *svc) DeleteFile(ctx context.Context, ref *RemoteRef, path, sha, message string) (*githubv3.RepositoryContentResponse, error) { + contentRes, _, err := s.rest.Repositories.DeleteFile(ctx, ref.RepoOwner, ref.RepoName, path, &githubv3.RepositoryContentFileOptions{ + Message: &message, + Branch: &ref.Ref, + SHA: &sha, + }) + + if err != nil { + return nil, err + } + return contentRes, nil +} diff --git a/backend/service/github/github_test.go b/backend/service/github/github_test.go index 7803e884e6..31deb0b2ca 100644 --- a/backend/service/github/github_test.go +++ b/backend/service/github/github_test.go @@ -406,6 +406,18 @@ func (m *mockRepositories) GetCommit(ctx context.Context, owner, repo, sha strin }, nil, nil } +func (m *mockRepositories) DeleteFile(ctx context.Context, owner, repo, path string, opts *githubv3.RepositoryContentFileOptions) (*githubv3.RepositoryContentResponse, *githubv3.Response, error) { + if m.generalError { + return &githubv3.RepositoryContentResponse{}, &githubv3.Response{}, errors.New(problem) + } + return &githubv3.RepositoryContentResponse{ + + Commit: githubv3.Commit{ + SHA: githubv3.String("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"), + }, + }, nil, nil +} + type mockUsers struct { user githubv3.User defaultUser string @@ -1044,6 +1056,67 @@ func TestGetPullRequest(t *testing.T) { } } +var deleteFileTests = []struct { + name string + errorText string + mockRepo *mockRepositories + repoOwner string + repoName string + branchName string + filePath string + fileSha string + commitMessage string + commitSha string +}{ + { + name: "happy path", + mockRepo: &mockRepositories{}, + repoOwner: "my-org", + repoName: "my-repo", + branchName: "my-branch", + commitSha: "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", + }, + { + name: "v3 client error", + mockRepo: &mockRepositories{generalError: true}, + errorText: "we've had a problem", + repoOwner: "my-org", + repoName: "my-repo", + branchName: "my-branch", + commitSha: "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed", + }, +} + +func TestDeleteFile(t *testing.T) { + for _, tt := range deleteFileTests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + s := &svc{rest: v3client{ + Repositories: tt.mockRepo, + }} + + resp, err := s. + DeleteFile( + context.Background(), + &RemoteRef{ + RepoOwner: tt.repoOwner, + RepoName: tt.repoName, + Ref: tt.branchName, + }, tt.filePath, tt.fileSha, tt.commitMessage) + + if tt.errorText != "" { + assert.Error(t, err) + assert.Contains(t, err.Error(), tt.errorText) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.commitSha, resp.GetSHA()) + } + }) + } +} + func TestNewService(t *testing.T) { cfg := &githubconfigv1.Config{} _, err := newService(cfg, tally.NoopScope, zap.NewNop()) diff --git a/backend/service/github/iface.go b/backend/service/github/iface.go index 487a4144f5..24c569c906 100644 --- a/backend/service/github/iface.go +++ b/backend/service/github/iface.go @@ -26,6 +26,7 @@ type v3repositories interface { GetContents(ctx context.Context, owner, repo, path string, opt *githubv3.RepositoryContentGetOptions) (*githubv3.RepositoryContent, []*githubv3.RepositoryContent, *githubv3.Response, error) CompareCommits(ctx context.Context, owner, repo string, base, head string, opts *githubv3.ListOptions) (*githubv3.CommitsComparison, *githubv3.Response, error) GetCommit(ctx context.Context, owner, repo, sha string, opts *githubv3.ListOptions) (*githubv3.RepositoryCommit, *githubv3.Response, error) + DeleteFile(ctx context.Context, owner, repo, path string, opts *githubv3.RepositoryContentFileOptions) (*githubv3.RepositoryContentResponse, *githubv3.Response, error) } // Interface for struct defined in https://github.com/google/go-github/blob/master/github/pulls.go.