зеркало из https://github.com/Azure/mirrorcat.git
Logging tells you the commit that was pushed
This commit is contained in:
Родитель
29e13d1caf
Коммит
776cb93772
|
@ -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
|
||||
|
|
5
push.go
5
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)
|
||||
|
|
38
push_test.go
38
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 {
|
||||
|
|
Загрузка…
Ссылка в новой задаче