Unverified Commit d3d23f2b authored by Angela Nguyen's avatar Angela Nguyen Committed by GitHub
Browse files

github: add function for retrieving repository info (#768)

parent c56a4663
Showing with 102 additions and 0 deletions
+102 -0
......@@ -47,6 +47,10 @@ func (s svc) GetCommit(ctx context.Context, ref *github.RemoteRef) (*github.Comm
panic("implement me")
}
func (s svc) GetRepository(ctx context.Context, ref *github.RemoteRef) (*github.Repository, error) {
panic("implement me")
}
func NewAsService(*any.Any, *zap.Logger, tally.Scope) (service.Service, error) {
return New(), nil
}
......@@ -56,6 +56,13 @@ type RemoteRef struct {
Ref string
}
// Repository contains information about a requested repository.
type Repository struct {
Name string
Owner string
DefaultBranch string
}
// File contains information about a requested file, including its content.
type File struct {
Path string
......@@ -74,6 +81,7 @@ type Client interface {
CreateIssueComment(ctx context.Context, ref *RemoteRef, number int, body string) error
CompareCommits(ctx context.Context, ref *RemoteRef, compareSHA string) (*scgithubv1.CommitComparison, error)
GetCommit(ctx context.Context, ref *RemoteRef) (*Commit, error)
GetRepository(ctx context.Context, ref *RemoteRef) (*Repository, error)
}
// This func can be used to create comments for PRs or Issues
......@@ -306,3 +314,24 @@ func (s *svc) GetCommit(ctx context.Context, ref *RemoteRef) (*Commit, error) {
Files: commit.Files,
}, nil
}
func (s *svc) GetRepository(ctx context.Context, repo *RemoteRef) (*Repository, error) {
q := &getRepositoryQuery{}
params := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner),
"name": githubv4.String(repo.RepoName),
}
err := s.graphQL.Query(ctx, q, params)
if err != nil {
return nil, err
}
r := &Repository{
Name: repo.RepoName,
Owner: repo.RepoOwner,
DefaultBranch: string(q.Repository.DefaultBranchRef.Name),
}
return r, nil
}
......@@ -350,3 +350,64 @@ func TestGetCommit(t *testing.T) {
})
}
}
type getRepositoryMock struct {
v4client
branchName string
}
func (g *getRepositoryMock) Query(ctx context.Context, query interface{}, variables map[string]interface{}) error {
q, ok := query.(*getRepositoryQuery)
if !ok {
panic("not a query")
}
q.Repository.DefaultBranchRef.Name = g.branchName
return nil
}
var getDefaultBranchTests = []struct {
name string
v4 getRepositoryMock
wantDefaultBranch string
}{
{
name: "1. default repo with main branch",
v4: getRepositoryMock{branchName: "main"},
wantDefaultBranch: "main",
},
{
name: "2. default repo with master branch",
v4: getRepositoryMock{branchName: "master"},
wantDefaultBranch: "master",
},
}
func TestGetRepository(t *testing.T) {
t.Parallel()
for _, tt := range getDefaultBranchTests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
a := assert.New(t)
s := &svc{graphQL: &tt.v4}
repo, err := s.GetRepository(context.Background(),
&RemoteRef{
RepoOwner: "owner",
RepoName: "myRepo",
},
)
if err != nil {
a.FailNowf("unexpected error: %s", err.Error())
return
}
gotDefaultBranch := repo.DefaultBranch
a.Equal(gotDefaultBranch, tt.wantDefaultBranch)
a.Nil(err)
})
}
}
......@@ -36,3 +36,11 @@ type getFileQuery struct {
} `graphql:"object(expression:$refPath)"`
} `graphql:"repository(owner:$owner,name:$name)"`
}
type getRepositoryQuery struct {
Repository struct {
DefaultBranchRef struct {
Name string
}
} `graphql:"repository(owner:$owner,name:$name)"`
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment