diff --git a/mirrorcat/program.go b/mirrorcat/program.go index efed547..d6bb395 100644 --- a/mirrorcat/program.go +++ b/mirrorcat/program.go @@ -77,8 +77,13 @@ func handlePushEvent(output http.ResponseWriter, req *http.Request) { return } - err = mirrorcat.Push(context.Background(), viper.GetString("original"), viper.GetString("mirror"), pushed.Ref) - if err != nil { + original := viper.GetString("original") + mirror := viper.GetString("mirror") + + err = mirrorcat.Push(context.Background(), original, mirror, pushed.Ref) + if err == nil { + log.Println("Pushed", pushed.Ref, "at", pushed.Head.ID, "from", original, "to", mirror) + } else { output.WriteHeader(http.StatusInternalServerError) log.Println("Unable to complete push:\n ", err.Error()) return diff --git a/push.go b/push.go index 94a56ef..6d85dad 100644 --- a/push.go +++ b/push.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "log" "os" "os/exec" "path" @@ -21,12 +20,13 @@ type PushEvent struct { Size int `json:"size"` DistinctSize int `json:"distinct_size"` Commits []Commit `json:"commits"` + Head Commit `json:"head_commit"` } // Commit is an item detailed in the PushEvent page linked above, which contains metadata // about commits that were pushed and that we're being informed of by a webhook. type Commit struct { - ID string `json:"sha"` + ID string `json:"id"` Message string `json:"message"` Author `json:"author"` URL string `json:"url"` @@ -105,7 +105,6 @@ func Push(ctx context.Context, original, mirror, ref string) (err error) { return } - log.Println("Pushing ", mirrorRemoteHandle, normalized) pusher := exec.CommandContext(ctx, "git", "push", mirrorRemoteHandle, normalized) pusher.Dir = cloneLoc err = runCmd(pusher) diff --git a/push_test.go b/push_test.go index 29542ae..376ad04 100644 --- a/push_test.go +++ b/push_test.go @@ -2,7 +2,9 @@ package mirrorcat_test import ( "context" + "encoding/json" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -23,6 +25,42 @@ func ExampleNormalizeRef() { // myBranch } +func TestPushEvent_UnmarshalJSON(t *testing.T) { + fileContent, err := os.Open(path.Join(".", "testdata", "examplePush.json")) + if err != nil { + t.Error(err) + t.FailNow() + } + + limited := &io.LimitedReader{ + R: fileContent, + N: 5 * 1024 * 1024, + } + + limitedContent, err := ioutil.ReadAll(limited) + if err != nil { + t.Error(err) + t.FailNow() + } + + var subject mirrorcat.PushEvent + err = json.Unmarshal(limitedContent, &subject) + if err != nil { + t.Error(err) + t.FailNow() + } + + if want := "refs/heads/current"; subject.Ref != want { + t.Logf("\ngot: %q\nwant: %q", subject.Ref, want) + t.Fail() + } + + if want := "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c"; subject.Head.ID != want { + t.Logf("\ngot: %q\nwant: %q", subject.Head.ID, want) + t.Fail() + } +} + //TestNormalizeRef exists to test edge cases that would just be confusing in a Example block. func TestNormalizeRef(t *testing.T) { testCases := []struct {