-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
180 lines (148 loc) · 3.99 KB
/
github.go
File metadata and controls
180 lines (148 loc) · 3.99 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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
)
var httpClient = http.DefaultClient
var githubAPIBaseURL = "https://api.github.com"
type GitHubRepo struct {
DefaultBranch string `json:"default_branch"`
}
type GitHubBranch struct {
Commit GitHubCommit `json:"commit"`
}
type GitHubCommit struct {
SHA string `json:"sha"`
}
type GitHubRef struct {
Object struct {
SHA string `json:"sha"`
} `json:"object"`
}
func parseGitHubURL(url string) (owner, repo string, err error) {
// Handle github.com/owner/repo format
re := regexp.MustCompile(`^github\.com/([^/]+)/([^/]+)/?$`)
matches := re.FindStringSubmatch(url)
if len(matches) != 3 {
return "", "", fmt.Errorf("invalid GitHub URL format")
}
return matches[1], matches[2], nil
}
func parseGitHubSpec(spec string) (repoURL, ref string, err error) {
parts := strings.Split(spec, "@")
if len(parts) == 1 {
return parts[0], "", nil
}
if len(parts) == 2 {
return parts[0], parts[1], nil
}
return "", "", fmt.Errorf("invalid spec format")
}
func resolveRef(owner, repo, ref string) (sha, resolvedRef string, err error) {
if ref == "" {
// Get default branch
return getLatestCommitSHA(owner, repo)
}
// Check if it's already a commit SHA (40 hex characters)
if matched, _ := regexp.MatchString("^[a-f0-9]{40}$", ref); matched {
return ref, ref, nil
}
// Try as a branch first
sha, resolvedRef, err = getBranchCommitSHA(owner, repo, ref)
if err == nil {
return sha, resolvedRef, nil
}
// Try as a tag
sha, err = getTagCommitSHA(owner, repo, ref)
if err == nil {
return sha, ref, nil
}
return "", "", fmt.Errorf("could not resolve ref '%s' as branch or tag", ref)
}
func getLatestCommitSHA(owner, repo string) (sha, defaultBranch string, err error) {
// First get the default branch
repoURL := fmt.Sprintf("%s/repos/%s/%s", githubAPIBaseURL, owner, repo)
resp, err := httpClient.Get(repoURL)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", "", fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
var repoInfo GitHubRepo
err = json.Unmarshal(body, &repoInfo)
if err != nil {
return "", "", err
}
// Now get the latest commit from the default branch
branchURL := fmt.Sprintf("%s/repos/%s/%s/branches/%s", githubAPIBaseURL, owner, repo, repoInfo.DefaultBranch)
resp, err = httpClient.Get(branchURL)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", "", fmt.Errorf("GitHub API returned status %d for branch", resp.StatusCode)
}
body, err = io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
var branchInfo GitHubBranch
err = json.Unmarshal(body, &branchInfo)
if err != nil {
return "", "", err
}
return branchInfo.Commit.SHA, repoInfo.DefaultBranch, nil
}
func getBranchCommitSHA(owner, repo, branch string) (sha, resolvedRef string, err error) {
branchURL := fmt.Sprintf("%s/repos/%s/%s/branches/%s", githubAPIBaseURL, owner, repo, branch)
resp, err := httpClient.Get(branchURL)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", "", fmt.Errorf("branch not found")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", "", err
}
var branchInfo GitHubBranch
err = json.Unmarshal(body, &branchInfo)
if err != nil {
return "", "", err
}
return branchInfo.Commit.SHA, branch, nil
}
func getTagCommitSHA(owner, repo, tag string) (string, error) {
tagURL := fmt.Sprintf("%s/repos/%s/%s/git/refs/tags/%s", githubAPIBaseURL, owner, repo, tag)
resp, err := httpClient.Get(tagURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("tag not found")
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var refInfo GitHubRef
err = json.Unmarshal(body, &refInfo)
if err != nil {
return "", err
}
return refInfo.Object.SHA, nil
}