blob: 17ec26ed1b0ce3a392e89790cc9adbbe59a90ea3 [file] [log] [blame]
// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gerrit
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
var jsonPrefix = []byte(")]}'")
type File struct {
Status string
LinesInserted int `json:"lines_inserted"`
SizeDelta int `json:"size_delta"`
Size int
Content []byte
}
type Change struct {
Files map[string]*File
}
type CheckerInput struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
Repository string `json:"repository"`
Status string `json:"status"`
Blocking []string `json:"blocking"`
Query string `json:"query"`
}
// Gerrit doesn't use the format with "T" in the middle, so must
// define a custom serializer.
const timeLayout = "2006-01-02 15:04:05.000000000"
type Timestamp time.Time
func (ts *Timestamp) String() string {
return ((time.Time)(*ts)).String()
}
var _ = (json.Marshaler)((*Timestamp)(nil))
func (ts *Timestamp) MarshalJSON() ([]byte, error) {
t := (*time.Time)(ts).UTC()
return []byte("\"" + t.Format(timeLayout) + "\""), nil
}
var _ = (json.Unmarshaler)((*Timestamp)(nil))
func (ts *Timestamp) UnmarshalJSON(b []byte) error {
b = bytes.TrimPrefix(b, []byte{'"'})
b = bytes.TrimSuffix(b, []byte{'"'})
t, err := time.Parse(timeLayout, string(b))
if err != nil {
return err
}
*ts = Timestamp(t)
return nil
}
type CheckerInfo struct {
UUID string `json:"uuid"`
Name string
Description string
URL string `json:"url"`
Repository string `json:"repository"`
Status string
Blocking []string `json:"blocking"`
Query string `json:"query"`
Created Timestamp `json:"created"`
Updated Timestamp `json:"updated"`
}
func (info *CheckerInfo) String() string {
out, _ := json.Marshal(info)
return string(out)
}
// Unmarshal unmarshals Gerrit JSON, stripping the security prefix.
func Unmarshal(content []byte, dest interface{}) error {
if !bytes.HasPrefix(content, jsonPrefix) {
if len(content) > 100 {
content = content[:100]
}
bodyStr := string(content)
return fmt.Errorf("prefix %q not found, got %s", jsonPrefix, bodyStr)
}
content = bytes.TrimPrefix(content, []byte(jsonPrefix))
return json.Unmarshal(content, dest)
}
type PendingCheckInfo struct {
State string
}
type CheckablePatchSetInfo struct {
Repository string
ChangeNumber int `json:"change_number"`
PatchSetID int `json:"patch_set_id"`
}
func (in *CheckablePatchSetInfo) String() string {
out, _ := json.Marshal(in)
return string(out)
}
type PendingChecksInfo struct {
PatchSet *CheckablePatchSetInfo `json:"patch_set"`
PendingChecks map[string]*PendingCheckInfo `json:"pending_checks"`
}
func (info *PendingCheckInfo) String() string {
out, _ := json.Marshal(info)
return string(out)
}
type CheckInput struct {
CheckerUUID string `json:"checker_uuid"`
State string `json:"state"`
Message string `json:"message"`
URL string `json:"url"`
Started *Timestamp `json:"started"`
}
func (in *CheckInput) String() string {
out, _ := json.Marshal(in)
return string(out)
}
type CheckInfo struct {
Repository string `json:"repository"`
ChangeNumber int `json:"change_number"`
PatchSetID int `json:"patch_set_id"`
CheckerUUID string `json:"checker_uuid"`
State string `json:"state"`
Message string `json:"message"`
Started Timestamp `json:"started"`
Finished Timestamp `json:"finished"`
Created Timestamp `json:"created"`
Updated Timestamp `json:"updated"`
CheckerName string `json:"checker_name"`
CheckerStatus string `json:"checker_status"`
Blocking []string `json:"blocking"`
}