def handle_describechannels_for_missing_tip(self): test_id = u"tests/1" test_channel = u"test_channel" test_protocol = u"test_protocol" gref = Gref(self.station.station.store, test_channel, test_id) root_obj = RootObject(test_id, test_channel, test_protocol) root_oid = self.station.station.write(root_obj.as_object()) current_oid = [root_oid] for i in xrange(10): update_obj = UpdateObject([current_oid.pop()], "loldata") current_oid.append(self.station.station.write(update_obj.as_object())) update_obj = UpdateObject([current_oid.pop()], "loldata") current_oid.append(binascii.hexlify(pygit2.hash(update_obj.as_object()))) self.station.station.update_gref(gref, [root_oid]) self.assertEqual([root_oid], gref.tips()) current_oid = current_oid.pop() self.station.payload = _payload(update_obj, gref, [current_oid]) handle_describechannels(self.station) self.assertEqual([current_oid], gref.tips())
def handle_describechannels_for_missing_tip(self): test_id = u"tests/1" test_channel = u"test_channel" test_protocol = u"test_protocol" gref = Gref(self.station.station.store, test_channel, test_id) root_obj = RootObject(test_id, test_channel, test_protocol) root_oid = self.station.station.write(root_obj.as_object()) current_oid = [root_oid] for i in xrange(10): update_obj = UpdateObject([current_oid.pop()], "loldata") current_oid.append(self.station.station.write(update_obj.as_object())) update_obj = UpdateObject([current_oid.pop()], "loldata") current_oid.append(binascii.hexlify(pygit2.hash(update_obj.as_object()))) self.station.station.update_gref(gref, [Tip(root_oid, "")]) self.assertEqual([root_oid], gref.tips()) current_oid = current_oid.pop() self.station.payload = _payload(update_obj, gref, [current_oid]) handle_describechannels(self.station) self.assertEqual([current_oid], gref.tips())
def test_hydrate_root_object(self): root = RootObject( "test_object", "[email protected]:groundstation/tests", "[email protected]:groundstation/testcase" ) hydrated_root = object_factory.hydrate_object(root.as_object()) self.assertTrue(isinstance(hydrated_root, RootObject))
def hydrate_object(protobuf): # Test if it's strongly typed first _type = type_of(protobuf) if _type == TYPE_ROOT: return RootObject.from_object(protobuf) elif _type == TYPE_UPDATE: return UpdateObject.from_object(protobuf) elif _type == TYPE_UNSET: # Use existing heuristic obj = RootObject.from_object(protobuf) if not obj.protocol: obj = UpdateObject.from_object(protobuf) return obj else: raise Exception("Unknown type; %s" % (str(_type)))
def create_gref(channel): def _write_object(obj): return station.write(obj.as_object()) name = request.form["name"] protocol = request.form["protocol"] user = request.form["user"] body = request.form["body"] title = request.form["title"] gref = Gref(station.store, channel, name) root = RootObject(name, channel, protocol) root_oid = _write_object(root) _title = UpdateObject([root_oid], json.dumps({ "type": "title", "id": None, "body": title, "user": user })) title_oid = _write_object(_title) _body = UpdateObject([title_oid], json.dumps({ "type": "body", "id": None, "body": body })) body_oid = _write_object(_body) _update_gref(gref, [Tip(body_oid, "")], []) return ""
def test_handle_listallchannels(self): test_id = u"tests/1" test_channel = u"test_channel" test_protocol = u"test_protocol" obj = RootObject(test_id, test_channel, test_protocol) gref = Gref(self.station.station.store, test_channel, test_id) oid = self.station.station.write(obj.as_object()) self.station.station.update_gref(gref, [Tip(oid, "")], []) handle_listallchannels(self.station) response = self.station.stream.pop() serialized_response = response.SerializeToString() self.assertIsInstance(serialized_response, str) assert len(self.station.stream) == 0, "Someone is leaving objects lyind around" channel_description = ChannelList() channel_description.ParseFromString(response.payload) self.assertEqual(channel_description.channels[0].channelname, test_channel) self.assertEqual(channel_description.channels[0].grefs[0].identifier, test_id) self.assertEqual(channel_description.channels[0].grefs[0].tips[0].tip, oid)
def write_issue(self, issue): parents = [] issue_id = self._issue_id(issue) gref = self.issue_gref(issue) def _write_new_tip(obj): our_parents = [] while parents: our_parents.append(parents.pop()) log.debug("Creating new object with parents: %s" % (str(our_parents))) oid = self.station.write(obj.as_object()) self.station.update_gref(gref, [Tip(oid, "")], our_parents) parents.append(oid) log.debug("Setting parents to: %s" % (str(parents))) def _parents(): return copy.copy(parents) if gref.exists(): log.info("Not creating any objects, a gref already exists at: %s" % str(gref)) return False log.info(("Creating a new root_object with:\n" + "id: %s\n" + "channel: %s\n" + "protocol: %s") % (issue_id, self.channel, self.protocol)) root_object = RootObject(issue_id, self.channel, self.protocol) _write_new_tip(root_object) # Write out the initial state # Creating lots of tiny objects should make deduping easier later if issue.fields.reporter: reporter = issue.fields.reporter.name else: reporter = "Anonymous" title_payload = { "type": "title", "id": None, "body": issue.fields.summary, "user": reporter } update_object = UpdateObject(_parents(), json.dumps(title_payload)) _write_new_tip(update_object) # Write out the body of the issue body_payload = { "type": "body", "id": None, "body": issue.fields.description } update_object = UpdateObject(_parents(), json.dumps(body_payload)) _write_new_tip(update_object) everything = [] for comment in issue.fields.comment.comments: comment.type = "comment" everything.append(comment) for history in issue.changelog.histories: history.type = "event" everything.append(history) everything.sort(key=lambda x: x.created) for item in everything: if item.type == "comment": payload = { "type": "comment", "id": int(item.id), "body": item.body, "user": item.author.name } elif item.type == "event": stateChange = ", ".join([ "%s from %s to %s" % (x.field, x.fromString, x.toString) for x in item.items ]) payload = { "type": "event", "id": item.id, "state": stateChange, "user": item.author.name } else: raise Exception("Unhandled type") update_object = UpdateObject(_parents(), json.dumps(payload)) _write_new_tip(update_object)
def write_issue(self, issue): # Stupid implementation, blindly write with no deduping or merge # resolution. parents = [] issue_id = self._issue_id(issue.number) gref = self.issue_gref(issue.number) def _write_new_tip(obj): our_parents = [] while parents: our_parents.append(parents.pop()) log.debug("Creating new object with parents: %s" % (str(our_parents))) oid = self.station.write(obj.as_object()) self.station.update_gref(gref, [Tip(oid, "")], our_parents) parents.append(oid) log.debug("Setting parents to: %s" % (str(parents))) def _parents(): return copy.copy(parents) # Bail out if we've already written: if gref.exists(): log.info("Not creating any objects, a gref already exists at: %s" % str(gref)) return False # Write out a root object log.info(("Creating a new root_object with:\n" + "id: %s\n" + "channel: %s\n" + "protocol: %s") % (issue_id, self.channel, self.protocol)) root_object = RootObject(issue_id, self.channel, self.protocol) _write_new_tip(root_object) # Write out the initial state # Creating lots of tiny objects should make deduping easier later title_payload = { "type": "title", "id": None, "body": issue.title, "user": issue.user.login } update_object = UpdateObject(_parents(), json.dumps(title_payload)) _write_new_tip(update_object) # Write out the body of the issue body_payload = {"type": "body", "id": None, "body": issue.body} update_object = UpdateObject(_parents(), json.dumps(body_payload)) _write_new_tip(update_object) # Write out all of the comments and events everything = [] everything.extend(issue.get_comments()) everything.extend(issue.get_events()) everything.sort(key=lambda x: x.created_at) for item in everything: if isinstance(item, github.IssueComment.IssueComment): payload = { "type": "comment", "id": item.id, "body": item.body, "user": item.user.login } elif isinstance(item, github.IssueEvent.IssueEvent): payload = { "type": "event", "id": item.id, "state": item.event, "user": item.actor.login } else: raise Exception("Unhandled item %s" % (repr(item))) update_object = UpdateObject(_parents(), json.dumps(payload)) _write_new_tip(update_object)
def hydrate_object(protobuf): obj = RootObject.from_object(protobuf) if not obj.protocol: obj = UpdateObject.from_object(protobuf) return obj
# Creates a thread in the current groundstation context import uuid import stricken from groundstation.node import Node from groundstation.station import Station from groundstation.objects.root_object import RootObject from groundstation.objects.update_object import UpdateObject from groundstation.gref import Gref node = Node() station = Station.from_env(node) CHANNEL = "messages" thread_id = str(uuid.uuid1()) gref = Gref(station.store, CHANNEL, thread_id) root = RootObject(thread_id, CHANNEL, stricken.PROTOCOL) oid = station.write(root.as_object()) print("Root id: %s" % (oid)) update = UpdateObject([oid], "Post content") oid = station.write(update.as_object()) print("Update id: %s" % (oid)) gref.write_tip(oid, "")
def create_root_object(self, gref): return RootObject(gref.identifier, gref.channel, "test_protocol")
def root_object(id, channel, protocol): return RootObject(id, channel, protocol)
def test_hydrate_root_object(self): root = RootObject("test_object", "[email protected]:groundstation/tests", "[email protected]:groundstation/testcase") hydrated_root = object_factory.hydrate_object(root.as_object()) self.assertTrue(isinstance(hydrated_root, RootObject))