Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use body as empty string when GitHub API responds with None #113

Merged
merged 1 commit into from Sep 14, 2021
Merged

Use body as empty string when GitHub API responds with None #113

merged 1 commit into from Sep 14, 2021

Conversation

ghost
Copy link

@ghost ghost commented Sep 9, 2021

If there is nothing in the body of a PR, GitHub API responds with None. Added an if statement to use body as empty string if response is None.

Fixes #111

Can test this by creating a PR in some repository with no body or use this PR: https://github.com/prayank23/bitcoin/pull/6

Python code to test
import codecs
import unicodedata
import re
import json
from urllib.request import Request, urlopen

def get_response(req_url, ghtoken):
    req = Request(req_url)
    if ghtoken is not None:
        req.add_header('Authorization', 'token ' + ghtoken)
    return urlopen(req)

def sanitize(s, newlines=False):

    return ''.join(ch for ch in s if unicodedata.category(ch)[0] != "C" or (ch == '\n' and newlines))


def sanitize_ghdata(rec):

    if 'title' in rec:
        rec['title'] = sanitize(rec['title'], newlines=False)
    if rec['body'] is None:
        rec['body'] = ''
    rec['body'] = sanitize(rec['body'], newlines=True)

    if not re.match('[a-zA-Z0-9-]+\Z', rec['user']['login'], re.DOTALL):
        raise ValueError('Github username contains invalid characters: {}'.format(sanitize(rec['user']['login'])))

    return rec

def main():
    reader = codecs.getreader('utf-8')
    req_url = "https://api.github.com/repos/prayank23/bitcoin/pulls/6"
    ghtoken = "YOURGITHUBTOKEN"

    response = sanitize_ghdata(json.load(reader(get_response(req_url, ghtoken))))
    print(response)

if __name__ == '__main__':
    main()

@maflcko
Copy link
Contributor

maflcko commented Sep 9, 2021

cr ACK, didn't test

@laanwj
Copy link
Member

laanwj commented Sep 14, 2021

LGTM ACK

@laanwj laanwj merged commit 7a4fad3 into bitcoin-core:master Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

github-merge.py cannot merge PRs with empty body
3 participants