Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathgit_refs.go
More file actions
Latest commit
187 lines (158 loc) · 5.34 KB
/
Copy pathgit_refs.go
File metadata and controls
187 lines (158 loc) · 5.34 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
)
// Reference represents a GitHub reference.
typeReferencestruct {
// The name of the fully qualified reference, i.e.: `refs/heads/master`.
Ref*string`json:"ref"`
URL*string`json:"url"`
Object*GitObject`json:"object"`
NodeID*string`json:"node_id,omitempty"`
}
func (rReference) String() string {
returnStringify(r)
}
// GitObject represents a Git object.
typeGitObjectstruct {
Type*string`json:"type"`
SHA*string`json:"sha"`
URL*string`json:"url"`
}
func (oGitObject) String() string {
returnStringify(o)
}
// CreateRef represents the payload for creating a reference.
typeCreateRefstruct {
Refstring`json:"ref"`
SHAstring`json:"sha"`
}
// UpdateRef represents the payload for updating a reference.
typeUpdateRefstruct {
SHAstring`json:"sha"`
Force*bool`json:"force,omitempty"`
}
// GetRef fetches a single reference in a repository.
// The ref must be formatted as `heads/<branch name>` for branches and `tags/<tag name>` for tags.
//
// GitHub API docs: https://docs.github.com/rest/git/refs?apiVersion=2022-11-28#get-a-reference
//
//meta:operation GET /repos/{owner}/{repo}/git/ref/{ref}
func (s*GitService) GetRef(ctx context.Context, owner, repo, refstring) (*Reference, *Response, error) {
ref=strings.TrimPrefix(ref, "refs/")
u:=fmt.Sprintf("repos/%v/%v/git/ref/%v", owner, repo, refURLEscape(ref))
req, err:=s.client.NewRequest(ctx, "GET", u, nil)
iferr!=nil {
returnnil, nil, err
}
varr*Reference
resp, err:=s.client.Do(req, &r)
iferr!=nil {
returnnil, resp, err
}
returnr, resp, nil
}
// refURLEscape escapes every path segment of the given ref. Those must
// not contain escaped "/" - as "%2F" - or github will not recognize it.
funcrefURLEscape(refstring) string {
parts:=strings.Split(ref, "/")
fori, s:=rangeparts {
parts[i] =url.PathEscape(s)
}
returnstrings.Join(parts, "/")
}
// ListMatchingRefs lists references in a repository that match a supplied ref.
// The ref in the URL must be formatted as `heads/<branch name>` for branches and `tags/<tag name>` for tags.
// If the ref doesn't exist in the repository, but existing refs start with ref, they will be returned as an array.
// Use an empty ref to list all references.
//
// GitHub API docs: https://docs.github.com/rest/git/refs?apiVersion=2022-11-28#list-matching-references
//
//meta:operation GET /repos/{owner}/{repo}/git/matching-refs/{ref}
func (s*GitService) ListMatchingRefs(ctx context.Context, owner, repo, refstring) ([]*Reference, *Response, error) {
ref=strings.TrimPrefix(ref, "refs/") // API expects no "refs/" prefix
u:=fmt.Sprintf("repos/%v/%v/git/matching-refs/%v", owner, repo, refURLEscape(ref))
req, err:=s.client.NewRequest(ctx, "GET", u, nil)
iferr!=nil {
returnnil, nil, err
}
varrs []*Reference
resp, err:=s.client.Do(req, &rs)
iferr!=nil {
returnnil, resp, err
}
returnrs, resp, nil
}
// CreateRef creates a new ref in a repository.
//
// GitHub API docs: https://docs.github.com/rest/git/refs?apiVersion=2022-11-28#create-a-reference
//
//meta:operation POST /repos/{owner}/{repo}/git/refs
func (s*GitService) CreateRef(ctx context.Context, owner, repostring, bodyCreateRef) (*Reference, *Response, error) {
ifbody.Ref=="" {
returnnil, nil, errors.New("ref must be provided")
}
ifbody.SHA=="" {
returnnil, nil, errors.New("sha must be provided")
}
// ensure the 'refs/' prefix is present
body.Ref="refs/"+strings.TrimPrefix(body.Ref, "refs/")
u:=fmt.Sprintf("repos/%v/%v/git/refs", owner, repo)
req, err:=s.client.NewRequest(ctx, "POST", u, body)
iferr!=nil {
returnnil, nil, err
}
varr*Reference
resp, err:=s.client.Do(req, &r)
iferr!=nil {
returnnil, resp, err
}
returnr, resp, nil
}
// UpdateRef updates an existing ref in a repository.
//
// GitHub API docs: https://docs.github.com/rest/git/refs?apiVersion=2022-11-28#update-a-reference
//
//meta:operation PATCH /repos/{owner}/{repo}/git/refs/{ref}
func (s*GitService) UpdateRef(ctx context.Context, owner, repo, refstring, bodyUpdateRef) (*Reference, *Response, error) {
ifref=="" {
returnnil, nil, errors.New("ref must be provided")
}
ifbody.SHA=="" {
returnnil, nil, errors.New("sha must be provided")
}
refPath:=strings.TrimPrefix(ref, "refs/")
u:=fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refURLEscape(refPath))
req, err:=s.client.NewRequest(ctx, "PATCH", u, body)
iferr!=nil {
returnnil, nil, err
}
varr*Reference
resp, err:=s.client.Do(req, &r)
iferr!=nil {
returnnil, resp, err
}
returnr, resp, nil
}
// DeleteRef deletes a ref from a repository.
//
// GitHub API docs: https://docs.github.com/rest/git/refs?apiVersion=2022-11-28#delete-a-reference
//
//meta:operation DELETE /repos/{owner}/{repo}/git/refs/{ref}
func (s*GitService) DeleteRef(ctx context.Context, owner, repo, refstring) (*Response, error) {
ref=strings.TrimPrefix(ref, "refs/")
u:=fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refURLEscape(ref))
req, err:=s.client.NewRequest(ctx, "DELETE", u, nil)
iferr!=nil {
returnnil, err
}
returns.client.Do(req, nil)
}