Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing changelog generation tool #264

Merged
merged 28 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
97a64c0
Introducing changelog
doggydogworld Jul 1, 2024
47c7d92
Moving things around
doggydogworld Jul 1, 2024
7d016de
Fixing typo
doggydogworld Jul 2, 2024
d8046e7
Changing licenses on new files
doggydogworld Jul 2, 2024
2bfafc1
Moving to multi-module
doggydogworld Jul 3, 2024
62d5e67
Moved to github SDK
doggydogworld Jul 5, 2024
eb0adee
Removing some unecessary code
doggydogworld Jul 5, 2024
9f091be
Requiring dir to be passed in to simplify code
doggydogworld Jul 5, 2024
cc60d0d
Initial refactor to go-git lib
doggydogworld Jul 5, 2024
d604039
Removing more git execs for native go git
doggydogworld Jul 5, 2024
de6e47e
Fixing some typos
doggydogworld Jul 5, 2024
6d34444
More refactors for git module
doggydogworld Jul 5, 2024
7ce3d02
Moving another function to git module
doggydogworld Jul 5, 2024
3d7f69d
Fixing typo
doggydogworld Jul 5, 2024
6b00945
Updating an error string
doggydogworld Jul 5, 2024
7ffa993
Restoring bot module deps
doggydogworld Jul 5, 2024
d3a4a52
Moving around a file
doggydogworld Jul 5, 2024
f7c537f
Updating docs for git module
doggydogworld Jul 5, 2024
9acb082
Using v3 API for search
doggydogworld Jul 6, 2024
6e67e79
Cleaning up multi-module and adding links back to ent cl
doggydogworld Jul 9, 2024
d48641f
Using git CLI
doggydogworld Jul 9, 2024
207e590
Even prettier changelog
doggydogworld Jul 9, 2024
a6692fb
Go moduling changelog and libs
doggydogworld Jul 10, 2024
c94b5c0
Using cl for template name
doggydogworld Jul 10, 2024
f4a6950
Fixing changelog mod
doggydogworld Jul 10, 2024
62ba564
Updating govulncheck to use latest go version
doggydogworld Jul 15, 2024
2c75aae
Using more secure version of go-github
doggydogworld Jul 15, 2024
86a40ae
New dependencies
doggydogworld Jul 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go.work*
50 changes: 50 additions & 0 deletions libs/git/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2024 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package git

import (
"bytes"
"os/exec"
"strings"

"github.com/gravitational/trace"
)

// IsAvailable returns status of git
func IsAvailable() error {
_, err := exec.LookPath("git")
return err
}

// RunCmd runs git and returns output (stdout/stderr, depends on the cmd result) and error
func (r *Repo) RunCmd(args ...string) (string, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer

cmd := exec.Command("git", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = r.dir

err := cmd.Run()

if err != nil {
return strings.TrimSpace(stderr.String()), trace.Wrap(err)
}

return strings.TrimSpace(stdout.String()), nil
}
99 changes: 99 additions & 0 deletions libs/git/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2024 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package git

import (
"fmt"

go_git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/gravitational/trace"
)

// Repo provides a collection of functions to query and modify a single git repository.
// Wrapper around the go-git library that also includes methods to execute git commands
// on the system for missing compatability.
type Repo struct {
dir string
Repository *go_git.Repository
}

// NewRepoFromDirectory initializes [Repo] from a directory.
func NewRepoFromDirectory(dir string) (*Repo, error) {
inner, err := go_git.PlainOpen(dir)
if err != nil {
return &Repo{}, trace.Wrap(err)
}

return &Repo{
dir: dir,
Repository: inner,
}, nil
}

// GetCommitForTag attempts to get the resolved commit for a given tag.
func (r *Repo) GetCommitForTag(tag string) (*object.Commit, error) {
t, err := r.Repository.Tag(tag)
if err != nil {
return &object.Commit{}, trace.Wrap(err, fmt.Sprintf("tag %q not found, use a valid git tag", tag))
}
return r.Repository.CommitObject(t.Hash())
}

// GetCommitForHead attempts to get the resolved commit for head.
func (r *Repo) GetCommitForHead() (*object.Commit, error) {
ref, err := r.Repository.Reference(plumbing.HEAD, false)
if err != nil {
return &object.Commit{}, trace.Wrap(err, "can't get latest reference for ent")
}
return r.Repository.CommitObject(ref.Hash())
}

// GetBranchForHead attempts to get the resolved branch of HEAD.
func (r *Repo) GetBranchNameForHead() (string, error) {
// symbolic-ref HEAD
ref, err := r.Repository.Reference(plumbing.HEAD, true)
if err != nil {
return "", trace.Wrap(err, "not on a branch")
}

if !ref.Name().IsBranch() {
return "", trace.BadParameter("not on a branch: %s", ref)
}
return ref.Name().String(), nil
}

// GetParentReleaseBranch will attempt to find a parent branch for HEAD.
// This will also work if HEAD has branched from an earlier commit in a release branch e.g.
//
// o---o---HEAD
// /---o---o---branch/v16
func (r *Repo) GetParentReleaseBranch() (string, error) {
forkPointRef, err := r.RunCmd("merge-base", "--fork-point", "HEAD")
doggydogworld marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", trace.Wrap(err)
}
fbranch, err := r.RunCmd("branch", "--list", "branch/v*", "--contains", forkPointRef, "--format", "%(refname:short)")
if err != nil {
return "", trace.Wrap(err)
}
if fbranch == "" { // stdout is empty indicating the search failed
return "", trace.Errorf("could not find a valid root branch")
}
return fbranch, nil
}
35 changes: 35 additions & 0 deletions libs/git/go.mod
doggydogworld marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module github.com/gravitational/shared-workflows/libs/git

go 1.22.4

require (
github.com/go-git/go-git/v5 v5.12.0
github.com/gravitational/trace v1.3.1
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
github.com/go-git/go-billy/v5 v5.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/tools v0.14.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading
Loading