-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!:
ref
management and commit trailer handling (#53)
* feat: add `ref` verb supporting reference copying * docs: update README * feat!: refactor commit trailers * enable support for setting multiple arbitrary commit trailers via the `--trailer` flag: `--trailer "Key=Value" --trailer "Key2=Value2"`, or `env GHUP_TRAILER=$(jo Key=Value Key2=Value2)`. * BREAKING CHANGE: the `--trailer.key`, `--trailer.user` and `--trailer.email` flags have been renamed to `--author.trailer`, `--user.name` and `--user.email` respectively. Environment-based overrides still support the old names. * fix: document and extend `info` verb
- Loading branch information
Showing
15 changed files
with
1,334 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/apex/log" | ||
"github.com/google/go-github/v64/github" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/nexthink-oss/ghup/internal/remote" | ||
"github.com/nexthink-oss/ghup/internal/util" | ||
"github.com/nexthink-oss/ghup/pkg/choiceflag" | ||
) | ||
|
||
type sRef struct { | ||
Ref string `yaml:"ref"` | ||
SHA string `yaml:"sha"` | ||
} | ||
|
||
type tRef struct { | ||
Ref string `yaml:"ref"` | ||
Updated bool `yaml:"updated"` | ||
OldSHA string `yaml:"old_sha,omitempty"` | ||
SHA string `yaml:"sha,omitempty"` | ||
Error string `yaml:"error,omitempty"` | ||
} | ||
|
||
type report struct { | ||
Source sRef `yaml:"source"` | ||
Target []tRef `yaml:"target"` | ||
} | ||
|
||
func (r report) String() string { | ||
return util.EncodeYAML(&r) | ||
} | ||
|
||
var updateRefCmd = &cobra.Command{ | ||
Use: "update-ref [flags] -s <source> <target> ...", | ||
Short: "Update target refs to match source", | ||
Args: cobra.MinimumNArgs(1), | ||
PreRunE: validateFlags, | ||
RunE: runUpdateRefCmd, | ||
} | ||
|
||
func init() { | ||
updateRefCmd.Flags().StringP("source", "s", "", "source `ref-or-commit`") | ||
updateRefCmd.MarkFlagRequired("source") | ||
viper.BindPFlag("source", updateRefCmd.Flags().Lookup("source")) | ||
|
||
refTypes := []string{"heads", "tags"} | ||
|
||
defaultSourceType := choiceflag.NewChoiceFlag(refTypes) | ||
_ = defaultSourceType.Set("heads") | ||
updateRefCmd.Flags().VarP(defaultSourceType, "source-type", "S", "unqualified source ref type") | ||
viper.BindPFlag("source-type", updateRefCmd.Flags().Lookup("source-type")) | ||
|
||
defaultTargetType := choiceflag.NewChoiceFlag(refTypes) | ||
_ = defaultTargetType.Set("tags") | ||
updateRefCmd.Flags().VarP(defaultTargetType, "target-type", "T", "unqualified target ref type") | ||
viper.BindPFlag("target-type", updateRefCmd.Flags().Lookup("target-type")) | ||
|
||
updateRefCmd.Flags().SortFlags = false | ||
|
||
rootCmd.AddCommand(updateRefCmd) | ||
} | ||
|
||
func runUpdateRefCmd(cmd *cobra.Command, args []string) (err error) { | ||
ctx := context.Background() | ||
|
||
client, err := remote.NewTokenClient(ctx, viper.GetString("token")) | ||
if err != nil { | ||
return errors.Wrap(err, "NewTokenClient") | ||
} | ||
|
||
sourceRefName := viper.GetString("source") | ||
var sourceObject string | ||
|
||
if util.IsCommitHash(sourceRefName) { | ||
sourceCommit, _, err := client.GetCommitSHA(ctx, owner, repo, sourceRefName) | ||
if err != nil { | ||
return errors.Wrapf(err, "GetCommitSHA(%s, %s, %s)", owner, repo, sourceRefName) | ||
} | ||
sourceObject = *sourceCommit | ||
} else { | ||
sourceRefName, err = util.NormalizeRefName(sourceRefName, viper.GetString("source-type")) | ||
if err != nil { | ||
return errors.Wrapf(err, "NormalizeRefName(%s, %s)", sourceRefName, viper.GetString("source-type")) | ||
} | ||
|
||
log.Infof("resolving source ref: %s", sourceRefName) | ||
sourceRef, _, err := client.V3.Git.GetRef(ctx, owner, repo, sourceRefName) | ||
if err != nil { | ||
return errors.Wrapf(err, "GetSourceRef(%s, %s, %s)", owner, repo, sourceRefName) | ||
} | ||
|
||
sourceObject = sourceRef.Object.GetSHA() | ||
} | ||
|
||
targetRefNames := args | ||
|
||
// ensure all target refs are properly qualified | ||
for i, targetRefName := range targetRefNames { | ||
targetRefName, err = util.NormalizeRefName(targetRefName, viper.GetString("target-type")) | ||
if err != nil { | ||
return errors.Wrapf(err, "NormalizeRefName(%s, %s)", targetRefName, viper.GetString("target-type")) | ||
} | ||
|
||
targetRefNames[i] = targetRefName | ||
} | ||
|
||
report := report{ | ||
Source: sRef{ | ||
Ref: sourceRefName, | ||
SHA: sourceObject, | ||
}, | ||
Target: make([]tRef, 0, len(targetRefNames)), | ||
} | ||
|
||
for _, targetRefName := range targetRefNames { | ||
targetReport := tRef{ | ||
Ref: targetRefName, | ||
} | ||
|
||
targetRef := &github.Reference{ | ||
Ref: &targetRefName, | ||
Object: &github.GitObject{ | ||
SHA: github.String(sourceObject), | ||
}, | ||
} | ||
|
||
oldHash, newHash, err := client.UpdateRefName(ctx, owner, repo, targetRefName, targetRef, viper.GetBool("force")) | ||
if err != nil { | ||
targetReport.Error = errors.Wrapf(err, "UpdateRefName").Error() | ||
report.Target = append(report.Target, targetReport) | ||
continue | ||
} | ||
targetReport.SHA = newHash | ||
if oldHash != newHash { | ||
targetReport.OldSHA = oldHash | ||
targetReport.Updated = true | ||
} | ||
report.Target = append(report.Target, targetReport) | ||
} | ||
|
||
fmt.Print(report) | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.