Browse Source

Merge pull request #62 from matrix-org/kegan/gh-no-webhooks-means-no-service

Delete webhook services when there are no webhooks left
kegan/gh-logout 0.1.0
Kegsay 8 years ago
committed by GitHub
parent
commit
e6e859ec48
  1. 8
      src/github.com/matrix-org/go-neb/database/db.go
  2. 9
      src/github.com/matrix-org/go-neb/database/schema.go
  3. 14
      src/github.com/matrix-org/go-neb/services/github/github_webhook.go

8
src/github.com/matrix-org/go-neb/database/db.go

@ -104,6 +104,14 @@ func (d *ServiceDB) LoadService(serviceID string) (service types.Service, err er
return
}
// DeleteService deletes the given service from the database.
func (d *ServiceDB) DeleteService(serviceID string) (err error) {
err = runTransaction(d.db, func(txn *sql.Tx) error {
return deleteServiceTxn(txn, serviceID)
})
return
}
// LoadServicesForUser loads all the bot services configured for a given user.
// Returns an empty list if there aren't any services configured.
func (d *ServiceDB) LoadServicesForUser(serviceUserID string) (services []types.Service, err error) {

9
src/github.com/matrix-org/go-neb/database/schema.go

@ -231,6 +231,15 @@ func selectServicesForUserTxn(txn *sql.Tx, userID string) (srvs []types.Service,
return
}
const deleteServiceSQL = `
DELETE FROM services WHERE service_id = $1
`
func deleteServiceTxn(txn *sql.Tx, serviceID string) error {
_, err := txn.Exec(deleteServiceSQL, serviceID)
return err
}
const insertRealmSQL = `
INSERT INTO auth_realms(
realm_id, realm_type, realm_json, time_added_ms, time_updated_ms

14
src/github.com/matrix-org/go-neb/services/github/github_webhook.go

@ -192,6 +192,20 @@ func (s *githubWebhookService) PostRegister(oldService types.Service) {
}).Warn("Failed to remove webhook")
}
}
// If we are not tracking any repos any more then we are back to square 1 and not doing anything
// so remove ourselves from the database. This is safe because this is still within the critical
// section for this service.
if len(newRepos) == 0 {
logger := log.WithFields(log.Fields{
"service_type": s.ServiceType(),
"service_id": s.ServiceID(),
})
logger.Info("Removing service as no webhooks are registered.")
if err := database.GetServiceDB().DeleteService(s.ServiceID()); err != nil {
logger.WithError(err).Error("Failed to delete service")
}
}
}
func (s *githubWebhookService) joinWebhookRooms(client *matrix.Client) error {

Loading…
Cancel
Save