Skip to content

Commit

Permalink
(snippet) create on forkedFromRemote, then try forkedRemote, then try…
Browse files Browse the repository at this point in the history
… personal snippet
  • Loading branch information
zaquestion committed Dec 4, 2017
1 parent 58a05fa commit 160fdac
Show file tree
Hide file tree
Showing 32 changed files with 1,585 additions and 367 deletions.
11 changes: 8 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@

[[constraint]]
name = "github.com/xanzy/go-gitlab"
version = "0.6.0"
revision = "5759674b9a31780c812771c21f3b08c0b3978ef7"
4 changes: 2 additions & 2 deletions cmd/mrCheckout.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var checkoutCmd = &cobra.Command{
Long: ``,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(targetRemote)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand All @@ -40,7 +40,7 @@ var checkoutCmd = &cobra.Command{
// https://docs.gitlab.com/ee/user/project/merge_requests/#checkout-merge-requests-locally
branch := mrs[0].SourceBranch
mr := fmt.Sprintf("refs/merge-requests/%s/head", mrIDStr)
gitf := git.New("fetch", targetRemote, fmt.Sprintf("%s:%s", mr, branch))
gitf := git.New("fetch", forkedFromRemote, fmt.Sprintf("%s:%s", mr, branch))
err = gitf.Run()
if err != nil {
log.Fatal(err)
Expand Down
9 changes: 0 additions & 9 deletions cmd/mrCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ const (
targetBranch = "master"
)

var (
// Will be updated to upstream in init() if upstream remote exists
forkedFromRemote = "origin"
)

// mrCmd represents the mr command
var mrCreateCmd = &cobra.Command{
Use: "create",
Expand All @@ -39,10 +34,6 @@ var mrCreateCmd = &cobra.Command{

func init() {
mrCmd.AddCommand(mrCreateCmd)
_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
forkedFromRemote = "upstream"
}
}

func runMRCreate(cmd *cobra.Command, args []string) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/mrList.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var listCmd = &cobra.Command{
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(forkFromOrigin)
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"unicode"

"github.com/spf13/cobra"
gitconfig "github.com/tcnksm/go-gitconfig"
"github.com/zaquestion/lab/internal/git"
lab "github.com/zaquestion/lab/internal/gitlab"
)

// RootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -70,9 +72,24 @@ func labUsage(c *cobra.Command) string {
return buf.String()
}

var (
// Will be updated to upstream in init() if "upstream" remote exists
forkedFromRemote = "origin"
// Will be updated to lab.User() in init() if forkedFrom is "origin"
forkRemote = "origin"
)

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
_, err := gitconfig.Local("remote.upstream.url")
if err == nil {
forkedFromRemote = "upstream"
}

if forkedFromRemote == "origin" {
forkRemote = lab.User()
}
if cmd, _, err := RootCmd.Find(os.Args[1:]); err != nil || cmd.Use == "clone" {
// Determine if any undefined flags were passed to "clone
if cmd.Use == "clone" && len(os.Args) > 2 {
Expand Down
68 changes: 46 additions & 22 deletions cmd/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (

var (
msgs []string
path string
name string
file string
private bool
public bool
)
Expand All @@ -33,20 +33,20 @@ var snippetCmd = &cobra.Command{
Short: "Create a snippet on GitLab or in a project",
Long: `
Source snippets from stdin, file, or in editor from scratch
Write title&description in editor, or -m`,
Write title & description in editor, or -m`,
Run: func(cmd *cobra.Command, args []string) {
rn, err := git.PathWithNameSpace(forkedFromRemote)
if err != nil {
log.Fatal(err)
if len(args) > 0 {
file = args[0]
}
code, err := determineCode(path)
rn, _ := git.PathWithNameSpace(forkedFromRemote)
code, err := determineCode(file)
if err != nil {
log.Fatal(err)
}
if code == "" {
log.Fatal("aborting snippet due to empty contents")
}
title, _, err := determineMsg(msgs, code)
title, body, err := determineMsg(msgs, code)
if title == "" {
log.Fatal("aborting snippet due to empty msg")
}
Expand All @@ -58,26 +58,50 @@ Write title&description in editor, or -m`,
case public:
visibility = gitlab.PublicVisibility
}
// TODO: expand gitlab api to support creating snippets with descriptions
snip, err := lab.CreateSnippet(rn, &gitlab.CreateSnippetOptions{
Title: gitlab.String(title),
Code: gitlab.String(code),
FileName: gitlab.String(name),
Visibility: &visibility,
})
if rn != "" {
psOpts := gitlab.CreateProjectSnippetOptions{
Title: gitlab.String(title),
Description: gitlab.String(body),
Code: gitlab.String(code),
FileName: gitlab.String(name),
Visibility: &visibility,
}
// Assuming that if you have permissions to create
// snippets on forkedFromRepo thats what you want
snip, err := lab.CreateProjectSnippet(rn, &psOpts)
if err == nil && snip != nil {
fmt.Println(snip.WebURL)
return
}

// Try creating on user fork if failed to create on
// forkedFromRepo. Seemingly the next best bet
rn, err = git.PathWithNameSpace(forkRemote)
if err == nil {
snip, err = lab.CreateProjectSnippet(rn, &psOpts)
if err == nil && snip != nil {
fmt.Println(snip.WebURL)
return
}
}
}

sOpts := gitlab.CreateSnippetOptions{
Title: gitlab.String(title),
Description: gitlab.String(body),
Content: gitlab.String(code),
FileName: gitlab.String(name),
Visibility: &visibility,
}
snip, err := lab.CreateSnippet(&sOpts)
if err != nil {
log.Fatal(err)

}
if snip == nil {
log.Fatal("Fatal: snippet failed to be created")
}
// TODO: expand gitlab api to expose web_url field
// https://github.com/xanzy/go-gitlab/pull/247
project := lab.User() + "/" + rn
if strings.Contains(rn, "/") {
project = rn
log.Fatal("failed to create snippet")
}
fmt.Printf("%s/%s/%d", lab.Host(), project, snip.ID)
fmt.Println(snip.WebURL)
},
}

Expand Down
23 changes: 18 additions & 5 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Host() string {

// User exposes the configured GitLab user
func User() string {
return host
return user
}

func Init() {
Expand Down Expand Up @@ -73,8 +73,8 @@ func Init() {
token, errt = gitconfig.Entire("gitlab.token")
if err != nil {
fmt.Print("Enter default GitLab user: ")
User, err = reader.ReadString('\n')
User = strings.TrimSpace(User)
user, err = reader.ReadString('\n')
user = strings.TrimSpace(user)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -325,8 +325,8 @@ func BranchPushed(project, branch string) bool {
return b != nil
}

// CreateSnippet creates a snippet in a project
func CreateSnippet(project string, opts *gitlab.CreateSnippetOptions) (*gitlab.Snippet, error) {
// CreateProjectSnippet creates a snippet in a project
func CreateProjectSnippet(project string, opts *gitlab.CreateProjectSnippetOptions) (*gitlab.Snippet, error) {
p, err := FindProject(project)
if err != nil {
return nil, err
Expand All @@ -342,3 +342,16 @@ func CreateSnippet(project string, opts *gitlab.CreateSnippetOptions) (*gitlab.S

return snip, nil
}

// CreateSnippet creates a personal snippet
func CreateSnippet(opts *gitlab.CreateSnippetOptions) (*gitlab.Snippet, error) {
snip, resp, err := lab.Snippets.CreateSnippet(opts)
if os.Getenv("DEBUG") != "" {
fmt.Println(resp.Response.Status)
}
if err != nil {
return nil, err
}

return snip, nil
}
30 changes: 16 additions & 14 deletions vendor/github.com/xanzy/go-gitlab/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 160fdac

Please sign in to comment.