Example #1
0
func (p *Pivotal) Configure(req *http.Request) (helpers.ConfigureResponse, error) {
	cr := new(helpers.ConfigureRequest)
	if err := helpers.MapConfigureRequest(req, cr); err != nil {
		return nil, err
	}

	projectID := cr.Settings.GetString("project_id")
	if projectID == "" {
		return nil, ErrProjectIdNotValid
	}

	userToken := cr.UserToken
	if userToken == "" {
		return nil, ErrUserTokenIsNotValid
	}

	cp := &ConfigurePivotal{
		// prepare our endpoint
		WebhookURL:     prepareEndpoint(p.publicURL, PIVOTAL, cr.ServiceToken),
		WebhookVersion: "v5",
		WebhookID:      cr.Settings.GetString("webhook_id"),
	}

	createReq, err := p.sendRequest(cp, projectID, userToken)
	if err != nil {
		return nil, err
	}

	return createReq, nil

}
Example #2
0
// Configure is for supporting CRUD operations on repository webhooks
// in github. It decides what to do by comparing old and new settings
func (g Github) Configure(req *http.Request) (helpers.ConfigureResponse, error) {
	cr := new(helpers.ConfigureRequest)
	if err := helpers.MapConfigureRequest(req, cr); err != nil {
		return nil, err
	}

	repo := cr.Settings.GetString("repository")
	if repo == "" {
		return nil, ErrInvalidRepository
	}

	oldRepo := cr.OldSettings.GetString("repository")

	// configure same repository, therefore update webhook
	if repo == oldRepo {
		baseUrl := fmt.Sprintf("%s/repos/%s/hooks", g.ServerUrl, oldRepo)
		serviceId := cr.OldSettings.GetString("serviceId")
		if serviceId == "" {
			return nil, ErrServiceIdNotSet
		}
		url := fmt.Sprintf("%s/%s", baseUrl, serviceId)

		return g.configure(cr, "PATCH", url)
	}

	// now create the new webhook
	url := fmt.Sprintf("%s/repos/%s/hooks", g.ServerUrl, repo)

	createRes, err := g.configure(cr, "POST", url)
	if err != nil {
		return nil, err
	}

	// repository name has changed, therefore delete the previous webhook
	if oldRepo != "" {
		baseUrl := fmt.Sprintf("%s/repos/%s/hooks", g.ServerUrl, oldRepo)
		serviceId := cr.OldSettings.GetString("serviceId")
		if serviceId == "" {
			return nil, ErrServiceIdNotSet
		}

		url := fmt.Sprintf("%s/%s", baseUrl, serviceId)
		if _, err := g.configure(cr, "DELETE", url); err != nil {
			g.Log.Error("Could not delete the previous repository %s: %s", oldRepo, err)
		}
	}

	return createRes, nil
}