Skip to content

Commit

Permalink
Fix changelog generation when querying submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
garethjevans committed Sep 3, 2021
1 parent 4da472d commit f141ad6
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/changelog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
)

var (
Expand Down Expand Up @@ -51,17 +50,19 @@ func main() {

skipSubmodule := os.Getenv("SKIP_SUBMODULE")
if skipSubmodule != "true" {
fmt.Printf("Also querying submodule for changes...\n")
//git ls-tree HEAD src/app-autoscaler | awk '{print $3}'
toSha, err := getShaOfSubmoduleAtCommit("HEAD")
toSha, err := util.GetShaOfSubmoduleAtCommit("HEAD")
if err != nil {
panic(err)
}

fromSha, err := getShaOfSubmoduleAtCommit(commit)
fromSha, err := util.GetShaOfSubmoduleAtCommit(commit)
if err != nil {
panic(err)
}

fmt.Printf("Checking from %s to %s\n", fromSha, toSha)
if fromSha != toSha {
// get PRs from app-autoscaler too
otherPRs, err := client.FetchPullRequestsAfterCommit(owner, "app-autoscaler", branch, fromSha, toSha)
Expand Down Expand Up @@ -98,16 +99,3 @@ func main() {

fmt.Printf("Total PRs %d\n", len(prs))
}

func getShaOfSubmoduleAtCommit(commit string) (string, error) {
cmd := util.Command{Name: "git", Args: []string{"ls-tree", commit, "../app-autoscaler"}}
runner := util.DefaultCommandRunner{}

output, err := runner.RunWithoutRetry(&cmd)
if err != nil {
return "", err
}

parts := strings.Split(output, " ")
return parts[2], nil
}
19 changes: 19 additions & 0 deletions src/changelog/util/git.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package util

import "strings"

var Runner CommandRunner

func GetShaOfSubmoduleAtCommit(commit string) (string, error) {
cmd := Command{Name: "git", Args: []string{"ls-tree", commit, "../app-autoscaler"}}
if Runner == nil {
Runner = DefaultCommandRunner{}
}
output, err := Runner.RunWithoutRetry(&cmd)
if err != nil {
return "", err
}

parts := strings.Split(output, " ")
return strings.Split(parts[2], "\t")[0], nil
}
23 changes: 23 additions & 0 deletions src/changelog/util/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package util_test

import (
"changelog/util"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetShaOfSubmoduleAtCommit(t *testing.T) {
util.Runner = &FakeCommandRunner{}
sha, err := util.GetShaOfSubmoduleAtCommit("HEAD")
assert.NoError(t, err)
assert.Equal(t, sha, "e57e6dccf303e621ec4239b5d97aeba168a6de50")
}

type FakeCommandRunner struct {
}

// RunWithoutRetry Execute the command without retrying on failure and block waiting for return values.
func (d FakeCommandRunner) RunWithoutRetry(c *util.Command) (string, error) {
return "160000 commit e57e6dccf303e621ec4239b5d97aeba168a6de50\t../app-autoscaler", nil
}

0 comments on commit f141ad6

Please sign in to comment.