@@ -26,27 +26,40 @@ import (
2626)
2727
2828type CommandHandler struct {
29- Config * config.Config
30- DB * db.DB
31- OAuth * gh.OAuth
32- StateCache * cache.Cache [string , int64 ]
33- ClientFactory * gh.ClientFactory
34- EncryptionKey string
35- ContextCache * cache.Cache [string , models.MessageContext ]
29+ Config * config.Config
30+ DB * db.DB
31+ OAuth * gh.OAuth
32+ StateCache * cache.Cache [string , int64 ]
33+ ClientFactory * gh.ClientFactory
34+ EncryptionKey string
35+ ContextCache * cache.Cache [string , models.MessageContext ]
3636}
3737
3838func NewCommandHandler (cfg * config.Config , database * db.DB , oauth * gh.OAuth , stateCache * cache.Cache [string , int64 ], factory * gh.ClientFactory , key string , ctxCache * cache.Cache [string , models.MessageContext ]) * CommandHandler {
3939 return & CommandHandler {
40- Config : cfg ,
41- DB : database ,
42- OAuth : oauth ,
43- StateCache : stateCache ,
44- ClientFactory : factory ,
45- EncryptionKey : key ,
46- ContextCache : ctxCache ,
40+ Config : cfg ,
41+ DB : database ,
42+ OAuth : oauth ,
43+ StateCache : stateCache ,
44+ ClientFactory : factory ,
45+ EncryptionKey : key ,
46+ ContextCache : ctxCache ,
4747 }
4848}
4949
50+ func requireAdminOrPrivate (b * gotgbot.Bot , ctx * ext.Context , deniedMessage string ) error {
51+ if ctx .EffectiveChat != nil && ctx .EffectiveChat .Type == gotgbot .ChatTypePrivate {
52+ return nil
53+ }
54+
55+ if ctx .EffectiveChat != nil && ctx .EffectiveUser != nil && utils .IsAdmin (b , ctx .EffectiveChat .Id , ctx .EffectiveUser .Id ) {
56+ return nil
57+ }
58+
59+ _ , err := ctx .EffectiveMessage .Reply (b , deniedMessage , nil )
60+ return err
61+ }
62+
5063func (h * CommandHandler ) Start (b * gotgbot.Bot , ctx * ext.Context ) error {
5164 msg := `<b>Welcome to the GitHub Bot!</b> 🤖
5265
@@ -177,8 +190,7 @@ func repoPageKeyboardNav(page int, resp *github.Response) []gotgbot.InlineKeyboa
177190}
178191
179192func (h * CommandHandler ) AddRepo (b * gotgbot.Bot , ctx * ext.Context ) error {
180- if ctx .EffectiveChat .Type != gotgbot .ChatTypePrivate && ! utils .IsAdmin (b , ctx .EffectiveChat .Id , ctx .EffectiveUser .Id ) {
181- _ , err := ctx .EffectiveMessage .Reply (b , "Only admins can add repositories." , nil )
193+ if err := requireAdminOrPrivate (b , ctx , "Only admins can add repositories." ); err != nil {
182194 return err
183195 }
184196
@@ -352,8 +364,7 @@ func (h *CommandHandler) sendRepoList(b *gotgbot.Bot, ctx *ext.Context, page int
352364}
353365
354366func (h * CommandHandler ) Settings (b * gotgbot.Bot , ctx * ext.Context ) error {
355- if ctx .EffectiveChat .Type != gotgbot .ChatTypePrivate && ! utils .IsAdmin (b , ctx .EffectiveChat .Id , ctx .EffectiveUser .Id ) {
356- _ , err := ctx .EffectiveMessage .Reply (b , "Only admins can modify settings." , nil )
367+ if err := requireAdminOrPrivate (b , ctx , "Only admins can modify settings." ); err != nil {
357368 return err
358369 }
359370
@@ -384,8 +395,7 @@ func (h *CommandHandler) Settings(b *gotgbot.Bot, ctx *ext.Context) error {
384395}
385396
386397func (h * CommandHandler ) RemoveRepo (b * gotgbot.Bot , ctx * ext.Context ) error {
387- if ctx .EffectiveChat .Type != gotgbot .ChatTypePrivate && ! utils .IsAdmin (b , ctx .EffectiveChat .Id , ctx .EffectiveUser .Id ) {
388- _ , err := ctx .EffectiveMessage .Reply (b , "Only admins can remove repositories." , nil )
398+ if err := requireAdminOrPrivate (b , ctx , "Only admins can remove repositories." ); err != nil {
389399 return err
390400 }
391401
@@ -414,31 +424,31 @@ func (h *CommandHandler) RemoveRepo(b *gotgbot.Bot, ctx *ext.Context) error {
414424 }
415425 } else {
416426
417- var owner , repo string
418- for i := 0 ; i < len (repoFullName ); i ++ {
419- if repoFullName [i ] == '/' {
420- owner = repoFullName [:i ]
421- repo = repoFullName [i + 1 :]
422- break
423- }
427+ var owner , repo string
428+ for i := 0 ; i < len (repoFullName ); i ++ {
429+ if repoFullName [i ] == '/' {
430+ owner = repoFullName [:i ]
431+ repo = repoFullName [i + 1 :]
432+ break
424433 }
434+ }
425435
426- if owner != "" && repo != "" {
427- _ , err := client .Repositories .DeleteHook (context .Background (), owner , repo , link .WebhookID )
428- if err != nil {
429- if h .handleAuthError (b , ctx , err ) {
430- webhookStatusMsg = "\n \n ⚠️ <b>Warning:</b> GitHub authentication failed. Webhook not removed."
436+ if owner != "" && repo != "" {
437+ _ , err := client .Repositories .DeleteHook (context .Background (), owner , repo , link .WebhookID )
438+ if err != nil {
439+ if h .handleAuthError (b , ctx , err ) {
440+ webhookStatusMsg = "\n \n ⚠️ <b>Warning:</b> GitHub authentication failed. Webhook not removed."
441+ } else {
442+ var errResp * github.ErrorResponse
443+ if errors .As (err , & errResp ) && errResp .Response .StatusCode == http .StatusNotFound {
431444 } else {
432- var errResp * github.ErrorResponse
433- if errors .As (err , & errResp ) && errResp .Response .StatusCode == http .StatusNotFound {
434- } else {
435- webhookStatusMsg = fmt .Sprintf ("\n \n ⚠️ <b>Warning:</b> Failed to remove webhook from GitHub: %v" , err )
436- }
445+ webhookStatusMsg = fmt .Sprintf ("\n \n ⚠️ <b>Warning:</b> Failed to remove webhook from GitHub: %v" , err )
437446 }
438447 }
439448 }
440449 }
441450 }
451+ }
442452
443453 err = h .DB .RemoveRepoLink (context .Background (), ctx .EffectiveChat .Id , repoFullName )
444454 if err != nil {
@@ -494,7 +504,6 @@ Visit the <a href="https://github.com/AshokShau/GithubBot">GitHub page</a> for m
494504 return err
495505}
496506
497-
498507func (h * CommandHandler ) Privacy (b * gotgbot.Bot , ctx * ext.Context ) error {
499508 msg := `<b>Privacy Policy</b>
500509
@@ -557,6 +566,10 @@ func (h *CommandHandler) Reopen(b *gotgbot.Bot, ctx *ext.Context) error {
557566
558567func (h * CommandHandler ) Approve (b * gotgbot.Bot , ctx * ext.Context ) error {
559568 msg := ctx .EffectiveMessage
569+ if err := requireAdminOrPrivate (b , ctx , "Only admins can approve pull requests in this chat." ); err != nil {
570+ return err
571+ }
572+
560573 if msg .ReplyToMessage == nil {
561574 _ , err := msg .Reply (b , "Please use this command in reply to a notification." , nil )
562575 return err
@@ -598,6 +611,10 @@ func (h *CommandHandler) Approve(b *gotgbot.Bot, ctx *ext.Context) error {
598611
599612func (h * CommandHandler ) handleIssueAction (b * gotgbot.Bot , ctx * ext.Context , state string ) error {
600613 msg := ctx .EffectiveMessage
614+ if err := requireAdminOrPrivate (b , ctx , "Only admins can update issues or pull requests in this chat." ); err != nil {
615+ return err
616+ }
617+
601618 if msg .ReplyToMessage == nil {
602619 _ , err := msg .Reply (b , "Please use this command in reply to a notification." , nil )
603620 return err
0 commit comments