Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Clutch
Commits
d3d23f2b
Unverified
Commit
d3d23f2b
authored
4 years ago
by
Angela Nguyen
Committed by
GitHub
4 years ago
Browse files
Options
Download
Email Patches
Plain Diff
github: add function for retrieving repository info (#768)
parent
c56a4663
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
backend/mock/service/githubmock/githubmock.go
+4
-0
backend/mock/service/githubmock/githubmock.go
backend/service/github/github.go
+29
-0
backend/service/github/github.go
backend/service/github/github_test.go
+61
-0
backend/service/github/github_test.go
backend/service/github/graphql.go
+8
-0
backend/service/github/graphql.go
with
102 additions
and
0 deletions
+102
-0
backend/mock/service/githubmock/githubmock.go
+
4
-
0
View file @
d3d23f2b
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
backend/service/github/github.go
+
29
-
0
View file @
d3d23f2b
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
backend/service/github/github_test.go
+
61
-
0
View file @
d3d23f2b
...
...
@@ -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
)
})
}
}
This diff is collapsed.
Click to expand it.
backend/service/github/graphql.go
+
8
-
0
View file @
d3d23f2b
...
...
@@ -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)"`
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment