-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package proxyservice | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"encoding/json" | ||
|
||
"github.com/streadway/amqp" | ||
"github.com/taskcluster/pulse-go/pulse" | ||
) | ||
|
||
|
||
// https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/notifications.html#pulse-notifications | ||
type HgMessage struct { | ||
Type string | ||
Data interface{} | ||
} | ||
|
||
type changegroupMessage struct { | ||
RepoUrl string `json:"repo_url"` | ||
Heads []string `json:"heads"` | ||
PushlogPushes []struct{ | ||
pushid int | ||
time int | ||
user string | ||
push_json_url string | ||
push_full_json_url string | ||
} `json:"pushlog_pushes"` | ||
Source string `json:"Source"` | ||
} | ||
|
||
|
||
func (msg *HgMessage) UnmarshalJSON(b []byte) error { | ||
var raw struct { | ||
Type string | ||
Data json.RawMessage | ||
} | ||
if err := json.Unmarshal(b, &raw); err != nil { | ||
return err | ||
} | ||
msg.Type = raw.Type | ||
if raw.Type == "changegroup.1" { | ||
var data changegroupMessage | ||
if err := json.Unmarshal(b, &data); err != nil { | ||
return err | ||
} | ||
msg.Data = data | ||
return nil | ||
} | ||
return fmt.Errorf("Unknown hg message type %s", msg.Type) | ||
} | ||
|
||
type hgPushBinding struct { | ||
Repository string | ||
} | ||
|
||
func (binding hgPushBinding) RoutingKey() string { | ||
return fmt.Sprintf("route.%s", binding.Repository) | ||
} | ||
|
||
func (binding hgPushBinding) ExchangeName() string { | ||
return "exchange/hgpushes/v2" | ||
} | ||
|
||
func (binding hgPushBinding) NewPayloadObject() interface{} { | ||
return new(HgMessage) | ||
} | ||
|
||
type HgmoPulseHandler struct { | ||
Jenkins *Jenkins | ||
Pulse *pulse.Connection | ||
QueueName string | ||
ValidHgRepos map[string]bool | ||
} | ||
|
||
func NewHgmoPulseHandler(jenkins *Jenkins, pulse *pulse.Connection, queueName string, hgRepos ...string) *HgmoPulseHandler { | ||
validHgRepos := make(map[string]bool) | ||
for _, hgRepo := range hgRepos { | ||
validHgRepos[hgRepo] = true | ||
} | ||
return &HgmoPulseHandler{ | ||
Jenkins: jenkins, | ||
Pulse: pulse, | ||
QueueName: queueName, | ||
ValidHgRepos: validHgRepos, | ||
} | ||
} | ||
|
||
func (handler *HgmoPulseHandler) handleMessage(message interface{}, delivery amqp.Delivery) { | ||
switch t := message.(type) { | ||
case *HgMessage: | ||
switch data := t.Data.(type) { | ||
case *changegroupMessage: | ||
if len(data.Heads) != 1 { | ||
log.Printf("Message %s has %d heads, only 1 supported", t, len(data.Heads)) | ||
} | ||
if len(data.PushlogPushes) != 1 { | ||
log.Printf("Message %s has %d pushlog pushes, only 1 supported", t, len(data.PushlogPushes)) | ||
} | ||
if err := handler.Jenkins.TriggerHgJob(data.RepoUrl, data.Heads[0], t); err != nil { | ||
log.Printf("Error triggering hg.mozilla.org job: %s", err) | ||
} | ||
} | ||
} | ||
delivery.Ack(false) // acknowledge message *after* processing | ||
} | ||
|
||
func (handler *HgmoPulseHandler) Consume() error { | ||
bindings := make([]pulse.Binding, 0) | ||
for validHgRepo := range handler.ValidHgRepos { | ||
bindings = append(bindings, hgPushBinding{Repository: validHgRepo}) | ||
} | ||
_, err := handler.Pulse.Consume( | ||
handler.QueueName, | ||
handler.handleMessage, | ||
1, // prefetch 1 message at a time | ||
false, // don't autoacknowledge messages | ||
bindings... | ||
) | ||
return err | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters