diff --git a/cmd/bundle.go b/cmd/bundle.go index 442e2c10..e9b54e98 100644 --- a/cmd/bundle.go +++ b/cmd/bundle.go @@ -108,4 +108,16 @@ func init() { bundleCmd.Flags().MarkHidden("deploymentId") bundleCmd.Flags().Bool("ci", false, "Used to track a specific CI job") bundleCmd.Flags().MarkHidden("ci") + bundleCmd.Flags().String("ci-remote-url", "", "Used to set the remote repository URL for your deployment metadata") + bundleCmd.Flags().String("ci-branch", "", "Used to set the branch name for your deployment metadata") + bundleCmd.Flags().String("ci-commit", "", "Used to set the commit hash for your deployment metadata") + bundleCmd.Flags().String("ci-message", "", "Used to set the commit message for your deployment metadata") + bundleCmd.Flags().String("ci-git-provider", "", "Used to set the git provider for your deployment metadata") + + bundleCmd.Flags().MarkHidden("ci-remote-url") + bundleCmd.Flags().MarkHidden("ci-branch") + bundleCmd.Flags().MarkHidden("ci-commit") + bundleCmd.Flags().MarkHidden("ci-messsage") + bundleCmd.Flags().MarkHidden("ci-git-provider") + } diff --git a/cmd/cloud.go b/cmd/cloud.go index c8fb650e..c0df9e46 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -146,6 +146,11 @@ Examples: transportUrl := context.TransportURL token := context.Token ci, _ := cmd.Flags().GetBool("ci") + ciRemoteUrl, _ := cmd.Flags().GetString("ci-remote-url") + ciBranch, _ := cmd.Flags().GetString("ci-branch") + ciCommit, _ := cmd.Flags().GetString("ci-commit") + ciMessage, _ := cmd.Flags().GetString("ci-message") + ciGitProvider, _ := cmd.Flags().GetString("ci-git-provider") deploymentConfig := project.NewDeploymentConfig() client := util.NewAPIClient(ctx, logger, apiUrl, token) @@ -353,14 +358,25 @@ Examples: deploymentId = "/" + deploymentId } - gitInfo, err := deployer.GetGitInfo(dir) - if err != nil { - logger.Error("error getting git info: %s", err) - } + var gitInfo deployer.GitInfo var originType string - if ci { + isOverwritingGitInfo := ciRemoteUrl != "" || ciBranch != "" || ciCommit != "" || ciMessage != "" || ciGitProvider != "" + if ci && isOverwritingGitInfo { originType = "ci" + gitInfo = deployer.GitInfo{ + RemoteURL: &ciRemoteUrl, + Branch: &ciBranch, + Commit: &ciCommit, + CommitMessage: &ciMessage, + GitProvider: &ciGitProvider, + IsRepo: true, + } } else { + info, err := deployer.GetGitInfo(logger, dir) + if err != nil { + logger.Debug("Failed to get git info: %v", err) + } + gitInfo = *info originType = "cli" } @@ -632,8 +648,20 @@ func init() { cloudDeployCmd.Flags().StringP("dir", "d", ".", "The directory to the project to deploy") cloudDeployCmd.Flags().String("deploymentId", "", "Used to track a specific deployment") cloudDeployCmd.Flags().Bool("ci", false, "Used to track a specific CI job") + cloudDeployCmd.Flags().String("ci-remote-url", "", "Used to set the remote repository URL for your deployment metadata") + cloudDeployCmd.Flags().String("ci-branch", "", "Used to set the branch name for your deployment metadata") + cloudDeployCmd.Flags().String("ci-commit", "", "Used to set the commit hash for your deployment metadata") + cloudDeployCmd.Flags().String("ci-message", "", "Used to set the commit message for your deployment metadata") + cloudDeployCmd.Flags().String("ci-git-provider", "", "Used to set the git provider for your deployment metadata") + cloudDeployCmd.Flags().MarkHidden("deploymentId") cloudDeployCmd.Flags().MarkHidden("ci") + cloudDeployCmd.Flags().MarkHidden("ci-remote-url") + cloudDeployCmd.Flags().MarkHidden("ci-branch") + cloudDeployCmd.Flags().MarkHidden("ci-commit") + cloudDeployCmd.Flags().MarkHidden("ci-messsage") + cloudDeployCmd.Flags().MarkHidden("ci-git-provider") + cloudDeployCmd.Flags().String("format", "text", "The output format to use for results which can be either 'text' or 'json'") cloudDeployCmd.Flags().String("org-id", "", "The organization to create the project in") cloudDeployCmd.Flags().String("templates-dir", "", "The directory to load the templates. Defaults to loading them from the github.com/agentuity/templates repository") diff --git a/internal/deployer/metadata.go b/internal/deployer/metadata.go index 55ea6d09..3bb18127 100644 --- a/internal/deployer/metadata.go +++ b/internal/deployer/metadata.go @@ -5,15 +5,18 @@ import ( "runtime" "strings" + "github.com/agentuity/go-common/logger" "github.com/go-git/go-git/v5" ) // GitInfo contains basic git repository information type GitInfo struct { - RemoteURL string `json:"remoteUrl"` - Branch string `json:"branch"` - Commit string `json:"commit"` - IsRepo bool `json:"isRepo"` + RemoteURL *string `json:"remoteUrl"` + Branch *string `json:"branch"` + Commit *string `json:"commit"` + CommitMessage *string `json:"commitMessage"` + IsRepo bool `json:"isRepo"` + GitProvider *string } type MetadataOrigin struct { @@ -54,7 +57,7 @@ func GetMachineInfo() *MachineInfo { } // GetGitInfo extracts git information from a directory -func GetGitInfo(dir string) (*GitInfo, error) { +func GetGitInfo(logger logger.Logger, dir string) (*GitInfo, error) { info := &GitInfo{} repo, err := git.PlainOpen(dir) @@ -64,24 +67,34 @@ func GetGitInfo(dir string) (*GitInfo, error) { // Get remote URL remote, err := repo.Remote("origin") - if err == nil { - info.RemoteURL = remote.Config().URLs[0] + if err == nil && len(remote.Config().URLs) > 0 { + remoteURL := remote.Config().URLs[0] // re-write the github url to be https so they display correctly in the UI - // git@github.com:agentuity/agent-changelog.git - if strings.HasPrefix(info.RemoteURL, "git@github.com:") { - info.RemoteURL = strings.Replace(info.RemoteURL, "git@github.com:", "https://github.com/", 1) - if strings.HasSuffix(info.RemoteURL, ".git") { - info.RemoteURL = strings.TrimSuffix(info.RemoteURL, ".git") + if strings.HasPrefix(remoteURL, "git@github.com:") { + remoteURL = strings.Replace(remoteURL, "git@github.com:", "https://github.com/", 1) + if strings.HasSuffix(remoteURL, ".git") { + remoteURL = strings.TrimSuffix(remoteURL, ".git") } } + info.RemoteURL = &remoteURL } // Get current branch and commit head, err := repo.Head() if err == nil { - info.Branch = head.Name().Short() - info.Commit = head.Hash().String() + branch := head.Name().Short() + commitHash := head.Hash().String() + info.Branch = &branch + info.Commit = &commitHash info.IsRepo = true + commit, err := repo.CommitObject(head.Hash()) + if err == nil { + msg := strings.TrimSpace(commit.Message) + info.CommitMessage = &msg + } + } + if err != nil { + logger.Trace(err.Error()) } return info, nil