From 5222f60520140875506d8663551c72f02afe4068 Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:17:27 +0000 Subject: [PATCH 1/6] feat(devcontainer, ci): update devcontainer configuration and CI script - Replace azure-cli-persistence feature with shell-history in devcontainer setup. - Add curl command for syft installation in CI jobs. - Adjust GoReleaser to support building for Windows. feat(install): add installation script for GIt-Commit tool - Introduce a script to automate the installation of the gic tool, fetching the latest version from GitHub. refactor(logger): improve logging during commit message generation - Enhance logging to provide better insights into the Azure AD token generation process. --- .devcontainer/devcontainer.json | 4 +-- .github/workflows/ci.yaml | 7 ++-- .goreleaser.yaml | 9 +++-- README.md | 2 +- cmd/root.go | 22 +++++------- internal/llm/llm.go | 7 ++-- script/install-gic.sh | 63 +++++++++++++++++++++++++++++++++ 7 files changed, 88 insertions(+), 26 deletions(-) create mode 100644 script/install-gic.sh diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index dd747d0..fa23407 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -14,14 +14,14 @@ "installOhMyZsh": true, "installOhMyZshConfig": true }, - "ghcr.io/stuartleeks/dev-container-features/shell-history:0": {}, "ghcr.io/devcontainers/features/azure-cli:1": { "installBicep": true }, // "ghcr.io/prulloac/devcontainer-features/ollama:1": { // "pull": "phi3.5" // } - "ghcr.io/stuartleeks/dev-container-features/azure-cli-persistence:0": {}, + "ghcr.io/stuartleeks/dev-container-features/shell-history:0": {} + }, "waitFor": "onCreateCommand", // "updateContentCommand": "python3 -m pip install -r requirements.txt", diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 480d754..3cf6212 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -41,8 +41,10 @@ jobs: with: go-version: 1.23 - # - name: Run Tests - # run: | + - name: Run Tests + run: | + curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin + # go test -v ./... release: @@ -70,6 +72,7 @@ jobs: echo 'deb [trusted=yes] https://repo.goreleaser.com/apt/ /' | sudo tee /etc/apt/sources.list.d/goreleaser.list sudo apt update sudo apt install goreleaser + curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin - name: Semantic Release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 444cf24..0c8aa73 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -21,6 +21,9 @@ builds: goos: - linux - darwin + - windows + ldflags: + - -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} sboms: - artifacts: archive @@ -41,8 +44,4 @@ archives: format: zip changelog: - sort: asc - filters: - exclude: - - "^docs:" - - "^test:" + use: github-native diff --git a/README.md b/README.md index 7db0ba5..6a31317 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # GIt-Commit (gic) -A tool that helps developers generate git commit messages based on the `git diff` of staged files, following specified instructions. It's ideal for use alongside [Semantic Release](https://github.com/semantic-release/semantic-release). +Reducing cognitive load by automating commit message generation, allowing developers to focus on coding instead of crafting messages. A tool that helps developers generate git commit messages based on the `git diff` of staged files, following instructions. It's ideal for use alongside [Semantic Release](https://github.com/semantic-release/semantic-release). ## AzureAD diff --git a/cmd/root.go b/cmd/root.go index 24d3d89..cdc87ad 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,4 +1,3 @@ -// Package cmd provides the command-line interface for the gic application. package cmd import ( @@ -12,23 +11,27 @@ import ( "github.com/spf13/cobra" ) -// const exitCodeFailure = 1 - var ( hash string verbose bool - rootCmd = &cobra.Command{ Use: "gic", Short: "gic", Long: "gic generates git commit messages based on staged changes.", - PersistentPreRun: func(_ *cobra.Command, _ []string) { + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { // Set logger level based on the verbose flag if verbose { logger.SetLogLevel("debug") } else { logger.SetLogLevel("info") } + + // Check for non-flag arguments + if len(args) > 0 { + return fmt.Errorf("unexpected arguments: %v", args) + } + + return nil }, } ) @@ -37,17 +40,12 @@ var ( func Execute(version, commit string) error { rootCmd.Version = version hash = commit - setVersion() - rootCmd.RunE = executeCmd - return rootCmd.Execute() } func executeCmd(_ *cobra.Command, _ []string) error { - // _ = cmd - // _ = args l := logger.GetLogger() l.Debug("Started executing command") l.Debug("Start loading configuration") @@ -56,27 +54,23 @@ func executeCmd(_ *cobra.Command, _ []string) error { return err } l.Debug("Finish loading configuration") - l.Debug("Start getting staged changes") gitDiff, err := git.GetStagedChanges() if err != nil { return err } l.Debug("Finish getting staged changes") - l.Debug("Start generating commit message") commitMessage, err := llm.GenerateCommitMessage(cfg, gitDiff) if err != nil { return err } l.Debug("Finish generating commit message") - l.Debug("Start validating commit message includes changes") if commitMessage == "### NO STAGED CHAGES ###" { return nil } l.Debug("Finish validating commit message includes changes") - return git.Commit(commitMessage, cfg) } diff --git a/internal/llm/llm.go b/internal/llm/llm.go index 7f3c728..c6a73c4 100644 --- a/internal/llm/llm.go +++ b/internal/llm/llm.go @@ -44,7 +44,7 @@ func GenerateCommitMessage(cfg config.Config, diff string) (string, error) { case "azure": return GenerateCommitMessageAzure(apikey, cfg, diff) case "azure_ad": - return GenerateCommitMessageAzureAD(cfg, diff) + return GenerateCommitMessageAzureAD(cfg, diff, l) case "openai": return GenerateCommitMessageOpenAI(apikey, cfg, diff) case "ollama": @@ -109,9 +109,12 @@ func GenerateCommitMessageAzure(apikey string, cfg config.Config, diff string) ( // authentication. // It takes a config.Config object and a string representing // the diff as input. -func GenerateCommitMessageAzureAD(cfg config.Config, diff string) (string, error) { +func GenerateCommitMessageAzureAD(cfg config.Config, diff string, l *logger.Logger) (string, error) { + l.Debug("GenerateCommitMessageAzureAD") + l.Debug("obtaining token credential") tokenCredential, err := azidentity.NewDefaultAzureCredential(nil) if err != nil { + l.Error("failed getting token", "error", err) return emptyString, err } client, err := azopenai.NewClient(cfg.AzureEndpoint, tokenCredential, nil) diff --git a/script/install-gic.sh b/script/install-gic.sh new file mode 100644 index 0000000..2a9a24d --- /dev/null +++ b/script/install-gic.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Variables +REPO_OWNER="jsburckhardt" +REPO_NAME="gic" +BINARY_NAME="gic" + +# Function to get the latest version from GitHub API +get_latest_version() { + LATEST_URL="https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/releases/latest" + curl -s "$LATEST_URL" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' +} + +# Check if a version is passed as an argument +if [ -z "$1" ]; then + # No version provided, get the latest version + VERSION=$(get_latest_version) + echo "No version provided, installing the latest version: $VERSION" +else + VERSION=$1 + echo "Installing specified version: $VERSION" +fi + +# Determine the OS and architecture following the naming template +OS=$(uname | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m) + +if [ "$ARCH" == "x86_64" ]; then + ARCH="x86_64" +elif [ "$ARCH" == "i686" ]; then + ARCH="i386" +elif [ "$ARCH" == "armv7l" ]; then + ARCH="armv7" +elif [ "$ARCH" == "aarch64" ]; then + ARCH="arm64" +fi + +# Construct the download URL to match the naming template +DOWNLOAD_URL="https://github.com/$REPO_OWNER/$REPO_NAME/releases/download/$VERSION/${REPO_NAME}_$(echo "$OS" | sed 's/.*/\u&/')_$ARCH.tar.gz" + +# Create a temporary directory for the download +TMP_DIR=$(mktemp -d) +cd "$TMP_DIR" || exit + +# Download the binary tarball +echo "Downloading $BINARY_NAME from $DOWNLOAD_URL" +curl -LO "$DOWNLOAD_URL" + +# Extract the tarball +echo "Extracting the tarball" +tar -xzf "${REPO_NAME}_$(echo "$OS" | sed 's/.*/\u&/')_$ARCH.tar.gz" + +# Move the binary to /usr/local/bin +echo "Installing $BINARY_NAME" +sudo mv $BINARY_NAME /usr/local/bin/ + +# Cleanup +cd - || exit +rm -rf "$TMP_DIR" + +# Verify installation +echo "Verifying installation" +$BINARY_NAME --version From 8d60305bc909ec5859feec07d3b18f70ca02dc3d Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:19:14 +0000 Subject: [PATCH 2/6] docs(cmd): add package comment for command-line interface refactor(cmd): simplify PersistentPreRunE function signature to ignore command variable --- cmd/root.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index cdc87ad..36376a3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,3 +1,4 @@ +// Package cmd provides the command-line interface for the gic application. package cmd import ( @@ -18,7 +19,7 @@ var ( Use: "gic", Short: "gic", Long: "gic generates git commit messages based on staged changes.", - PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + PersistentPreRunE: func(_ *cobra.Command, args []string) error { // Set logger level based on the verbose flag if verbose { logger.SetLogLevel("debug") From 5ce4d61b11eaf3c3154edd8f1180e557a6425531 Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:20:56 +0000 Subject: [PATCH 3/6] feat(logging): add info log for commit messages before execution This change introduces an informational log statement that logs the commit message immediately before the commit action is executed, aiding in debugging and traceability. --- cmd/root.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/root.go b/cmd/root.go index 36376a3..1b00c2e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -71,6 +71,7 @@ func executeCmd(_ *cobra.Command, _ []string) error { if commitMessage == "### NO STAGED CHAGES ###" { return nil } + l.Info("Commit message: ", commitMessage) l.Debug("Finish validating commit message includes changes") return git.Commit(commitMessage, cfg) } From 2ef61469e14bc4ffdf8a286dc0c1f1234f08ec82 Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:21:27 +0000 Subject: [PATCH 4/6] fix(cmd): correct string concatenation for commit message logging Ensure proper formatting of commit message in logs. --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 1b00c2e..d0d46c9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -71,7 +71,7 @@ func executeCmd(_ *cobra.Command, _ []string) error { if commitMessage == "### NO STAGED CHAGES ###" { return nil } - l.Info("Commit message: ", commitMessage) + l.Info("Commit message: " + commitMessage) l.Debug("Finish validating commit message includes changes") return git.Commit(commitMessage, cfg) } From 067b904c449b6843f5e953fe0dc83d747f91074f Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:21:57 +0000 Subject: [PATCH 5/6] fix(cmd): standardize log output for commit message formatting Ensure the commit message log is consistently formatted. --- cmd/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index d0d46c9..cac57c2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -71,7 +71,7 @@ func executeCmd(_ *cobra.Command, _ []string) error { if commitMessage == "### NO STAGED CHAGES ###" { return nil } - l.Info("Commit message: " + commitMessage) + l.Info("commit message: " + commitMessage) l.Debug("Finish validating commit message includes changes") return git.Commit(commitMessage, cfg) } From b1253ac4b1b12e4a9cd1a1f9778d3e103de1f3c2 Mon Sep 17 00:00:00 2001 From: jsburckhardt Date: Thu, 12 Sep 2024 08:33:03 +0000 Subject: [PATCH 6/6] chore(ci): disable test execution step in CI workflow Commented out the testing step in the CI configuration to streamline the process until tests are reintroduced. --- .github/workflows/ci.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 3cf6212..96f1043 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -41,10 +41,8 @@ jobs: with: go-version: 1.23 - - name: Run Tests - run: | - curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin - + # - name: Run Tests + # run: | # go test -v ./... release: