/
repository.go
54 lines (47 loc) · 1.25 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package model
import (
"fmt"
"github.com/google/go-github/github"
"github.com/piotrpersona/gg/neo"
)
// Repository represents repository as a graph node.
type Repository struct {
RepoID int64
Name string
OwnerID int64
Owner string
}
func (r Repository) String() string {
return fmt.Sprintf("%s/%s", r.Owner, r.Name)
}
// CreateRepository will create model Repository object from GitHub API Repository.
func CreateRepository(ghRepository *github.Repository) Repository {
return Repository{
RepoID: ghRepository.GetID(),
Name: ghRepository.GetName(),
OwnerID: ghRepository.GetOwner().GetID(),
Owner: ghRepository.GetOwner().GetLogin(),
}
}
// ID will return Repository.RepoID
func (r Repository) ID() int64 {
return r.RepoID
}
// Neo is an implementation of neo.Resource interface.
// It will return Repository as neo4j query string.
// There will be created Repository and owner user nodes.
// Repository will be connected with owner node with relation OWNER.
func (r Repository) Neo() neo.Query {
queryString := fmt.Sprintf(
`MERGE (repo:Repository {
ID: %d,
Name: "%s",
})
MERGE (user:User {
ID: %d,
Name: "%s"
})
MERGE (user)-[r:OWNER]-(repo)
`, r.RepoID, r.Name, r.OwnerID, r.Owner)
return neo.Query(queryString)
}