Browse Source

Add JIRARealm.HasWebhook and set it on webhook creation

pull/24/head
Kegan Dougal 8 years ago
parent
commit
e4b0121b31
  1. 2
      src/github.com/matrix-org/go-neb/realms/jira/jira.go
  2. 14
      src/github.com/matrix-org/go-neb/services/jira/webhook/webhook.go

2
src/github.com/matrix-org/go-neb/realms/jira/jira.go

@ -32,6 +32,7 @@ type JIRARealm struct {
ConsumerSecret string
PublicKeyPEM string // clobbered based on PrivateKeyPEM
PrivateKeyPEM string
HasWebhook bool // clobbered based on NEB
}
// JIRASession represents a single authentication session between a user and a JIRA endpoint.
@ -95,6 +96,7 @@ func (r *JIRARealm) Register() error {
if r.JIRAEndpoint == "" {
return errors.New("JIRAEndpoint must be specified")
}
r.HasWebhook = false // never let the user set this; only NEB can.
// Check to see if JIRA endpoint is valid by pinging an endpoint
cli, err := r.JIRAClient("", true)

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

@ -4,6 +4,7 @@ import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/andygrunwald/go-jira"
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/errors"
"github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/realms/jira"
@ -72,8 +73,11 @@ func RegisterHook(jrealm *realms.JIRARealm, projects []string, userID, webhookEn
// All projects that wish to be tracked are public, but the user cannot create
// webhooks. The only way this will work is if we already have a webhook for this
// JIRA endpoint.
// TODO: Check for an existing webhook for this realm (flag on realm?)
return fmt.Errorf("Not supported yet")
if !jrealm.HasWebhook {
logger.Print("No webhook exists for this realm.")
return fmt.Errorf("Not authorised to create webhook: not an admin.")
}
return nil
}
// The user is probably an admin (can query webhooks endpoint)
@ -115,7 +119,11 @@ func createWebhook(jrealm *realms.JIRARealm, webhookEndpointURL, userID string)
"realm_id": jrealm.ID(),
"jira_url": jrealm.JIRAEndpoint,
}).Print("Created webhook")
return nil
// mark this on the realm and persist it.
jrealm.HasWebhook = true
_, err = database.GetServiceDB().StoreAuthRealm(jrealm)
return err
}
func getWebhook(cli *jira.Client, webhookEndpointURL string) (*jiraWebhook, *errors.HTTPError) {

Loading…
Cancel
Save