From e1951b0bad43d31389c1a3cc9914fb54a4402235 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 3 May 2020 12:39:54 +0000 Subject: [PATCH] Convert posts to Commit type if they contain patches --- git.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------ posts.py | 5 +++++ 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/git.py b/git.py index cf8ee8193..3c5ff6817 100644 --- a/git.py +++ b/git.py @@ -42,7 +42,8 @@ def getGitProjectName(baseDir: str, nickname: str, domain: str, def isGitPatch(baseDir: str, nickname: str, domain: str, messageType: str, - subject: str, content: str) -> bool: + subject: str, content: str, + checkProjectName=True) -> bool: """Is the given post content a git patch? """ if messageType != 'Note' and \ @@ -57,6 +58,8 @@ def isGitPatch(baseDir: str, nickname: str, domain: str, return False if 'diff ' not in content: return False + if 'From ' not in content: + return False if 'From:' not in content: return False if 'Date:' not in content: @@ -66,11 +69,11 @@ def isGitPatch(baseDir: str, nickname: str, domain: str, if '
' not in content: if '
' not in content: return False - projectName = \ - getGitProjectName(baseDir, nickname, domain, - subject) - if not projectName: - return False + if checkProjectName: + projectName = \ + getGitProjectName(baseDir, nickname, domain, subject) + if not projectName: + return False return True @@ -88,6 +91,48 @@ def getGitHash(patchStr: str) -> str: return None +def convertPostToCommit(baseDir: str, nickname: str, domain: str, + postJsonObject: {}) -> bool: + """Detects whether the given post contains a patch + and if so then converts it to a Commit ActivityPub type + """ + if not postJsonObject.get('object'): + return False + if not isinstance(postJsonObject['object'], dict): + return False + if not postJsonObject['object'].get('type'): + return False + if postJsonObject['object']['type'] == 'Commit': + return True + if not postJsonObject['object'].get('summary'): + return False + if not postJsonObject['object'].get('content'): + return False + if not postJsonObject['object'].get('attributedTo'): + return False + if not isGitPatch(baseDir, nickname, domain, + postJsonObject['object']['type'], + postJsonObject['object']['summary'], + postJsonObject['object']['content'], + False): + return False + postJsonObject['object']['type'] = 'Commit' + # add a commitedBy parameter + if not postJsonObject['object'].get('committedBy'): + postJsonObject['object']['committedBy'] = \ + postJsonObject['object']['attributedTo'] + patchStr = gitFormatContent(postJsonObject['object']['content']) + commitHash = getGitHash(patchStr) + if commitHash: + postJsonObject['object']['hash'] = commitHash + postJsonObject['object']['description'] = { + "mediaType": "text/plain", + "content": patchStr + } + print('Converted post to Commit type') + return True + + def gitAddFromHandle(patchStr: str, handle: str) -> str: """Adds the activitypub handle of the sender to the patch """ diff --git a/posts.py b/posts.py index e64be8b86..ecebc8d29 100644 --- a/posts.py +++ b/posts.py @@ -49,6 +49,7 @@ from auth import createBasicAuthHeader from config import getConfigParam from blocking import isBlocked from filters import isFiltered +from git import convertPostToCommit # try: # from BeautifulSoup import BeautifulSoup # except ImportError: @@ -795,6 +796,10 @@ def createPostBase(baseDir: str, nickname: str, domain: str, port: int, modFile.write(newPostId + '\n') modFile.close() + # If a commit has been posted - i.e. the output from + # git format-patch - then convert the activitypub type + convertPostToCommit(baseDir, nickname, domain, newPost) + if schedulePost: if eventDate and eventTime: # add an item to the scheduled post index file