Commit e6e859ec authored by Kegsay's avatar Kegsay Committed by GitHub
Browse files

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

Delete webhook services when there are no webhooks left
parents 0275069c 26789050
......@@ -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) {
......
......@@ -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
......
......@@ -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 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment