Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ test-direct-mode-multi-sdk:
test-pr-mode:
docker compose run --rm main ./testing/test.sh ./testing/pr-mode.env

test-pr-mode-signed-commits:
docker compose run --rm main ./testing/test.sh ./testing/pr-mode-signed-commits.env

test-push-code-samples-only:
docker compose run --rm main ./testing/test.sh ./testing/push-code-samples-only.env

Expand Down
66 changes: 43 additions & 23 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,32 +370,45 @@ func (g *Git) CommitAndPush(openAPIDocVersion, speakeasyVersion, doc string, act

logging.Info("Commit and pushing changes to git")

if err := g.Add("."); err != nil {
return "", fmt.Errorf("error adding changes: %w", err)
catchAllCommitMessage := "feat: regenerated with Speakeasy CLI"
if action == environment.ActionSuggest {
catchAllCommitMessage = "feat: suggestions for OpenAPI spec"
}

var commitMessage string
if action == environment.ActionRunWorkflow {
commitMessage = fmt.Sprintf("ci: regenerated with OpenAPI Doc %s, Speakeasy CLI %s", openAPIDocVersion, speakeasyVersion)
if sourcesOnly {
commitMessage = fmt.Sprintf("ci: regenerated with Speakeasy CLI %s", speakeasyVersion)
}
} else if action == environment.ActionSuggest {
commitMessage = fmt.Sprintf("ci: suggestions for OpenAPI doc %s", doc)
commits := []struct {
paths []string
msg string
}{
{paths: []string{"**/.speakeasy/", "*gen.yaml", "*gen.lock", "*workflow.yaml", "*workflow.lock"}, msg: "build: Speakeasy config and lock files"},
{paths: []string{"*.md"}, msg: "docs: regenerate markdown files"},
{paths: []string{"."}, msg: catchAllCommitMessage},
}

// Create commit message
if !environment.GetSignedCommits() {
commitHash, err := w.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Name: "speakeasybot",
Email: "[email protected]",
When: time.Now(),
},
All: true,
})
if err != nil {
return "", fmt.Errorf("error committing changes: %w", err)
var err error

var lastCommitHash plumbing.Hash
for _, commit := range commits {
for _, path := range commit.paths {
if err = g.Add(path); err != nil {
logging.Info(fmt.Errorf("unable to add changes for %v: %w", path, err).Error())
}
}

h, err := w.Commit(commit.msg, &git.CommitOptions{
Author: &object.Signature{
Name: "speakeasybot",
Email: "[email protected]",
When: time.Now(),
},
AllowEmptyCommits: false,
All: commit.msg == catchAllCommitMessage,
})
if err != nil {
logging.Info(fmt.Errorf("unable to commit changes for %v: %w", commit.paths, err).Error())
} else {
lastCommitHash = h
}
}

if err := g.repo.Push(&git.PushOptions{
Expand All @@ -404,7 +417,13 @@ func (g *Git) CommitAndPush(openAPIDocVersion, speakeasyVersion, doc string, act
}); err != nil {
return "", pushErr(err)
}
return commitHash.String(), nil
return lastCommitHash.String(), nil
}

// ---- START Signed commits ----
// TODO: Due to priority constraints we don't split up into multiple commits like we do above
if err := g.Add("."); err != nil {
return "", fmt.Errorf("error adding changes: %w", err)
}

branch, err := g.GetCurrentBranch()
Expand Down Expand Up @@ -447,7 +466,7 @@ func (g *Git) CommitAndPush(openAPIDocVersion, speakeasyVersion, doc string, act

// Commit changes
commitResult, _, err := g.client.Git.CreateCommit(context.Background(), owner, repo, &github.Commit{
Message: github.String(commitMessage),
Message: github.String(catchAllCommitMessage),
Tree: &github.Tree{SHA: tree.SHA},
Parents: []*github.Commit{parentCommit}}, &github.CreateCommitOptions{})
if err != nil {
Expand Down Expand Up @@ -520,6 +539,7 @@ func (g *Git) createAndPushTree(ref *github.Reference, sourceFiles git.Status) (
tree, _, err = g.client.Git.CreateTree(context.Background(), owner, repo, *ref.Object.SHA, entries)
return tree, err
}

func (g *Git) Add(arg string) error {
// We execute this manually because go-git doesn't properly support gitignore
cmd := exec.Command("git", "add", arg)
Expand Down
6 changes: 6 additions & 0 deletions testing/pr-mode-signed-commits.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
INPUT_MODE="pr"
INPUT_LANGUAGES="- go"
GITHUB_REPOSITORY="speakeasy-api/sdk-generation-action-test-repo"
INPUT_FORCE=true
RUN_FINALIZE=true
INPUT_SIGNED_COMMITS=true
3 changes: 1 addition & 2 deletions testing/pr-mode.env
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
INPUT_MODE="pr"
INPUT_ACTION="generate"
INPUT_LANGUAGES="- go"
GITHUB_REPOSITORY="speakeasy-api/sdk-generation-action-test-repo"
INPUT_FORCE=true
RUN_FINALIZE=true
INPUT_SIGNED_COMMITS=true
INPUT_SIGNED_COMMITS=false
4 changes: 2 additions & 2 deletions testing/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV_FILE=$1

function run_action() {
rm -rf ./repo || true
rm ./bin/speakeasy || true
rm -f ./bin/speakeasy || true
go run main.go
}

Expand All @@ -20,6 +20,6 @@ fi

set -o allexport && source ${ENV_FILE} && set +o allexport

rm output.txt || true
rm -f output.txt
INPUT_ACTION="run-workflow"
run_action
Loading