patchset http routines

This commit is contained in:
Jeff Carr 2025-09-06 15:22:31 -05:00
parent c0b9518c0d
commit bf031595e3
5 changed files with 168 additions and 29 deletions

20
http.go
View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url"
"os/user" "os/user"
"strings" "strings"
@ -14,29 +15,36 @@ import (
"go.wit.com/log" "go.wit.com/log"
) )
func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) { func (f *Forge) HttpPost(base string, route string, data []byte) ([]byte, error) {
// Fix using url.JoinPath (Best Practice)
baseURL, _ := url.Parse(f.forgeURL) // "http://forge.grid.wit.com:2520")
finalURL := baseURL.JoinPath(route) // Correctly produces ...:2520/patches
var err error var err error
var req *http.Request var req *http.Request
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
// log.Info("httpPost() with len", len(data), "url", url) if req == nil {
return nil, err
}
usr, _ := user.Current() usr, _ := user.Current()
req.Header.Set("author", usr.Username) req.Header.Set("author", usr.Username)
req.Header.Set("hostname", f.hostname) req.Header.Set("hostname", f.hostname)
return rawHttpPost(req)
}
func rawHttpPost(req *http.Request) ([]byte, error) {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Error(err)
return []byte("client.Do(req) error"), err return []byte("client.Do(req) error"), err
} }
defer resp.Body.Close() defer resp.Body.Close()
// log.Info("httpPost() with len", len(data))
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Error(err)
return body, err return body, err
} }

View File

@ -8,9 +8,7 @@ import (
// retrieves current patches from forge // retrieves current patches from forge
func (f *Forge) GetPatches() error { func (f *Forge) GetPatches() error {
url := f.forgeURL + "GetPatchsets" body, err := f.HttpPost("junk", "GetPatchsets", nil)
log.Info("GetPatchsets() url", url)
body, err := f.HttpPost(url, nil)
if err != nil { if err != nil {
log.Info("httpPost() failed:", err) log.Info("httpPost() failed:", err)
return err return err

View File

@ -11,6 +11,27 @@ import (
"go.wit.com/log" "go.wit.com/log"
) )
func (f *Forge) SendPatches(what string, p *Patches) (*Patches, error) {
route := "/patches/" + what
data, err := p.Marshal()
if err != nil {
return nil, err
}
log.Infof("pb: len=%d, err=%v\n", len(data), err)
newdata, err := f.HttpPost("rm this", route, data)
if err != nil {
return nil, err
}
if newdata == nil {
return nil, err
}
if len(newdata) == 0 {
return nil, err
}
log.Info("TODO: Unmarshal() to patches", len(newdata))
return nil, err
}
// makes a new patches protobuf. These are all the patches on your machine. // makes a new patches protobuf. These are all the patches on your machine.
func NewPatches() *Patches { func NewPatches() *Patches {
x := new(Patches) x := new(Patches)
@ -47,19 +68,18 @@ func (f *Forge) SubmitDevelPatchSet(name string) (*Patchset, error) {
} }
func (f *Forge) submitPatchset(pset *Patchset) error { func (f *Forge) submitPatchset(pset *Patchset) error {
var url string
url = f.forgeURL + "patchset"
msg, err := pset.Marshal() msg, err := pset.Marshal()
if err != nil { if err != nil {
log.Info("proto.Marshal() failed:", err) log.Info("proto.Marshal() failed:", err)
return err return err
} }
log.Info("proto.Marshal() msg len", len(msg)) log.Info("proto.Marshal() msg len", len(msg))
body, err := f.HttpPost(url, msg) body, err := f.HttpPost("rm this", "patchset", msg)
if err != nil { if err != nil {
log.Info("httpPost() failed:", err) log.Info("httpPost() failed:", err)
return err return err
} }
log.Info("HTTP: proto.Marshal() sent", len(msg), "ok and got back", len(body))
newpb := NewPatches() newpb := NewPatches()
if err := newpb.Unmarshal(body); err != nil { if err := newpb.Unmarshal(body); err != nil {
@ -83,19 +103,18 @@ func (f *Forge) submitPatchset(pset *Patchset) error {
} }
func (f *Forge) SubmitPatchesNew(pset *Patches, urlpath string) (*Patches, error) { func (f *Forge) SubmitPatchesNew(pset *Patches, urlpath string) (*Patches, error) {
var url string
url = f.forgeURL + "oldpatchset"
msg, err := pset.Marshal() msg, err := pset.Marshal()
if err != nil { if err != nil {
log.Info("proto.Marshal() failed:", err) log.Info("proto.Marshal() failed:", err)
return nil, err return nil, err
} }
log.Info("proto.Marshal() msg len", len(msg)) log.Info("proto.Marshal() msg len", len(msg))
body, err := f.HttpPost(url, msg) body, err := f.HttpPost("rm this", urlpath, msg)
if err != nil { if err != nil {
log.Info("httpPost() failed:", err) log.Info("httpPost() failed:", err)
return nil, err return nil, err
} }
log.Info("HTTP: proto.Marshal() sent", len(msg), "ok and got back", len(body))
newpb := NewPatches() newpb := NewPatches()
if err := newpb.Unmarshal(body); err != nil { if err := newpb.Unmarshal(body); err != nil {

114
patchset.http.go Normal file
View File

@ -0,0 +1,114 @@
// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0
package forgepb
import (
"bytes"
"net/http"
"net/url"
"os"
"os/user"
"go.wit.com/log"
)
func (p *Patches) HttpPostVerbose(baseURL string, route string) (*Patches, error) {
p.PrintTable()
return p.HttpPost(baseURL, route)
}
func (p *Patches) HttpPost(baseURL string, route string) (*Patches, error) {
// if you ever have "http://www.wit.com//" GO will regect the server recieving it.
// Even though the linux kernel gets the network payload
// also it never gives you an error about that, it just goes away invisably inside GO
tmpURL, _ := url.Parse(baseURL) // "http://forge.grid.wit.com:2520")
finalURL := tmpURL.JoinPath("/patches/", route) // Correctly produces ...:2520/patches
data, _ := p.Marshal()
var err error
var req *http.Request
log.Info("patches PB HttpPost", finalURL.String())
req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
if req == nil {
return nil, err
}
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
hostname, _ := os.Hostname()
req.Header.Set("hostname", hostname)
newdata, err := rawHttpPost(req)
newpb := NewPatches()
err = newpb.Unmarshal(newdata)
log.Infof("patchset PB HttpPost %s sent len(%d) got len(%d)\n", finalURL.String(), p.Len(), newpb.Len())
return newpb, err
}
func (p *Patchset) HttpPost(baseURL string, route string) (*Patchset, error) {
// if you ever have "http://www.wit.com//" GO will regect the server recieving it.
// Even though the linux kernel gets the network payload
// also it never gives you an error about that, it just goes away invisably inside GO
tmpURL, _ := url.Parse(baseURL) // "http://forge.grid.wit.com:2520")
finalURL := tmpURL.JoinPath("/patchset/", route) // Correctly produces ...:2520/patches
data, _ := p.Marshal()
var err error
var req *http.Request
log.Info("patchset PB HttpPost", finalURL.String())
req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
if req == nil {
return nil, err
}
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
hostname, _ := os.Hostname()
req.Header.Set("hostname", hostname)
newdata, err := rawHttpPost(req)
newpb := new(Patchset)
err = newpb.Unmarshal(newdata)
log.Infof("patchset PB HttpPost %s sent (%d)bytes got (%d)bytes\n", finalURL.String(), len(data), len(newdata))
return newpb, err
}
func (p *Patchsets) HttpPost(baseURL string, route string) (*Patchsets, error) {
// if you ever have "http://www.wit.com//" GO will regect the server recieving it.
// Even though the linux kernel gets the network payload
// also it never gives you an error about that, it just goes away invisably inside GO
tmpURL, _ := url.Parse(baseURL) // "http://forge.grid.wit.com:2520")
finalURL := tmpURL.JoinPath("/patchsets/", route) // Correctly produces ...:2520/patches
data, _ := p.Marshal()
var err error
var req *http.Request
log.Info("patchsets PB HttpPost", finalURL.String())
req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data))
if req == nil {
return nil, err
}
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
hostname, _ := os.Hostname()
req.Header.Set("hostname", hostname)
newdata, err := rawHttpPost(req)
newpb := NewPatchsets()
err = newpb.Unmarshal(newdata)
log.Infof("patchset PB HttpPost %s sent len(%d) got len(%d)\n", finalURL.String(), p.Len(), newpb.Len())
return newpb, err
}

View File

@ -32,20 +32,20 @@ import "google/protobuf/timestamp.proto"; // Import the well-known type for Time
message Patch { message Patch {
string namespace = 1; // the base repo git namespace string namespace = 1; // the base repo git namespace
bytes data = 2; // the raw data of the whole patch bytes data = 2; // the raw data of the whole patch
string gH = 3; // after some deliberation, I think I'll just try variable names string gH = 3; // Commit Hash (%H)
string gT = 4; string gT = 4; // Tree Hash (%T)
string gP = 5; string gP = 5; // Parent Hashes (%P)
string gs = 6; string gs = 6; // Subject (%s)
string gaI = 7; // that exactly match what git uses. string gaI = 7; // Author Date, ISO 8601 format (%aI)
string gan = 8; string gan = 8; // Author Name (%an)
string gae = 9; string gae = 9; // Author Email (%ae)
string gcI = 10; string gcI = 10; // Committer Date, ISO 8601 format (%cI)
string gcn = 11; string gcn = 11; // Committer Name (%cn)
string gce = 12; string gce = 12; // Committer Email (%ce)
string gN = 13; string gN = 13; // Commit Notes (%N)
string gGG = 14; string gGG = 14; // GPG Signature, raw (%GG)
string gGS = 15; string gGS = 15; // GPG Signer Name (%GS)
string gGK = 16; string gGK = 16; // GPG Key ID (%GK)
string newHash = 17; // new hash string newHash = 17; // new hash
string state = 18; // the 'state' of the patch string state = 18; // the 'state' of the patch
string filename = 19; // `autogenpb:unique` `autogenpb:sort` string filename = 19; // `autogenpb:unique` `autogenpb:sort`