-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add git integration with first command - start work on work package - create branch and switch to it - convert type and subject to lower case - replace whitespace with - - drop every other character not being alphanumeric
- Loading branch information
Showing
6 changed files
with
309 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package git | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/opf/openproject-cli/cmd/git/start" | ||
) | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "git [command]", | ||
Short: "Executes a git related command.", | ||
Long: `This is a command prefix that enables a nested | ||
set of executable commands, related to a git context.`, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(start.RootCmd) | ||
} |
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,13 @@ | ||
package start | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
var RootCmd = &cobra.Command{ | ||
Use: "start [resource]", | ||
Short: "Starts the work on a resource", | ||
Long: "Creates a branch based on the given resource and switches to it.", | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(startWorkPackageCmd) | ||
} |
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,104 @@ | ||
package start | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/opf/openproject-cli/components/printer" | ||
"github.com/opf/openproject-cli/components/resources/work_packages" | ||
) | ||
|
||
var startWorkPackageCmd = &cobra.Command{ | ||
Use: "workpackage [id]", | ||
Short: "Starts the work on a work package", | ||
Long: "Creates a branch based on the workpackage id, subject and type. Switches to the branch after creation.", | ||
Run: startWorkPackage, | ||
} | ||
|
||
func startWorkPackage(_ *cobra.Command, args []string) { | ||
id := checkArgumentsForId(args) | ||
|
||
repo, err := git.PlainOpen(".") | ||
if err != nil { | ||
printer.ErrorText("No git repository found in the current location.") | ||
return | ||
} | ||
|
||
head, err := repo.Head() | ||
handleGitError(err) | ||
|
||
worktree, err := repo.Worktree() | ||
handleGitError(err) | ||
|
||
branchName, err := deriveBranchName(id) | ||
if err != nil { | ||
printer.Error(err) | ||
return | ||
} | ||
|
||
err = worktree.Checkout(&git.CheckoutOptions{ | ||
Hash: head.Hash(), | ||
Branch: plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", branchName)), | ||
Create: true, | ||
Keep: true, | ||
}) | ||
handleGitError(err) | ||
|
||
printer.Info(fmt.Sprintf("Switched to a new branch: %s", branchName)) | ||
} | ||
|
||
func checkArgumentsForId(args []string) uint64 { | ||
if len(args) != 1 { | ||
printer.ErrorText(fmt.Sprintf("Expected 1 argument [id], but got %d", len(args))) | ||
os.Exit(1) | ||
} | ||
|
||
id, err := strconv.ParseUint(args[0], 10, 64) | ||
if err != nil { | ||
printer.ErrorText(fmt.Sprintf("'%s' is an invalid work package id. Must be a number.", args[0])) | ||
os.Exit(1) | ||
} | ||
|
||
return id | ||
} | ||
|
||
func handleGitError(err error) { | ||
if err != nil { | ||
printer.ErrorText(fmt.Sprintf("Fatal error on executing git commands: %s", err.Error())) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func deriveBranchName(id uint64) (string, error) { | ||
workPackage, err := work_packages.Lookup(id) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
name := fmt.Sprintf( | ||
"%s/%d-%s", | ||
sanitizeString(workPackage.Type), | ||
workPackage.Id, | ||
sanitizeString(workPackage.Subject), | ||
) | ||
|
||
return name, nil | ||
} | ||
|
||
func sanitizeString(str string) string { | ||
lower := strings.ToLower(str) | ||
return regexp.MustCompile("[^a-zA-Z0-9-/_.]").ReplaceAllStringFunc(lower, func(s string) string { | ||
if s == " " { | ||
return "-" | ||
} | ||
|
||
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
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.