1
- package webhook
1
+ // Copyright 2022 The Gitea Authors. All rights reserved.
2
+ // Use of this source code is governed by a MIT-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package main
2
6
3
7
import (
4
8
"encoding/json"
@@ -8,12 +12,11 @@ import (
8
12
)
9
13
10
14
type Payload struct {
11
- Form map [string ]struct {
12
- Label string `json:"label"`
13
- Type string `json:"type"`
14
- Required bool `json:"required"`
15
- Default interface {} `json:"default"`
16
- Value interface {} `json:"value"`
15
+ Form struct {
16
+ Username string `json:"username"`
17
+ FavoriteNumber int `json:"favorite-number"`
18
+ Pineapple bool `json:"pineapple"`
19
+ Passphrase string `json:"passphrase"`
17
20
} `json:"form"`
18
21
}
19
22
@@ -28,58 +31,23 @@ func main() {
28
31
panic (err )
29
32
}
30
33
31
- username , ok := p .Form ["username" ]
32
- if ! ok {
33
- panic ("username not found in form data" )
34
- }
35
- checkType (username .Type , username .Value )
36
- if username .Value .(string ) != "jolheiser" {
34
+ if p .Form .Username != "jolheiser" {
37
35
panic ("username should be jolheiser" )
38
36
}
39
37
40
- favNum , ok := p .Form ["favorite-number" ]
41
- if ! ok {
42
- panic ("favorite-number not found in form data" )
43
- }
44
- checkType (favNum .Type , favNum .Value )
45
- if username .Value .(float64 ) != 12 {
38
+ if p .Form .FavoriteNumber != 12 {
46
39
panic ("favNum should be 12" )
47
40
}
48
41
49
- pineapple , ok := p .Form ["pineapple" ]
50
- if ! ok {
51
- panic ("pineapple not found in form data" )
52
- }
53
- checkType (pineapple .Type , pineapple .Value )
54
- if pineapple .Value .(bool ) {
42
+ if p .Form .Pineapple {
55
43
panic ("pineapple should be false" )
56
44
}
57
45
58
- secret , ok := p .Form ["secret" ]
59
- if ! ok {
60
- panic ("secret not found in form data" )
61
- }
62
- checkType (secret .Type , secret .Value )
63
- if secret .Value .(string ) != "sn34ky" {
46
+ if p .Form .Passphrase != "sn34ky" {
64
47
panic ("secret should be sn34ky" )
65
48
}
66
49
}
67
50
68
- func checkType (typ string , val interface {}) {
69
- var ok bool
70
- switch typ {
71
- case "text" , "secret" :
72
- _ , ok = val .(string )
73
- case "bool" :
74
- _ , ok = val .(bool )
75
- case "number" :
76
- _ , ok = val .(float64 )
77
- }
78
- if ! ok {
79
- panic (fmt .Sprintf ("unexpected type %q for %v" , typ , val ))
80
- }
81
- }
82
-
83
51
// override panic
84
52
func panic (v interface {}) {
85
53
fmt .Println (v )
0 commit comments