Skip to content

Commit

Permalink
If applied, this commit will add some GoDocs and apply suggestions fr…
Browse files Browse the repository at this point in the history
…om code review

Signed-off-by: Rana Tarek Hassan <[email protected]>
  • Loading branch information
ranatrk committed Jul 19, 2022
1 parent e2b6b9f commit 63f6a3a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
24 changes: 6 additions & 18 deletions github/client_repository_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,30 @@ func (c *TreeClient) Create(ctx context.Context, tree *gitprovider.TreeInfo) (*g
return &responseTreeInfo, nil
}

// Get returns a tree
// Get returns a single tree using the SHA1 value for that tree.
func (c *TreeClient) Get(ctx context.Context, sha string, recursive bool) (*gitprovider.TreeInfo, error) {
// GET /repos/{owner}/{repo}/git/trees
repoName := c.ref.GetRepository()
repoOwner := c.ref.GetIdentity()
githubTree, _, err := c.c.Client().Git.GetTree(ctx, repoOwner, repoName, sha, true)
githubTree, _, err := c.c.Client().Git.GetTree(ctx, repoOwner, repoName, sha, recursive)
if err != nil {
return nil, err
}

treeEntries := make([]*gitprovider.TreeEntry, 0)
for _, treeEntry := range githubTree.Entries {
treeEntries := make([]*gitprovider.TreeEntry, len(githubTree.Entries))
for ind, treeEntry := range githubTree.Entries {
size := 0
if *treeEntry.Type != "tree" {
size = *treeEntry.Size
}
treeEntries = append(treeEntries, &gitprovider.TreeEntry{
treeEntries[ind] = &gitprovider.TreeEntry{
Path: *treeEntry.Path,
Mode: *treeEntry.Mode,
Type: *treeEntry.Type,
Size: size,
SHA: *treeEntry.SHA,
URL: *treeEntry.URL,
})
}
}

treeInfo := gitprovider.TreeInfo{
Expand Down Expand Up @@ -136,15 +136,3 @@ func (c *TreeClient) List(ctx context.Context, sha string, recursive bool) ([]*g

return treeEntries, nil
}

func createTreeEntry(githubTreeEntry github.TreeEntry) *gitprovider.TreeEntry {
newTreeEntry := gitprovider.TreeEntry{
Path: *githubTreeEntry.Path,
Mode: *githubTreeEntry.Mode,
Type: *githubTreeEntry.Type,
Size: *githubTreeEntry.Size,
SHA: *githubTreeEntry.SHA,
URL: *githubTreeEntry.URL,
}
return &newTreeEntry
}
4 changes: 2 additions & 2 deletions github/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ var _ = Describe("GitHub Provider", func() {
if treeEntry.Path == "LICENSE" || treeEntry.Path == "README.md" {
continue
}
Expect(*&treeEntry.Path).To(Equal(*files[ind-itemsToBeIgnored].Path))
Expect(treeEntry.Path).To(Equal(*files[ind-itemsToBeIgnored].Path))
continue

}
Expand All @@ -638,7 +638,7 @@ var _ = Describe("GitHub Provider", func() {
if treeEntry.Path == "LICENSE" || treeEntry.Path == "README.md" {
continue
}
Expect(*&treeEntry.Path).To(Equal(*files[ind-2].Path))
Expect(treeEntry.Path).To(Equal(*files[ind-2].Path))
}

})
Expand Down
2 changes: 2 additions & 0 deletions gitprovider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ type FileClient interface {
Get(ctx context.Context, path, branch string, optFns ...FilesGetOption) ([]*CommitFile, error)
}

// TreeClient operates on the trees for a Git repository which describe the hierarchy between files in the repository
// This client can be accessed through Repository.Trees()
type TreeClient interface {
// Create allows for creating or editing tree
Create(ctx context.Context, tree *TreeInfo) (*TreeInfo, error)
Expand Down
7 changes: 4 additions & 3 deletions gitprovider/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,14 @@ type FilesGetOptions struct {
Recursive bool
}

// FilesGetOption is an interface for applying options when fetching/getting files
type FilesGetOption interface {
ApplyFilesGetOptions(target *FilesGetOptions)
}

// ApplyFilesGetOptions applies target options onto the invoked opts
func (opts *FilesGetOptions) ApplyFilesGetOptions(target *FilesGetOptions) {
// Go through each field in opts, and apply it to target if set
if opts.Recursive == true {
target.Recursive = opts.Recursive
}
target.Recursive = opts.Recursive

}
1 change: 1 addition & 0 deletions gitprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ type PullRequest interface {
Get() PullRequestInfo
}

// Tree represents a git tree which represents the hierarchy between files in a Git repository
type Tree interface {
// Object implements the Object interface,
// allowing access to the underlying object returned from the API.
Expand Down
26 changes: 20 additions & 6 deletions gitprovider/types_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,16 +226,30 @@ type PullRequestInfo struct {
WebURL string `json:"web_url"`
}

// TreeEntry contains info about each tree object's structure in TreeInfo whether it is a file or tree
type TreeEntry struct {
// Path is the path of the file/blob or sub tree in a tree
Path string `json:"path"`
// Mode of the file/tree.
// (100644:file (blob), 100755:executable (blob), 040000:subdirectory(tree),160000:submodule(commit),120000:blob that specifies the path of a symlink)
Mode string `json:"mode"`
// Type is the item type, It is either blob, tree, or commit.
Type string `json:"type"`
Size int `json:"size"`
SHA string `json:"sha"`
URL string `json:"url"`
// Size is the size of the file/blob if the type is a blob, it is not populated if the type is a tree
Size int `json:"size"`
// SHA is the SHA1 checksum ID of the object in the tree
SHA string `json:"sha"`
// URL is the url that can be used to retrieve the details of the blob, tree of commit
URL string `json:"url"`
}

// TreeInfo contains high-level information about a git Tree representing the hierarchy between files in a Git repository
type TreeInfo struct {
SHA string `json:"sha"`
Tree []*TreeEntry `json:"tree"`
Truncated bool `json:"truncated"`
// SHA is the SHA1 checksum ID of the tree, or the branch name
SHA string `json:"sha"`
// Tree is the list of TreeEntry objects describing the structure of the tree
Tree []*TreeEntry `json:"tree"`
// Truncated represents whether a tree is truncated when fetching a tree
// If truncated is true in the response when fetching a tree, then the number of items in the tree array exceeded the maximum limit
Truncated bool `json:"truncated"`
}

0 comments on commit 63f6a3a

Please sign in to comment.