Permalink
Browse files

Refactor airship, only needs pygithub package to write

  • Loading branch information...
1 parent c0fd7bf commit eeefb722567bf9816f85c7d32bf47dee9406770a @richo committed Apr 3, 2013
View
4 airship/__init__.py
@@ -8,7 +8,7 @@
log = logger.getLogger(__name__)
# XXX We won't always be using the github adaptor!!
-from groundstation.protocols import github as github_protocol
+from groundstation.protocols.github.read_adaptor import GithubReadAdaptor
from groundstation.gref import Gref
import pygit2
@@ -53,7 +53,7 @@ def list_grefs(channel):
@app.route("/gref/<channel>/<path:identifier>")
def fetch_gref(channel, identifier):
- adaptor = github_protocol.GithubReadAdaptor(station, channel)
+ adaptor = GithubReadAdaptor(station, channel)
gref = Gref(station.store, channel, identifier)
log.info("Trying to fetch channel: %s identifier: %s" %
(channel, identifier))
View
126 groundstation/protocols/github/__init__.py
@@ -1,18 +1,8 @@
_version_ = "0.0.0"
_identifier_ = "richo@psych0tik.net:github:%s" % (_version_)
-import json
-import copy
-
-import github
-
from groundstation.gref import Gref
-import groundstation.objects.object_factory as object_factory
-
-from groundstation.objects.root_object import RootObject
-from groundstation.objects.update_object import UpdateObject
-
from groundstation.protocols import BaseProtocol
from groundstation import logger
@@ -31,119 +21,3 @@ def issue_gref(self, issue):
def _issue_id(self, issue):
return self.issue_format % (issue)
-
-
-class GithubReadAdaptor(AbstractGithubAdaptor):
- """GithubReadAdaptor(station, repo_name)
-
- Accepts a station and the name of a github repo, ie "richo/groundstation"
- """
- @property
- def repo_name(self):
- return self.repo
-
- def get_issue(self, issue):
- if isinstance(issue, Gref):
- gref = issue
- else:
- gref = self.issue_gref(issue)
-
- marshalled_gref = gref.marshall()
-
- assert len(marshalled_gref["roots"]) == 1, \
- "Anything other than one root node and you've got a problem"
-
- return marshalled_gref
-
-
-class GithubWriteAdaptor(AbstractGithubAdaptor):
- """GithubAdaptor(station, gh)
-
- Accepts a station and a github repo object (from PyGithub)
- """
- protocol = _identifier_
-
- @property
- def repo_name(self):
- return self.repo.full_name.replace("/", "_")
-
- 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, [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)
View
25 groundstation/protocols/github/read_adaptor.py
@@ -0,0 +1,25 @@
+from groundstation.protocols.github import _identifier_, AbstractGithubAdaptor
+from groundstation.gref import Gref
+
+
+class GithubReadAdaptor(AbstractGithubAdaptor):
+ """GithubReadAdaptor(station, repo_name)
+
+ Accepts a station and the name of a github repo, ie "richo/groundstation"
+ """
+ @property
+ def repo_name(self):
+ return self.repo
+
+ def get_issue(self, issue):
+ if isinstance(issue, Gref):
+ gref = issue
+ else:
+ gref = self.issue_gref(issue)
+
+ marshalled_gref = gref.marshall()
+
+ assert len(marshalled_gref["roots"]) == 1, \
+ "Anything other than one root node and you've got a problem"
+
+ return marshalled_gref
View
104 groundstation/protocols/github/write_adaptor.py
@@ -0,0 +1,104 @@
+import copy
+import json
+
+import github
+
+from groundstation.protocols.github import _identifier_, AbstractGithubAdaptor
+from groundstation.objects.root_object import RootObject
+from groundstation.objects.update_object import UpdateObject
+
+from groundstation import logger
+log = logger.getLogger(__name__)
+
+
+class GithubWriteAdaptor(AbstractGithubAdaptor):
+ """GithubAdaptor(station, gh)
+
+ Accepts a station and a github repo object (from PyGithub)
+ """
+ protocol = _identifier_
+
+ @property
+ def repo_name(self):
+ return self.repo.full_name.replace("/", "_")
+
+ 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, [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)

0 comments on commit eeefb72

Please sign in to comment.