Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cmd/bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"errors"
"fmt"
"log/slog"
"os"

"github.com/kelseyhightower/envconfig"

Expand All @@ -29,9 +31,10 @@ type config struct {

GitHub github.Config

IsCI bool `envconfig:"CI"`
PR int `envconfig:"DRONE_PULL_REQUEST" required:"true"`
Event string `envconfig:"DRONE_BUILD_EVENT"`
IsCI bool `envconfig:"CI"`
PR int `envconfig:"DRONE_PULL_REQUEST" required:"true"`
Event string `envconfig:"DRONE_BUILD_EVENT"`
LogLevel string `envconfig:"LOG_LEVEL" default:"info"`
}

const pullRequestEvent = "pull_request"
Expand Down Expand Up @@ -60,6 +63,12 @@ func (c config) validate() error {
return fmt.Errorf("github configuration: %w", err)
}

var level slog.Level
if err := level.UnmarshalText([]byte(c.LogLevel)); err != nil {
return fmt.Errorf("parsing log level: %w", err)
}
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))
slog.SetDefault(logger)
// TODO check repo exists

return nil
Expand Down
13 changes: 8 additions & 5 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ func realMain(ctx context.Context) error {

repo := git.NewRepository(cfg.Manifests.RepoPath)

oldCommit := "master"
newCommit, err := repo.GetCurrentCommit(ctx)
oldCommit, err := repo.GetCommit(ctx, "HEAD^")
if err != nil {
return fmt.Errorf("getting current commit: %w", err)
return fmt.Errorf("getting commit: %w", err)
}

rev := oldCommit + "..." + newCommit
cf, err := repo.ChangedFiles(ctx, rev)
newCommit, err := repo.GetCommit(ctx, "HEAD")
if err != nil {
return fmt.Errorf("getting commit: %w", err)
}

cf, err := repo.ChangedFiles(ctx, oldCommit, newCommit)
if err != nil {
return err
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
Expand All @@ -29,27 +30,31 @@ func (r Repository) git(ctx context.Context, args ...string) ([]byte, error) {

out, err := cmd.Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
return nil, fmt.Errorf("running %v: %w\n%s", cmd, ee, ee.Stderr)
}
return nil, fmt.Errorf("running %v: %w", cmd, err)
}

return out, nil
}

func (r Repository) GetCurrentCommit(ctx context.Context) (string, error) {
head, err := r.git(ctx, "rev-parse", "HEAD")
func (r Repository) GetCommit(ctx context.Context, ref string) (string, error) {
head, err := r.git(ctx, "rev-parse", ref)
if err != nil {
return "", err
}

return strings.TrimSpace(string(head)), nil
}

func (r Repository) ChangedFiles(ctx context.Context, rev string) (ChangedFiles, error) {
func (r Repository) ChangedFiles(ctx context.Context, oldCommit string, newCommit string) (ChangedFiles, error) {
cf := ChangedFiles{
Renamed: make(map[string]string),
}

out, err := r.git(ctx, "diff", "--name-status", rev)
out, err := r.git(ctx, "diff", "--name-status", oldCommit, newCommit)
if err != nil {
return cf, err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"strings"

Expand Down Expand Up @@ -32,6 +33,7 @@ func NewClient(ctx context.Context, cfg Config) (Client, error) {
var token = cfg.Token

if cfg.AppID > 0 {
slog.Info("Using GitHub app authentication")
t, err := ghinstallation.NewAppsTransport(
httpcache.NewMemoryCacheTransport(),
cfg.AppID,
Expand All @@ -52,6 +54,8 @@ func NewClient(ctx context.Context, cfg Config) (Client, error) {
}

token = tok.GetToken()
} else {
slog.Info("Using GitHub token authentication")
}

ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
Expand Down
Loading