Skip to content

Commit

Permalink
Merge master into prod, release: v1.1.28
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorbenei committed Sep 6, 2017
2 parents a758c1e + 2c88897 commit 0b5ec35
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bitrise*
gin-bin
.gows.user.yml
.idea
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ If the (commit) message includes `[skip ci]` or `[ci skip]` no build will be tri
* handled on the path: `/h/gogs/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`
* [Deveo](https://deveo.com)
* handled on the path: `/h/deveo/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`
* [Assembla](https://assembla.com)
* handled on the path: `/h/assembla/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`

### GitHub - setup & usage:

Expand Down Expand Up @@ -151,6 +153,25 @@ a [Deveo](https://deveo.com) *repository*.
That's all! The next time you __push code__ or __push a new tag__
a build will be triggered (if you have Trigger mapping defined for the event(s) on Bitrise).

### Assembla - setup & usage:

Follow these steps to add your `bitrise-webhooks` URL to your [Assembla](https://assembla.com) *space*.

1. Open your *space* on [assembla.com](https://assembla.com) or your organisation's assembla domain
2. Go to the `Webhooks` section of the space
3. Select `Create New Webhook`
4. Set `Title` to `BitRise Webhook`
5. Specify the `bitrise-webhooks` URL (`.../h/assembla/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`) in the `External url` field
6. Select `application/json` in the `Content type` field
7. Paste the following code to `Content`:
```json
{"assembla": {"space": "%{space}", "action": "%{action}", "object": "%{object}"}, "message": {"title": "%{title}", "body": "%{body}", "author": "%{author}"}, "git": {"repository_suffix": "%{repository_suffix}", "repository_url": "%{repository_url}", "branch": "%{branch}", "commit_id": "%{commit_id}"}}
```
8. Select `Code commits` in the `Post updates about:` section
9. Click `Add`

That's all! The next time you __push code__ a build will be triggered (if you have Trigger mapping defined for the event(s) on Bitrise).

### Slack - setup & usage:

You can register the `bitrise-webhooks` URL (`.../h/slack/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`) as either
Expand Down Expand Up @@ -401,3 +422,4 @@ response provider will be used.
* [Chad Robinson](https://github.com/crrobinson14) - `Gogs` support
* [Rafael Nobre](https://github.com/nobre84) - Environment variables support in `Slack` commands
* [Tuomas Peippo](https://github.com/tume)- Skip CI feature
* [Erik Poort](https://github.com/ErikMediaMonks) - `Assembla` support
153 changes: 153 additions & 0 deletions service/hook/assembla/assembla.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package assembla

//
// Docs: https://articles.assembla.com/assembla-basics/learn-more/post-information-to-external-systems-using-webhooks
//

import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/bitrise-io/bitrise-webhooks/bitriseapi"
hookCommon "github.com/bitrise-io/bitrise-webhooks/service/hook/common"
)

// --------------------------
// --- Webhook Data Model ---

// SpaceEventModel ...
type SpaceEventModel struct {
Space string `json:"space"`
Action string `json:"action"`
Object string `json:"object"`
}

// MessageEventModel ...
type MessageEventModel struct {
Title string `json:"title"`
Body string `json:"body"`
Author string `json:"author"`
}

// GitEventModel ...
type GitEventModel struct {
RepositorySuffix string `json:"repository_suffix"`
RepositoryURL string `json:"repository_url"`
Branch string `json:"branch"`
CommitID string `json:"commit_id"`
}

// PushEventModel ...
type PushEventModel struct {
SpaceEventModel SpaceEventModel `json:"assembla"`
MessageEventModel MessageEventModel `json:"message"`
GitEventModel GitEventModel `json:"git"`
}

// ---------------------------------------
// --- Webhook Provider Implementation ---

// HookProvider ...
type HookProvider struct{}

func detectContentType(header http.Header) (string, error) {
contentType := header.Get("Content-Type")
if contentType == "" {
return "", errors.New("No Content-Type Header found")
}

return contentType, nil
}

func detectAssemblaData(pushEvent PushEventModel) error {
if (pushEvent.GitEventModel.CommitID == "") ||
(pushEvent.GitEventModel.Branch == "") ||
(pushEvent.GitEventModel.RepositoryURL == "") ||
(pushEvent.GitEventModel.RepositorySuffix == "") {
return errors.New("Webhook is not correctly setup, make sure you post updates about 'Code commits' in Assembla")
}

if (pushEvent.GitEventModel.CommitID == "---") ||
(pushEvent.GitEventModel.Branch == "---") ||
(pushEvent.GitEventModel.RepositoryURL == "---") ||
(pushEvent.GitEventModel.RepositorySuffix == "---") {
return errors.New("Webhook is not correctly setup, make sure you post updates about 'Code commits' in Assembla")
}

return nil
}

func transformPushEvent(pushEvent PushEventModel) hookCommon.TransformResultModel {
if pushEvent.SpaceEventModel.Action != "committed" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Action was not 'committed', was: %s", pushEvent.SpaceEventModel.Action),
}
}
if pushEvent.MessageEventModel.Body == "" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Message body can't be empty"),
}
}
if pushEvent.MessageEventModel.Author == "" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Message author can't be empty"),
}
}
if pushEvent.GitEventModel.Branch == "" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Git branch can't be empty"),
}
}
if pushEvent.GitEventModel.CommitID == "" {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Git commit id can't be empty"),
}
}

triggerAPIParams := []bitriseapi.TriggerAPIParamsModel{
{
BuildParams: bitriseapi.BuildParamsModel{
CommitMessage: pushEvent.MessageEventModel.Body,
Branch: pushEvent.GitEventModel.Branch,
CommitHash: pushEvent.GitEventModel.CommitID,
},
TriggeredBy: "webhook",
},
}

return hookCommon.TransformResultModel{
TriggerAPIParams: triggerAPIParams,
}
}

// TransformRequest ...
func (hp HookProvider) TransformRequest(r *http.Request) hookCommon.TransformResultModel {
contentType, err := detectContentType(r.Header)
if err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Issue with Headers: %s", err),
}
}
if contentType != hookCommon.ContentTypeApplicationJSON {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Content-Type is not supported: %s", contentType),
}
}

if r.Body == nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to read content of request body: no or empty request body"),
}
}

var pushEvent PushEventModel
if err := json.NewDecoder(r.Body).Decode(&pushEvent); err != nil {
return hookCommon.TransformResultModel{
Error: fmt.Errorf("Failed to parse request body as JSON: %s", err),
}
}

return transformPushEvent(pushEvent)
}
Loading

0 comments on commit 0b5ec35

Please sign in to comment.