Skip to content

Commit

Permalink
backend: Add delete file method to GitHub (#3100)
Browse files Browse the repository at this point in the history
  • Loading branch information
septum authored Aug 29, 2024
1 parent 81144d6 commit 8c59b7b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions backend/mock/service/githubmock/githubmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
14 changes: 14 additions & 0 deletions backend/service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
73 changes: 73 additions & 0 deletions backend/service/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions backend/service/github/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8c59b7b

Please sign in to comment.