-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpatch.go
More file actions
78 lines (64 loc) · 1.87 KB
/
patch.go
File metadata and controls
78 lines (64 loc) · 1.87 KB
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
package main
import (
"fmt"
"log"
"net/http"
"github.com/aymanbagabas/go-udiff"
"app/internal/myers"
)
const wikiBotUserID = 427613
func Diff(name, before, after string) string {
edits := myers.ComputeEdits(before, after)
unified, err := udiff.ToUnified(name, name, before, edits, 3)
if err != nil {
// Can't happen: edits are consistent.
log.Fatalf("internal error in diff.Unified: %v", err)
}
return unified
}
const PatchTypeSubject string = "subject"
const PatchTypeEpisode string = "episode"
const OrderByCreatedAt string = "created_at"
const OrderByUpdatedAt string = "updated_at"
func readableStateToDBValues(state string, defaultState string) ([]int32, string, string, error) {
if state == "" {
state = defaultState
}
var order string
var stateVals = make([]int32, 0, 5)
switch state {
case StateFilterPending:
order = OrderByCreatedAt
state = StateFilterPending
stateVals = append(stateVals, PatchStatePending)
case StateFilterAll:
order = OrderByCreatedAt
state = StateFilterAll
stateVals = append(stateVals, PatchStatePending, PatchStateAccepted, PatchStateRejected, PatchStateOutdated)
case StateFilterAccepted:
order = OrderByUpdatedAt
state = StateFilterAccepted
stateVals = append(stateVals, PatchStateAccepted)
case StateFilterRejected:
order = OrderByUpdatedAt
state = StateFilterRejected
stateVals = append(stateVals, PatchStateRejected)
case StateFilterReviewed:
order = OrderByUpdatedAt
state = StateFilterReviewed
stateVals = append(stateVals, PatchStateRejected, PatchStateOutdated, PatchStateAccepted)
default:
return nil, "", "", &HttpError{
StatusCode: http.StatusBadGateway,
Message: "invalid patch state",
}
}
return stateVals, state, order, nil
}
type HttpError struct {
StatusCode int
Message string
}
func (h *HttpError) Error() string {
return fmt.Sprintf("%d: %s", h.StatusCode, h.Message)
}