@@ -682,6 +682,59 @@ func WechatworkHooksNewPost(ctx *context.Context) {
682
682
ctx .Redirect (orCtx .Link )
683
683
}
684
684
685
+ // PackagistHooksNewPost response for creating packagist hook
686
+ func PackagistHooksNewPost (ctx * context.Context ) {
687
+ form := web .GetForm (ctx ).(* forms.NewPackagistHookForm )
688
+ ctx .Data ["Title" ] = ctx .Tr ("repo.settings" )
689
+ ctx .Data ["PageIsSettingsHooks" ] = true
690
+ ctx .Data ["PageIsSettingsHooksNew" ] = true
691
+ ctx .Data ["Webhook" ] = webhook.Webhook {HookEvent : & webhook.HookEvent {}}
692
+ ctx .Data ["HookType" ] = webhook .PACKAGIST
693
+
694
+ orCtx , err := getOrgRepoCtx (ctx )
695
+ if err != nil {
696
+ ctx .ServerError ("getOrgRepoCtx" , err )
697
+ return
698
+ }
699
+
700
+ if ctx .HasError () {
701
+ ctx .HTML (http .StatusOK , orCtx .NewTemplate )
702
+ return
703
+ }
704
+
705
+ meta , err := json .Marshal (& webhook_service.PackagistMeta {
706
+ Username : form .Username ,
707
+ APIToken : form .APIToken ,
708
+ PackageURL : form .PackageURL ,
709
+ })
710
+ if err != nil {
711
+ ctx .ServerError ("Marshal" , err )
712
+ return
713
+ }
714
+
715
+ w := & webhook.Webhook {
716
+ RepoID : orCtx .RepoID ,
717
+ URL : fmt .Sprintf ("https://packagist.org/api/update-package?username=%s&apiToken=%s" , url .QueryEscape (form .Username ), url .QueryEscape (form .APIToken )),
718
+ ContentType : webhook .ContentTypeJSON ,
719
+ HookEvent : ParseHookEvent (form .WebhookForm ),
720
+ IsActive : form .Active ,
721
+ Type : webhook .PACKAGIST ,
722
+ Meta : string (meta ),
723
+ OrgID : orCtx .OrgID ,
724
+ IsSystemWebhook : orCtx .IsSystemWebhook ,
725
+ }
726
+ if err := w .UpdateEvent (); err != nil {
727
+ ctx .ServerError ("UpdateEvent" , err )
728
+ return
729
+ } else if err := webhook .CreateWebhook (db .DefaultContext , w ); err != nil {
730
+ ctx .ServerError ("CreateWebhook" , err )
731
+ return
732
+ }
733
+
734
+ ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
735
+ ctx .Redirect (orCtx .Link )
736
+ }
737
+
685
738
func checkWebhook (ctx * context.Context ) (* orgRepoCtx , * webhook.Webhook ) {
686
739
ctx .Data ["RequireHighlightJS" ] = true
687
740
@@ -719,6 +772,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
719
772
ctx .Data ["TelegramHook" ] = webhook_service .GetTelegramHook (w )
720
773
case webhook .MATRIX :
721
774
ctx .Data ["MatrixHook" ] = webhook_service .GetMatrixHook (w )
775
+ case webhook .PACKAGIST :
776
+ ctx .Data ["PackagistHook" ] = webhook_service .GetPackagistHook (w )
722
777
}
723
778
724
779
ctx .Data ["History" ], err = w .History (1 )
@@ -1137,6 +1192,50 @@ func WechatworkHooksEditPost(ctx *context.Context) {
1137
1192
ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
1138
1193
}
1139
1194
1195
+ // PackagistHooksEditPost response for editing packagist hook
1196
+ func PackagistHooksEditPost (ctx * context.Context ) {
1197
+ form := web .GetForm (ctx ).(* forms.NewPackagistHookForm )
1198
+ ctx .Data ["Title" ] = ctx .Tr ("repo.settings" )
1199
+ ctx .Data ["PageIsSettingsHooks" ] = true
1200
+ ctx .Data ["PageIsSettingsHooksEdit" ] = true
1201
+
1202
+ orCtx , w := checkWebhook (ctx )
1203
+ if ctx .Written () {
1204
+ return
1205
+ }
1206
+ ctx .Data ["Webhook" ] = w
1207
+
1208
+ if ctx .HasError () {
1209
+ ctx .HTML (http .StatusOK , orCtx .NewTemplate )
1210
+ return
1211
+ }
1212
+
1213
+ meta , err := json .Marshal (& webhook_service.PackagistMeta {
1214
+ Username : form .Username ,
1215
+ APIToken : form .APIToken ,
1216
+ PackageURL : form .PackageURL ,
1217
+ })
1218
+ if err != nil {
1219
+ ctx .ServerError ("Marshal" , err )
1220
+ return
1221
+ }
1222
+
1223
+ w .Meta = string (meta )
1224
+ w .URL = fmt .Sprintf ("https://packagist.org/api/update-package?username=%s&apiToken=%s" , url .QueryEscape (form .Username ), url .QueryEscape (form .APIToken ))
1225
+ w .HookEvent = ParseHookEvent (form .WebhookForm )
1226
+ w .IsActive = form .Active
1227
+ if err := w .UpdateEvent (); err != nil {
1228
+ ctx .ServerError ("UpdateEvent" , err )
1229
+ return
1230
+ } else if err := webhook .UpdateWebhook (w ); err != nil {
1231
+ ctx .ServerError ("UpdateWebhook" , err )
1232
+ return
1233
+ }
1234
+
1235
+ ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
1236
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
1237
+ }
1238
+
1140
1239
// TestWebhook test if web hook is work fine
1141
1240
func TestWebhook (ctx * context.Context ) {
1142
1241
hookID := ctx .ParamsInt64 (":id" )
0 commit comments