- def accept_merge(self, prq, lib):
- doc = lib.document( prq.document )
- udoc = doc.take( prq.comitter.username )
- success, changed = udoc.share(prq.comment)
-
- if not success:
- return EntityConflict().django_response()
-
- doc = doc.latest()
-
- prq.status = 'A'
- prq.merged_revisions = unicode(doc.revision)
- prq.save()
-
- return SuccessAllOk().django_response({
- 'status': prq.status
- })
-
+ def accept_merge(self, user, prq, lib):
+ if prq.status not in ['N', 'E']:
+ return BadRequest().django_response({
+ 'reason': 'invalid-state',
+ 'message': "This pull request is alredy resolved. Can't accept."
+ })
+
+ src_doc = lib.document_for_rev( prq.source_revision )
+
+ lock = lib.lock()
+ try:
+ if not src_doc.up_to_date():
+ # This revision is no longer up to date, thus
+ # it needs to be updated, before push:
+ #
+ # Q: where to put the updated revision ?
+ # A: create a special user branch named prq-#prqid
+ prq_doc = src_doc.take("$prq-%d" % prq.id)
+
+ # This could be not the first time we try this,
+ # so the prq_doc could already be there
+ # and up to date
+
+ try:
+ prq_doc = prq_doc.update(user.username)
+ prq.source_revision = str(prq_doc.revision)
+ src_doc = prq_doc
+ except UpdateException, e:
+ # this can happen only if the merge program
+ # is misconfigured - try returning an entity conflict
+ # TODO: put some useful infor here
+ prq.status = 'E'
+ prq.save()
+ return EntityConflict().django_response({
+ 'reason': 'update-failed',
+ 'message': e.message })
+
+ # check if there are conflicts
+ if src_doc.has_conflict_marks():
+ prq.status = 'E'
+ prq.save()
+ # Now, the user must resolve the conflict
+ return EntityConflict().django_response({
+ "reason": "unresolved-conflicts",
+ "message": "There are conflict in the document. Resolve the conflicts retry accepting."
+ })
+
+ # So, we have an up-to-date, non-conflicting document
+ changed = src_doc.share(prq.comment)
+
+ if not changed:
+ # this is actually very bad, but not critical
+ log.error("Unsynched pull request: %d" % prq.id)
+
+ # sync state with repository
+ prq.status = 'A'
+ prq.merged_revision = str(src_doc.shared().revision)
+ prq.merged_timestamp = datetime.now()
+ prq.save()
+
+ return SuccessAllOk().django_response({
+ 'status': prq.status,
+ 'merged_into': prq.merged_revision,
+ 'merged_at': prq.merged_timestamp
+ })
+ finally:
+ lock.release()