Skip to content

Commit

Permalink
More git-related libraries (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfx authored Dec 21, 2023
1 parent 989c334 commit f0e62a1
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Version changelog

## 0.0.1

Initial version with some tooling.
6 changes: 6 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ This Software contains code from the following projects, licensed under the Data
Databricks CLI
Copyright (2022) Databricks, Inc.
License - https://github.com/databricks/cli/blob/main/LICENSE

This Software contains code from the following projects, licensed under the BSD-style license:

go-github
Copyright 2013 The go-github AUTHORS. All rights reserved.
License - https://github.com/google/go-github/blob/master/LICENSE
27 changes: 25 additions & 2 deletions go-libs/git/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (l *Checkout) cmd(ctx context.Context, args ...string) (string, error) {
return strings.TrimSpace(out), nil
}

func (l *Checkout) Dir() string {
return l.dir
}

func (l *Checkout) OrgAndRepo() (string, string, bool) {
tmp := strings.TrimSuffix(l.fetchRemote, ".git")
tmp = strings.TrimPrefix(tmp, "https://github.com/")
Expand Down Expand Up @@ -72,8 +76,27 @@ func (l *Checkout) CurrentBranch(ctx context.Context) (string, error) {
return l.cmd(ctx, "branch", "--show-current")
}

func (l *Checkout) DefaultBranch(ctx context.Context) (string, error) {
return l.cmd(ctx, "config", "--get", "init.defaultBranch")
}

func (l *Checkout) CheckoutMain(ctx context.Context) (string, error) {
return l.cmd(ctx, "checkout", "main")
defaultBranch, err := l.DefaultBranch(ctx)
if err != nil {
return "", fmt.Errorf("default branch: %w", err)
}
return l.cmd(ctx, "checkout", defaultBranch)
}

// If prepare/X.X.X does not name an existing branch, this creates the new branch,
// pointing to the current commit, as if by regular git checkout -b. If prepare/X.X.X does name an
// existing branch, what happens instead is that Git forcibly re-points the branch name to the current commit.
//
// This is a lot like a git reset --soft. A branch name is really just a human-readable name for some Git hash ID,
// and a soft reset changes the hash ID attached to the branch name, without touching the index or work-tree. In
// the same way, git checkout -B will change the ID attached to this name, without touching the index or work-tree.
func (l *Checkout) ForceCheckout(ctx context.Context, branch string) (string, error) {
return l.cmd(ctx, "checkout", "-B", branch)
}

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

func (l *Checkout) PushTag(ctx context.Context, v string) (string, error) {
func (l *Checkout) ForcePush(ctx context.Context, v string) (string, error) {
return l.cmd(ctx, "push", l.pushRemote, v, "-f")
}
53 changes: 53 additions & 0 deletions go-libs/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,25 @@ func (c *GitHubClient) Versions(ctx context.Context, org, repo string) (Versions
return releases, err
}

type CreateReleaseRequest struct {
TagName string `json:"tag_name,omitempty"`
Name string `json:"name,omitempty"`
Body string `json:"body,omitempty"`
Draft bool `json:"draft,omitempty"`
Prerelease bool `json:"prerelease,omitempty"`
GenerateReleaseNotes bool `json:"generate_release_notes,omitempty"`
DiscussionCategoryName string `json:"discussion_category_name,omitempty"`
}

func (c *GitHubClient) CreateRelease(ctx context.Context, org, repo string, req CreateReleaseRequest) (*Release, error) {
var res Release
url := fmt.Sprintf("%s/repos/%s/%s/releases", gitHubAPI, org, repo)
err := c.api.Do(ctx, "POST", url,
httpclient.WithRequestData(req),
httpclient.WithResponseUnmarshal(&res))
return &res, err
}

func (c *GitHubClient) GetRepo(ctx context.Context, org, name string) (repo Repo, err error) {
url := fmt.Sprintf("%s/repos/%s/%s", gitHubAPI, org, name)
err = c.api.Do(ctx, "GET", url, httpclient.WithResponseUnmarshal(&repo))
Expand Down Expand Up @@ -91,3 +110,37 @@ func (c *GitHubClient) CompareCommits(ctx context.Context, org, repo, base, head
err := c.api.Do(ctx, "GET", path, httpclient.WithResponseUnmarshal(&response))
return response.Commits, err
}

func (c *GitHubClient) ListPullRequests(ctx context.Context, org, repo string, opts PullRequestListOptions) ([]PullRequest, error) {
path := fmt.Sprintf("%s/repos/%s/%s/pulls", gitHubAPI, org, repo)
var prs []PullRequest
err := c.api.Do(ctx, "GET", path,
httpclient.WithRequestData(opts),
httpclient.WithResponseUnmarshal(&prs))
return prs, err
}

func (c *GitHubClient) EditPullRequest(ctx context.Context, org, repo string, number int, body PullRequestUpdate) error {
path := fmt.Sprintf("%s/repos/%s/%s/pulls/%d", gitHubAPI, org, repo, number)
return c.api.Do(ctx, "PATCH", path, httpclient.WithRequestData(body))
}

func (c *GitHubClient) CreatePullRequest(ctx context.Context, org, repo string, body NewPullRequest) (*PullRequest, error) {
path := fmt.Sprintf("%s/repos/%s/%s/pulls", gitHubAPI, org, repo)
var res PullRequest
err := c.api.Do(ctx, "POST", path,
httpclient.WithRequestData(body),
httpclient.WithResponseUnmarshal(&res))
if err != nil {
return nil, err
}
return &res, nil
}

func (c *GitHubClient) GetPullRequest(ctx context.Context, org, repo string, number int) (*PullRequest, error) {
path := fmt.Sprintf("%s/repos/%s/%s/pulls/%d", gitHubAPI, org, repo, number)
var res PullRequest
err := c.api.Do(ctx, "GET", path,
httpclient.WithResponseUnmarshal(&res))
return &res, err
}
11 changes: 11 additions & 0 deletions go-libs/github/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package github

type Label struct {
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
Color string `json:"color,omitempty"`
Description string `json:"description,omitempty"`
Default bool `json:"default,omitempty"`
NodeID string `json:"node_id,omitempty"`
}
106 changes: 106 additions & 0 deletions go-libs/github/pull_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package github

import "time"

type PullRequestListOptions struct {
// State filters pull requests based on their state. Possible values are:
// open, closed, all. Default is "open".
State string `url:"state,omitempty"`

// Head filters pull requests by head user and branch name in the format of:
// "user:ref-name".
Head string `url:"head,omitempty"`

// Base filters pull requests by base branch name.
Base string `url:"base,omitempty"`

// Sort specifies how to sort pull requests. Possible values are: created,
// updated, popularity, long-running. Default is "created".
Sort string `url:"sort,omitempty"`

// Direction in which to sort pull requests. Possible values are: asc, desc.
// If Sort is "created" or not specified, Default is "desc", otherwise Default
// is "asc"
Direction string `url:"direction,omitempty"`

Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
}

type PullRequestAutoMerge struct {
EnabledBy User `json:"enabled_by,omitempty"`
MergeMethod string `json:"merge_method,omitempty"`
CommitTitle string `json:"commit_title,omitempty"`
CommitMessage string `json:"commit_message,omitempty"`
}

type PullRequestBranch struct {
Label string `json:"label,omitempty"`
Ref string `json:"ref,omitempty"`
SHA string `json:"sha,omitempty"`
Repo Repo `json:"repo,omitempty"`
User User `json:"user,omitempty"`
}

type PullRequest struct {
ID int64 `json:"id,omitempty"`
Number int `json:"number,omitempty"`
State string `json:"state,omitempty"`
Locked bool `json:"locked,omitempty"`
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
ClosedAt time.Time `json:"closed_at,omitempty"`
MergedAt time.Time `json:"merged_at,omitempty"`
Labels []Label `json:"labels,omitempty"`
User User `json:"user,omitempty"`
Draft bool `json:"draft,omitempty"`
Merged bool `json:"merged,omitempty"`
Mergeable bool `json:"mergeable,omitempty"`
MergeableState string `json:"mergeable_state,omitempty"`
MergedBy User `json:"merged_by,omitempty"`
MergeCommitSHA string `json:"merge_commit_sha,omitempty"`
Rebaseable bool `json:"rebaseable,omitempty"`
Comments int `json:"comments,omitempty"`
Commits int `json:"commits,omitempty"`
Additions int `json:"additions,omitempty"`
Deletions int `json:"deletions,omitempty"`
ChangedFiles int `json:"changed_files,omitempty"`
URL string `json:"url,omitempty"`
HTMLURL string `json:"html_url,omitempty"`
IssueURL string `json:"issue_url,omitempty"`
StatusesURL string `json:"statuses_url,omitempty"`
DiffURL string `json:"diff_url,omitempty"`
PatchURL string `json:"patch_url,omitempty"`
CommitsURL string `json:"commits_url,omitempty"`
CommentsURL string `json:"comments_url,omitempty"`
ReviewCommentsURL string `json:"review_comments_url,omitempty"`
ReviewCommentURL string `json:"review_comment_url,omitempty"`
ReviewComments int `json:"review_comments,omitempty"`
Assignee User `json:"assignee,omitempty"`
Assignees []User `json:"assignees,omitempty"`
MaintainerCanModify bool `json:"maintainer_can_modify,omitempty"`
AuthorAssociation string `json:"author_association,omitempty"`
RequestedReviewers []User `json:"requested_reviewers,omitempty"`
AutoMerge PullRequestAutoMerge `json:"auto_merge,omitempty"`
Head PullRequestBranch `json:"head,omitempty"`
Base PullRequestBranch `json:"base,omitempty"`
}

type PullRequestUpdate struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
State string `json:"state,omitempty"`
Base string `json:"base,omitempty"`
}

type NewPullRequest struct {
Title string `json:"title,omitempty"`
Head string `json:"head,omitempty"`
Base string `json:"base,omitempty"`
Body string `json:"body,omitempty"`
Issue int `json:"issue,omitempty"`
MaintainerCanModify bool `json:"maintainer_can_modify,omitempty"`
Draft bool `json:"draft,omitempty"`
}

0 comments on commit f0e62a1

Please sign in to comment.