summaryrefslogtreecommitdiff
path: root/handlePatches.go
blob: 2c5ce833accff7011d4672c8c99e53ec7473fa46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main

import (
	"net/http"

	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/lib/protobuf/httppb"
	"go.wit.com/log"
)

func addNewPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
	newPatchesPB := new(forgepb.Patches)
	for newpatch := range pb.IterAll() {
		me.forge.AddNewPatch(newpatch)
		newPatchesPB.Append(newpatch)
	}
	return newPatchesPB
}

func handleMergedPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
	newPatchesPB := new(forgepb.Patches)
	for newpatch := range pb.IterAll() {
		if expirePatch(newpatch) {
			newPatchesPB.Append(newpatch)
		}
	}
	return newPatchesPB
}

func sendPendingPatches(pb *forgepb.Patches, reqPB *httppb.HttpRequest) *forgepb.Patches {
	allPatchesPB := new(forgepb.Patches)
	for pset := range me.forge.Patchsets.IterAll() {
		for newpatch := range pset.Patches.IterAll() {
			allPatchesPB.Append(newpatch)
		}
	}
	return allPatchesPB
}

func sendPendingPatchsets(pb *forgepb.Patchsets, reqPB *httppb.HttpRequest) *forgepb.Patchsets {
	allPatchsetsPB := new(forgepb.Patchsets)
	for pset := range me.forge.Patchsets.IterAll() {
		allPatchsetsPB.Append(pset)
	}
	return allPatchsetsPB
}

/*
func handlePatches(w http.ResponseWriter, pb *forgepb.Patches) error {
	route := pb.HttpRequest.Route

	log.Info("GOT PATCHES ROUTE", route, "with # patches =", pb.Len())
	if strings.HasPrefix(route, "/patches/old") {
	} else if strings.HasPrefix(route, "/patches/new") {
		log.Info("add new patches")
	} else {
		log.Info("unknown route", route)
	}

	return nil
}
*/

func makeReposPB(reqPB *httppb.HttpRequest) (*gitpb.Repos, error) {
	pb := gitpb.NewRepos()
	err := pb.Unmarshal(reqPB.ServerData)
	return pb, err
}

func makePatchesPB(reqPB *httppb.HttpRequest) (*forgepb.Patches, error) {
	pb := forgepb.NewPatches()
	err := pb.Unmarshal(reqPB.ServerData)
	return pb, err
}

func makePatchsetsPB(reqPB *httppb.HttpRequest) (*forgepb.Patchsets, error) {
	pb := forgepb.NewPatchsets()
	err := pb.Unmarshal(reqPB.ServerData)
	return pb, err
}

func sendPatchesError(w http.ResponseWriter, r *forgepb.Patches, err error) error {
	log.Info("send error back to user", err)
	return nil
}

/*
func marshalPatchesPB(r *http.Request, msg []byte) (*forgepb.Patches, error) {
	pb := forgepb.NewPatches()

	if err := pb.Unmarshal(msg); err != nil {
		log.Info("proto.Unmarshal() failed on wire message len", len(msg), err)
		// add the header
		pb.AddHttpToPB(r)
		return pb, err
	}
	// add the header
	pb.AddHttpToPB(r)
	return pb, nil
}
*/