func (m *MessageHandler) prepareForStorage(o *blob.Blob) error { if o.Type != github.EvtPullRequest || o.HasAttribute(LabelsAttribute) { return nil } number := o.Data.Get("number").MustInt() log.Debugf("fetching labels for %s #%d", m.repo.PrettyName(), number) l, _, err := m.client.Issues.ListLabelsByIssue(m.repo.User, m.repo.Repo, number, &gh.ListOptions{}) if err != nil { return fmt.Errorf("retrieve labels for issue %d: %v", number, err) } // TODO This is terrible var b bytes.Buffer var d []interface{} if err := json.NewEncoder(&b).Encode(l); err != nil { return fmt.Errorf("serializing labels: %v", err) } if err := json.Unmarshal(b.Bytes(), &d); err != nil { return fmt.Errorf("unserializing labels: %v", err) } o.Push(LabelsAttribute, d) return nil }
// Index stores the blob into the specified storage under the provided id for // a given repository. func (b *simpleBlobStore) Store(storage Storage, repo *Repository, blob *blob.Blob) error { switch storage { // Live is an index containing the webhook events. In this particular case, // we use the delivery id as the document index. // // When storing a live event, we always update the next two indices. case StoreLiveEvent: liveIndex := repo.LiveIndexForTimestamp(blob.Timestamp) log.Debugf("store live event to %s/%s/%s", liveIndex, blob.Type, blob.ID) if err := b.indexer.Index(liveIndex, blob); err != nil { return fmt.Errorf("store live event %s data: %v", blob.ID, err) } // Before falling through, replace the blob with the snapshot data from // the event, if any. if blob = blob.Snapshot(); blob == nil { return nil } fallthrough // Current state is an index containing the last version of items at a // given moment in time, and is updated at a frequency configured by the // user. // // When storing a current state, we always update the next index. case StoreCurrentState: stateIndex := repo.StateIndexForTimestamp(blob.Timestamp) log.Debugf("store current state to %s/%s/%s", stateIndex, blob.Type, blob.ID) if err := b.indexer.Index(stateIndex, blob); err != nil { return fmt.Errorf("store current state %s data: %v", blob.ID, err) } fallthrough // Snapshot is an index containing the last version of all items, opened or // closed. case StoreSnapshot: log.Debugf("store snapshot to %s/%s/%s", repo.SnapshotIndex(), blob.Type, blob.ID) if err := b.indexer.Index(repo.SnapshotIndex(), blob); err != nil { return fmt.Errorf("store snapshot %s data: %v", blob.ID, err) } } return nil }