2017-01-04 16:33:40 +00:00
|
|
|
package tq
|
|
|
|
|
|
|
|
import (
|
2017-04-05 22:43:00 +00:00
|
|
|
"time"
|
|
|
|
|
2017-01-04 17:11:16 +00:00
|
|
|
"github.com/git-lfs/git-lfs/errors"
|
2018-01-05 18:12:57 +00:00
|
|
|
"github.com/git-lfs/git-lfs/git"
|
2017-01-04 16:33:40 +00:00
|
|
|
"github.com/git-lfs/git-lfs/lfsapi"
|
2018-09-06 21:42:41 +00:00
|
|
|
"github.com/git-lfs/git-lfs/lfshttp"
|
2017-01-04 17:11:16 +00:00
|
|
|
"github.com/rubyist/tracerx"
|
2017-01-04 16:33:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tqClient struct {
|
2017-08-24 21:51:24 +00:00
|
|
|
MaxRetries int
|
2017-01-04 16:33:40 +00:00
|
|
|
*lfsapi.Client
|
|
|
|
}
|
|
|
|
|
2018-01-05 18:12:57 +00:00
|
|
|
type batchRef struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-01-04 16:33:40 +00:00
|
|
|
type batchRequest struct {
|
2017-01-06 22:51:49 +00:00
|
|
|
Operation string `json:"operation"`
|
|
|
|
Objects []*Transfer `json:"objects"`
|
|
|
|
TransferAdapterNames []string `json:"transfers,omitempty"`
|
2018-01-05 18:12:57 +00:00
|
|
|
Ref *batchRef `json:"ref"`
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 20:00:59 +00:00
|
|
|
type BatchResponse struct {
|
2017-01-06 22:51:49 +00:00
|
|
|
Objects []*Transfer `json:"objects"`
|
2017-01-09 20:04:58 +00:00
|
|
|
TransferAdapterName string `json:"transfer"`
|
2018-09-06 21:42:41 +00:00
|
|
|
endpoint lfshttp.Endpoint
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 18:12:57 +00:00
|
|
|
func Batch(m *Manifest, dir Direction, remote string, remoteRef *git.Ref, objects []*Transfer) (*BatchResponse, error) {
|
2017-01-07 03:13:31 +00:00
|
|
|
if len(objects) == 0 {
|
2017-01-09 20:50:19 +00:00
|
|
|
return &BatchResponse{}, nil
|
2017-01-07 03:13:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 20:50:19 +00:00
|
|
|
return m.batchClient().Batch(remote, &batchRequest{
|
2017-01-09 19:52:36 +00:00
|
|
|
Operation: dir.String(),
|
2017-01-07 03:13:31 +00:00
|
|
|
Objects: objects,
|
|
|
|
TransferAdapterNames: m.GetAdapterNames(dir),
|
2018-01-05 18:12:57 +00:00
|
|
|
Ref: &batchRef{Name: remoteRef.Refspec()},
|
2017-01-09 20:50:19 +00:00
|
|
|
})
|
2017-01-07 03:13:31 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 20:50:19 +00:00
|
|
|
func (c *tqClient) Batch(remote string, bReq *batchRequest) (*BatchResponse, error) {
|
2017-01-09 20:00:59 +00:00
|
|
|
bRes := &BatchResponse{}
|
2017-01-04 16:33:40 +00:00
|
|
|
if len(bReq.Objects) == 0 {
|
2017-01-09 20:50:19 +00:00
|
|
|
return bRes, nil
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(bReq.TransferAdapterNames) == 1 && bReq.TransferAdapterNames[0] == "basic" {
|
|
|
|
bReq.TransferAdapterNames = nil
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:00:59 +00:00
|
|
|
bRes.endpoint = c.Endpoints.Endpoint(bReq.Operation, remote)
|
2017-04-05 22:43:00 +00:00
|
|
|
requestedAt := time.Now()
|
|
|
|
|
2017-08-24 21:51:49 +00:00
|
|
|
req, err := c.NewRequest("POST", bRes.endpoint, "objects/batch", bReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "batch request")
|
|
|
|
}
|
2017-08-18 00:52:57 +00:00
|
|
|
|
2017-08-24 21:51:49 +00:00
|
|
|
tracerx.Printf("api: batch %d files", len(bReq.Objects))
|
2017-01-04 17:11:16 +00:00
|
|
|
|
2018-09-06 21:42:41 +00:00
|
|
|
req = c.Client.LogRequest(req, "lfs.batch")
|
2018-09-24 23:45:32 +00:00
|
|
|
res, err := c.DoAPIRequestWithAuth(remote, lfshttp.WithRetries(req, c.MaxRetries))
|
2017-08-24 21:51:49 +00:00
|
|
|
if err != nil {
|
2017-01-04 17:11:16 +00:00
|
|
|
tracerx.Printf("api error: %s", err)
|
2017-01-09 20:50:19 +00:00
|
|
|
return nil, errors.Wrap(err, "batch response")
|
2017-01-04 17:11:16 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 21:42:41 +00:00
|
|
|
if err := lfshttp.DecodeJSON(res, bRes); err != nil {
|
2017-01-09 20:50:19 +00:00
|
|
|
return bRes, errors.Wrap(err, "batch response")
|
2017-01-04 17:20:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-04 17:11:16 +00:00
|
|
|
if res.StatusCode != 200 {
|
2018-09-06 21:42:41 +00:00
|
|
|
return nil, lfshttp.NewStatusCodeError(res)
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 22:43:00 +00:00
|
|
|
for _, obj := range bRes.Objects {
|
|
|
|
for _, a := range obj.Actions {
|
|
|
|
a.createdAt = requestedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:50:19 +00:00
|
|
|
return bRes, nil
|
2017-01-04 16:33:40 +00:00
|
|
|
}
|