Skip to content

Commit f0e62a1

Browse files
authored
More git-related libraries (#30)
1 parent 989c334 commit f0e62a1

File tree

6 files changed

+206
-2
lines changed

6 files changed

+206
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Version changelog
2+
3+
## 0.0.1
4+
5+
Initial version with some tooling.

NOTICE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ This Software contains code from the following projects, licensed under the Data
55
Databricks CLI
66
Copyright (2022) Databricks, Inc.
77
License - https://github.com/databricks/cli/blob/main/LICENSE
8+
9+
This Software contains code from the following projects, licensed under the BSD-style license:
10+
11+
go-github
12+
Copyright 2013 The go-github AUTHORS. All rights reserved.
13+
License - https://github.com/google/go-github/blob/master/LICENSE

go-libs/git/checkout.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (l *Checkout) cmd(ctx context.Context, args ...string) (string, error) {
3636
return strings.TrimSpace(out), nil
3737
}
3838

39+
func (l *Checkout) Dir() string {
40+
return l.dir
41+
}
42+
3943
func (l *Checkout) OrgAndRepo() (string, string, bool) {
4044
tmp := strings.TrimSuffix(l.fetchRemote, ".git")
4145
tmp = strings.TrimPrefix(tmp, "https://github.com/")
@@ -72,8 +76,27 @@ func (l *Checkout) CurrentBranch(ctx context.Context) (string, error) {
7276
return l.cmd(ctx, "branch", "--show-current")
7377
}
7478

79+
func (l *Checkout) DefaultBranch(ctx context.Context) (string, error) {
80+
return l.cmd(ctx, "config", "--get", "init.defaultBranch")
81+
}
82+
7583
func (l *Checkout) CheckoutMain(ctx context.Context) (string, error) {
76-
return l.cmd(ctx, "checkout", "main")
84+
defaultBranch, err := l.DefaultBranch(ctx)
85+
if err != nil {
86+
return "", fmt.Errorf("default branch: %w", err)
87+
}
88+
return l.cmd(ctx, "checkout", defaultBranch)
89+
}
90+
91+
// If prepare/X.X.X does not name an existing branch, this creates the new branch,
92+
// pointing to the current commit, as if by regular git checkout -b. If prepare/X.X.X does name an
93+
// existing branch, what happens instead is that Git forcibly re-points the branch name to the current commit.
94+
//
95+
// This is a lot like a git reset --soft. A branch name is really just a human-readable name for some Git hash ID,
96+
// and a soft reset changes the hash ID attached to the branch name, without touching the index or work-tree. In
97+
// the same way, git checkout -B will change the ID attached to this name, without touching the index or work-tree.
98+
func (l *Checkout) ForceCheckout(ctx context.Context, branch string) (string, error) {
99+
return l.cmd(ctx, "checkout", "-B", branch)
77100
}
78101

79102
func (l *Checkout) ResetHard(ctx context.Context) (string, error) {
@@ -104,6 +127,6 @@ func (l *Checkout) CreateTag(ctx context.Context, v, msg string) (string, error)
104127
return l.cmd(ctx, "tag", v, "-f", "-m", msg)
105128
}
106129

107-
func (l *Checkout) PushTag(ctx context.Context, v string) (string, error) {
130+
func (l *Checkout) ForcePush(ctx context.Context, v string) (string, error) {
108131
return l.cmd(ctx, "push", l.pushRemote, v, "-f")
109132
}

go-libs/github/github.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,25 @@ func (c *GitHubClient) Versions(ctx context.Context, org, repo string) (Versions
6060
return releases, err
6161
}
6262

63+
type CreateReleaseRequest struct {
64+
TagName string `json:"tag_name,omitempty"`
65+
Name string `json:"name,omitempty"`
66+
Body string `json:"body,omitempty"`
67+
Draft bool `json:"draft,omitempty"`
68+
Prerelease bool `json:"prerelease,omitempty"`
69+
GenerateReleaseNotes bool `json:"generate_release_notes,omitempty"`
70+
DiscussionCategoryName string `json:"discussion_category_name,omitempty"`
71+
}
72+
73+
func (c *GitHubClient) CreateRelease(ctx context.Context, org, repo string, req CreateReleaseRequest) (*Release, error) {
74+
var res Release
75+
url := fmt.Sprintf("%s/repos/%s/%s/releases", gitHubAPI, org, repo)
76+
err := c.api.Do(ctx, "POST", url,
77+
httpclient.WithRequestData(req),
78+
httpclient.WithResponseUnmarshal(&res))
79+
return &res, err
80+
}
81+
6382
func (c *GitHubClient) GetRepo(ctx context.Context, org, name string) (repo Repo, err error) {
6483
url := fmt.Sprintf("%s/repos/%s/%s", gitHubAPI, org, name)
6584
err = c.api.Do(ctx, "GET", url, httpclient.WithResponseUnmarshal(&repo))
@@ -91,3 +110,37 @@ func (c *GitHubClient) CompareCommits(ctx context.Context, org, repo, base, head
91110
err := c.api.Do(ctx, "GET", path, httpclient.WithResponseUnmarshal(&response))
92111
return response.Commits, err
93112
}
113+
114+
func (c *GitHubClient) ListPullRequests(ctx context.Context, org, repo string, opts PullRequestListOptions) ([]PullRequest, error) {
115+
path := fmt.Sprintf("%s/repos/%s/%s/pulls", gitHubAPI, org, repo)
116+
var prs []PullRequest
117+
err := c.api.Do(ctx, "GET", path,
118+
httpclient.WithRequestData(opts),
119+
httpclient.WithResponseUnmarshal(&prs))
120+
return prs, err
121+
}
122+
123+
func (c *GitHubClient) EditPullRequest(ctx context.Context, org, repo string, number int, body PullRequestUpdate) error {
124+
path := fmt.Sprintf("%s/repos/%s/%s/pulls/%d", gitHubAPI, org, repo, number)
125+
return c.api.Do(ctx, "PATCH", path, httpclient.WithRequestData(body))
126+
}
127+
128+
func (c *GitHubClient) CreatePullRequest(ctx context.Context, org, repo string, body NewPullRequest) (*PullRequest, error) {
129+
path := fmt.Sprintf("%s/repos/%s/%s/pulls", gitHubAPI, org, repo)
130+
var res PullRequest
131+
err := c.api.Do(ctx, "POST", path,
132+
httpclient.WithRequestData(body),
133+
httpclient.WithResponseUnmarshal(&res))
134+
if err != nil {
135+
return nil, err
136+
}
137+
return &res, nil
138+
}
139+
140+
func (c *GitHubClient) GetPullRequest(ctx context.Context, org, repo string, number int) (*PullRequest, error) {
141+
path := fmt.Sprintf("%s/repos/%s/%s/pulls/%d", gitHubAPI, org, repo, number)
142+
var res PullRequest
143+
err := c.api.Do(ctx, "GET", path,
144+
httpclient.WithResponseUnmarshal(&res))
145+
return &res, err
146+
}

go-libs/github/labels.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package github
2+
3+
type Label struct {
4+
ID int64 `json:"id,omitempty"`
5+
URL string `json:"url,omitempty"`
6+
Name string `json:"name,omitempty"`
7+
Color string `json:"color,omitempty"`
8+
Description string `json:"description,omitempty"`
9+
Default bool `json:"default,omitempty"`
10+
NodeID string `json:"node_id,omitempty"`
11+
}

go-libs/github/pull_requests.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package github
2+
3+
import "time"
4+
5+
type PullRequestListOptions struct {
6+
// State filters pull requests based on their state. Possible values are:
7+
// open, closed, all. Default is "open".
8+
State string `url:"state,omitempty"`
9+
10+
// Head filters pull requests by head user and branch name in the format of:
11+
// "user:ref-name".
12+
Head string `url:"head,omitempty"`
13+
14+
// Base filters pull requests by base branch name.
15+
Base string `url:"base,omitempty"`
16+
17+
// Sort specifies how to sort pull requests. Possible values are: created,
18+
// updated, popularity, long-running. Default is "created".
19+
Sort string `url:"sort,omitempty"`
20+
21+
// Direction in which to sort pull requests. Possible values are: asc, desc.
22+
// If Sort is "created" or not specified, Default is "desc", otherwise Default
23+
// is "asc"
24+
Direction string `url:"direction,omitempty"`
25+
26+
Page int `url:"page,omitempty"`
27+
PerPage int `url:"per_page,omitempty"`
28+
}
29+
30+
type PullRequestAutoMerge struct {
31+
EnabledBy User `json:"enabled_by,omitempty"`
32+
MergeMethod string `json:"merge_method,omitempty"`
33+
CommitTitle string `json:"commit_title,omitempty"`
34+
CommitMessage string `json:"commit_message,omitempty"`
35+
}
36+
37+
type PullRequestBranch struct {
38+
Label string `json:"label,omitempty"`
39+
Ref string `json:"ref,omitempty"`
40+
SHA string `json:"sha,omitempty"`
41+
Repo Repo `json:"repo,omitempty"`
42+
User User `json:"user,omitempty"`
43+
}
44+
45+
type PullRequest struct {
46+
ID int64 `json:"id,omitempty"`
47+
Number int `json:"number,omitempty"`
48+
State string `json:"state,omitempty"`
49+
Locked bool `json:"locked,omitempty"`
50+
Title string `json:"title,omitempty"`
51+
Body string `json:"body,omitempty"`
52+
CreatedAt time.Time `json:"created_at,omitempty"`
53+
UpdatedAt time.Time `json:"updated_at,omitempty"`
54+
ClosedAt time.Time `json:"closed_at,omitempty"`
55+
MergedAt time.Time `json:"merged_at,omitempty"`
56+
Labels []Label `json:"labels,omitempty"`
57+
User User `json:"user,omitempty"`
58+
Draft bool `json:"draft,omitempty"`
59+
Merged bool `json:"merged,omitempty"`
60+
Mergeable bool `json:"mergeable,omitempty"`
61+
MergeableState string `json:"mergeable_state,omitempty"`
62+
MergedBy User `json:"merged_by,omitempty"`
63+
MergeCommitSHA string `json:"merge_commit_sha,omitempty"`
64+
Rebaseable bool `json:"rebaseable,omitempty"`
65+
Comments int `json:"comments,omitempty"`
66+
Commits int `json:"commits,omitempty"`
67+
Additions int `json:"additions,omitempty"`
68+
Deletions int `json:"deletions,omitempty"`
69+
ChangedFiles int `json:"changed_files,omitempty"`
70+
URL string `json:"url,omitempty"`
71+
HTMLURL string `json:"html_url,omitempty"`
72+
IssueURL string `json:"issue_url,omitempty"`
73+
StatusesURL string `json:"statuses_url,omitempty"`
74+
DiffURL string `json:"diff_url,omitempty"`
75+
PatchURL string `json:"patch_url,omitempty"`
76+
CommitsURL string `json:"commits_url,omitempty"`
77+
CommentsURL string `json:"comments_url,omitempty"`
78+
ReviewCommentsURL string `json:"review_comments_url,omitempty"`
79+
ReviewCommentURL string `json:"review_comment_url,omitempty"`
80+
ReviewComments int `json:"review_comments,omitempty"`
81+
Assignee User `json:"assignee,omitempty"`
82+
Assignees []User `json:"assignees,omitempty"`
83+
MaintainerCanModify bool `json:"maintainer_can_modify,omitempty"`
84+
AuthorAssociation string `json:"author_association,omitempty"`
85+
RequestedReviewers []User `json:"requested_reviewers,omitempty"`
86+
AutoMerge PullRequestAutoMerge `json:"auto_merge,omitempty"`
87+
Head PullRequestBranch `json:"head,omitempty"`
88+
Base PullRequestBranch `json:"base,omitempty"`
89+
}
90+
91+
type PullRequestUpdate struct {
92+
Title string `json:"title,omitempty"`
93+
Body string `json:"body,omitempty"`
94+
State string `json:"state,omitempty"`
95+
Base string `json:"base,omitempty"`
96+
}
97+
98+
type NewPullRequest struct {
99+
Title string `json:"title,omitempty"`
100+
Head string `json:"head,omitempty"`
101+
Base string `json:"base,omitempty"`
102+
Body string `json:"body,omitempty"`
103+
Issue int `json:"issue,omitempty"`
104+
MaintainerCanModify bool `json:"maintainer_can_modify,omitempty"`
105+
Draft bool `json:"draft,omitempty"`
106+
}

0 commit comments

Comments
 (0)