1

I am creating a bot with Spark (chat for enterprise), in Python, I use PyGitHub for the librairy. So when I write "repos" in my room with the bot he has to send me back the list of my repos. It works fine with my github personnal account but not with my professionnal account. If you can explain me why ? here my code:

def gitTest(self, details, message):
        url = "https://enter-prise.com"
        token = "abcd"
        github = Github(token, base_url=url)
        for repo in github.get_organization("org").get_repos():
            self.answer(details.roomId, markdown=repo.name) 


Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/main.py", line 44, in Main
    bot.isRunnable()
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/utils/Compute.py", line 47, in isRunnable
    self.spark(message[0], message[1])
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/testbotforgithub.py", line 33, in spark
    return self.answer(details.roomId, markdown=self.gitTest(details, message))
  File "/home/mflamant/Documents/bot/CiscoSparkPython/testbotforgithub/testbotforgithub.py", line 56, in gitTest
    for repo in github.get_organization(adt).get_repos():
  File "/usr/local/lib/python2.7/dist-packages/PyGithub-1.35-py2.7.egg/github/Organization.py", line 539, in get_repos
    self.url + "/repos",
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

Can you explain me what is wrong with my code ? thank you

wpercy
  • 9,636
  • 4
  • 33
  • 45
Kravennagen
  • 77
  • 10
  • `self.url` is obviously `None`. – Jared Smith Oct 17 '17 at 16:09
  • Yes, but I don't understand what it means and how to change it ? – Kravennagen Oct 17 '17 at 16:12
  • It means that you are trying to add two things that you can't meaningfully add together. Think back to primary school math class. What would you tell the teacher if he/she asked you to add the number 5 to chair? You would probably say the question makes no sense, and you'd be correct. Python is telling you that `self.url` is `None`, it's not there, doesn't exist, and that you're asking it to the equivalent of adding 5 to chair. You need to create it. See @wpercy's answer. – Jared Smith Oct 17 '17 at 16:18
  • Yeah I don't think that it was the concatenation that confused him - it was assigning to the attribute versus the local variable. – wpercy Oct 17 '17 at 16:33
  • I made differents tests, I change my post, I really think my problem come from the base_url. And right now I am writing it in the github(login, pass, base_url) not from a variable. – Kravennagen Oct 19 '17 at 13:16
  • @Kravennagen we solved your first problem by informing you that you need to assign to self.url. If you have another error, you should ask another question, not destroy this one. – wpercy Oct 19 '17 at 14:37
  • This is not a different error, I am trying to do exactly the same, that's why I don't create a new one, I just change my code and maybe I give a better explaination right now. Because the self.variable doesn't works. Thank you. – Kravennagen Oct 19 '17 at 14:49
  • Ok ! I found out ! I had to add "/api/v3" to the end of my URL and now it works, thank you everyone ! – Kravennagen Oct 19 '17 at 15:01

1 Answers1

2

If gitTest is an instance method, you need to assign to the attribute self.url, not just to the local variable url. So your method should probably look like this:

def gitTest(self, details, message):
    self.url = "https://enter-prise.com"
    self.token = "abcd"
    github = Github(token, base_url=url)
    for repo in github.get_organization("org").get_repos():
        self.answer(details.roomId, markdown=repo.name) 

This is why you pass in the reference to self as the first argument of any instance method.

wpercy
  • 9,636
  • 4
  • 33
  • 45
  • I'm sorry but it doesn't works for me...I decided to test with the github= Github("abcd", "https://enter-prise.com") directly, but again it doesn't works I can't get the repos from my github enterprise... – Kravennagen Oct 19 '17 at 12:16