forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_branch_issue_reference.go
145 lines (123 loc) · 3.33 KB
/
queries_branch_issue_reference.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
package api
import (
"fmt"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/shurcooL/githubv4"
)
type LinkedBranch struct {
BranchName string
URL string
}
func CreateLinkedBranch(client *Client, host string, repoID, issueID, branchID, branchName string) (string, error) {
var mutation struct {
CreateLinkedBranch struct {
LinkedBranch struct {
ID string
Ref struct {
Name string
}
}
} `graphql:"createLinkedBranch(input: $input)"`
}
input := githubv4.CreateLinkedBranchInput{
IssueID: githubv4.ID(issueID),
Oid: githubv4.GitObjectID(branchID),
}
if repoID != "" {
repo := githubv4.ID(repoID)
input.RepositoryID = &repo
}
if branchName != "" {
name := githubv4.String(branchName)
input.Name = &name
}
variables := map[string]interface{}{
"input": input,
}
err := client.Mutate(host, "CreateLinkedBranch", &mutation, variables)
if err != nil {
return "", err
}
return mutation.CreateLinkedBranch.LinkedBranch.Ref.Name, nil
}
func ListLinkedBranches(client *Client, repo ghrepo.Interface, issueNumber int) ([]LinkedBranch, error) {
var query struct {
Repository struct {
Issue struct {
LinkedBranches struct {
Nodes []struct {
Ref struct {
Name string
Repository struct {
Url string
}
}
}
} `graphql:"linkedBranches(first: 30)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"number": githubv4.Int(issueNumber),
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
}
if err := client.Query(repo.RepoHost(), "ListLinkedBranches", &query, variables); err != nil {
return []LinkedBranch{}, err
}
var branchNames []LinkedBranch
for _, node := range query.Repository.Issue.LinkedBranches.Nodes {
branch := LinkedBranch{
BranchName: node.Ref.Name,
URL: fmt.Sprintf("%s/tree/%s", node.Ref.Repository.Url, node.Ref.Name),
}
branchNames = append(branchNames, branch)
}
return branchNames, nil
}
func CheckLinkedBranchFeature(client *Client, host string) error {
var query struct {
Name struct {
Fields []struct {
Name string
}
} `graphql:"LinkedBranch: __type(name: \"LinkedBranch\")"`
}
if err := client.Query(host, "LinkedBranchFeature", &query, nil); err != nil {
return err
}
if len(query.Name.Fields) == 0 {
return fmt.Errorf("the `gh issue develop` command is not currently available")
}
return nil
}
func FindRepoBranchID(client *Client, repo ghrepo.Interface, ref string) (string, string, error) {
var query struct {
Repository struct {
Id string
DefaultBranchRef struct {
Target struct {
Oid string
}
}
Ref struct {
Target struct {
Oid string
}
} `graphql:"ref(qualifiedName: $ref)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"ref": githubv4.String(ref),
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
}
if err := client.Query(repo.RepoHost(), "FindRepoBranchID", &query, variables); err != nil {
return "", "", err
}
branchID := query.Repository.Ref.Target.Oid
if branchID == "" {
branchID = query.Repository.DefaultBranchRef.Target.Oid
}
return query.Repository.Id, branchID, nil
}