/
branch.go
169 lines (156 loc) · 5.3 KB
/
branch.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package github
import (
"context"
"fmt"
"github.com/int128/ghcp/pkg/git"
"github.com/shurcooL/githubv4"
)
type QueryForCommitInput struct {
ParentRepository git.RepositoryID
ParentRef git.RefName // optional
TargetRepository git.RepositoryID
TargetBranchName git.BranchName // optional
}
type QueryForCommitOutput struct {
CurrentUserName string
ParentDefaultBranchCommitSHA git.CommitSHA
ParentDefaultBranchTreeSHA git.TreeSHA
ParentRefCommitSHA git.CommitSHA // empty if the parent ref does not exist
ParentRefTreeSHA git.TreeSHA // empty if the parent ref does not exist
TargetRepositoryNodeID InternalRepositoryNodeID
TargetBranchNodeID InternalBranchNodeID
TargetBranchCommitSHA git.CommitSHA // empty if the branch does not exist
TargetBranchTreeSHA git.TreeSHA // empty if the branch does not exist
}
func (q *QueryForCommitOutput) TargetBranchExists() bool {
return q.TargetBranchCommitSHA != ""
}
// QueryForCommit returns the repository for creating or updating the branch.
func (c *GitHub) QueryForCommit(ctx context.Context, in QueryForCommitInput) (*QueryForCommitOutput, error) {
var q struct {
Viewer struct {
Login string
}
ParentRepository struct {
// default branch
DefaultBranchRef struct {
Name string
Target struct {
Commit struct {
Oid string
Tree struct {
Oid string
}
} `graphql:"... on Commit"`
}
}
// parent ref (optional)
ParentRef struct {
Prefix string
Name string
Target struct {
Commit struct {
Oid string
Tree struct {
Oid string
}
} `graphql:"... on Commit"`
}
} `graphql:"parentRef: ref(qualifiedName: $parentRef)"`
} `graphql:"parentRepository: repository(owner: $parentOwner, name: $parentRepo)"`
TargetRepository struct {
ID githubv4.ID
Ref struct {
ID githubv4.ID
Target struct {
Commit struct {
Oid string
Tree struct {
Oid string
}
} `graphql:"... on Commit"`
}
} `graphql:"ref(qualifiedName: $targetRef)"`
} `graphql:"targetRepository: repository(owner: $targetOwner, name: $targetRepo)"`
}
v := map[string]interface{}{
"parentOwner": githubv4.String(in.ParentRepository.Owner),
"parentRepo": githubv4.String(in.ParentRepository.Name),
"parentRef": githubv4.String(in.ParentRef),
"targetOwner": githubv4.String(in.TargetRepository.Owner),
"targetRepo": githubv4.String(in.TargetRepository.Name),
"targetRef": githubv4.String(in.TargetBranchName.QualifiedName().String()),
}
c.Logger.Debugf("Querying the repository with %+v", v)
if err := c.Client.Query(ctx, &q, v); err != nil {
return nil, fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", q)
out := QueryForCommitOutput{
CurrentUserName: q.Viewer.Login,
ParentDefaultBranchCommitSHA: git.CommitSHA(q.ParentRepository.DefaultBranchRef.Target.Commit.Oid),
ParentDefaultBranchTreeSHA: git.TreeSHA(q.ParentRepository.DefaultBranchRef.Target.Commit.Tree.Oid),
ParentRefCommitSHA: git.CommitSHA(q.ParentRepository.ParentRef.Target.Commit.Oid),
ParentRefTreeSHA: git.TreeSHA(q.ParentRepository.ParentRef.Target.Commit.Tree.Oid),
TargetRepositoryNodeID: q.TargetRepository.ID,
TargetBranchNodeID: q.TargetRepository.Ref.ID,
TargetBranchCommitSHA: git.CommitSHA(q.TargetRepository.Ref.Target.Commit.Oid),
TargetBranchTreeSHA: git.TreeSHA(q.TargetRepository.Ref.Target.Commit.Tree.Oid),
}
c.Logger.Debugf("Returning the repository: %+v", out)
return &out, nil
}
type CreateBranchInput struct {
RepositoryNodeID InternalRepositoryNodeID
BranchName git.BranchName
CommitSHA git.CommitSHA
}
// CreateBranch creates a branch and returns nil or an error.
func (c *GitHub) CreateBranch(ctx context.Context, in CreateBranchInput) error {
// https://docs.github.com/en/graphql/reference/mutations#createref
v := githubv4.CreateRefInput{
RepositoryID: in.RepositoryNodeID,
Name: githubv4.String(in.BranchName.QualifiedName().String()),
Oid: githubv4.GitObjectID(in.CommitSHA),
}
c.Logger.Debugf("Mutation createRef(%+v)", v)
var m struct {
CreateRef struct {
Ref struct {
Name string
}
} `graphql:"createRef(input: $input)"`
}
if err := c.Client.Mutate(ctx, &m, v, nil); err != nil {
return fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", m)
return nil
}
type UpdateBranchInput struct {
BranchRefNodeID InternalBranchNodeID
CommitSHA git.CommitSHA
Force bool
}
// UpdateBranch updates the branch and returns nil or an error.
func (c *GitHub) UpdateBranch(ctx context.Context, in UpdateBranchInput) error {
// https://docs.github.com/en/graphql/reference/mutations#updateref
v := githubv4.UpdateRefInput{
RefID: in.BranchRefNodeID,
Oid: githubv4.GitObjectID(in.CommitSHA),
Force: githubv4.NewBoolean(githubv4.Boolean(in.Force)),
}
c.Logger.Debugf("Mutation updateRef(%+v)", v)
var m struct {
UpdateRef struct {
Ref struct {
Name string
}
} `graphql:"updateRef(input: $input)"`
}
if err := c.Client.Mutate(ctx, &m, v, nil); err != nil {
return fmt.Errorf("GitHub API error: %w", err)
}
c.Logger.Debugf("Got the result: %+v", m)
return nil
}