gitpod/components/gitpod-cli/cmd/git-commit-message-helper.go
Filip Troníček c24b8f4aed
Add optional, org-wide Gitpod commit annotation (#20525)
* [supervisor] Add Gitpod commit annotation

* server and API changes

* [dashboard] add org setting for commit annotation

* Fix things

* Fix label for annotation switch

* Revert accidental rename

* minor docs fixes

* Add a feature flag for the setting: `commit_annotation_setting_enabled`

* Register hook in the cloned repo instead of under /etc/

* don't override existing hooks

* `gp git-commit-message-helper` to use `git interpret-trailers`

* Test it!

* 🧹 indeed

* Update timestamp of DB migration
2025-01-24 08:11:14 -05:00

72 lines
1.9 KiB
Go

// Copyright (c) 2025 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
"time"
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var gitCommitMessageHelperOpts struct {
CommitMessageFile string
}
func addGitpodTrailer(commitMsgFile string, hostName string) error {
trailerCmd := exec.Command("git", "interpret-trailers",
"--if-exists", "addIfDifferent",
"--trailer", fmt.Sprintf("Tool: gitpod/%s", hostName),
commitMsgFile)
output, err := trailerCmd.Output()
if err != nil {
return fmt.Errorf("error adding trailer: %w", err)
}
err = os.WriteFile(commitMsgFile, output, 0644)
if err != nil {
return fmt.Errorf("error writing commit message file: %w", err)
}
return nil
}
var gitCommitMessageHelper = &cobra.Command{
Use: "git-commit-message-helper",
Short: "Gitpod's Git commit message helper",
Long: "Automatically adds Tool information to Git commit messages",
Args: cobra.ExactArgs(0),
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()
wsInfo, err := gitpod.GetWSInfo(ctx)
if err != nil {
log.WithError(err).Fatal("error getting workspace info")
return nil // don't block commit
}
if err := addGitpodTrailer(gitCommitMessageHelperOpts.CommitMessageFile, wsInfo.GitpodApi.Host); err != nil {
log.WithError(err).Fatal("failed to add gitpod trailer")
return nil // don't block commit
}
return nil
},
}
func init() {
rootCmd.AddCommand(gitCommitMessageHelper)
gitCommitMessageHelper.Flags().StringVarP(&gitCommitMessageHelperOpts.CommitMessageFile, "file", "f", "", "Path to the commit message file")
_ = gitCommitMessageHelper.MarkFlagRequired("file")
}