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
26
37import (
48 "encoding/json"
@@ -8,12 +12,11 @@ import (
812)
913
1014type 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"`
1720 } `json:"form"`
1821}
1922
@@ -28,58 +31,23 @@ func main() {
2831 panic (err )
2932 }
3033
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" {
3735 panic ("username should be jolheiser" )
3836 }
3937
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 {
4639 panic ("favNum should be 12" )
4740 }
4841
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 {
5543 panic ("pineapple should be false" )
5644 }
5745
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" {
6447 panic ("secret should be sn34ky" )
6548 }
6649}
6750
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-
8351// override panic
8452func panic (v interface {}) {
8553 fmt .Println (v )
0 commit comments