Skip to content

Commit

Permalink
webhook: rm global sopel_instance; pass bot if bot parameter present
Browse files Browse the repository at this point in the history
Functions that have a `bot` parameter (has to be called `bot`, it is not
a matter of position, but name) will receive the managing Sopel instance
as an argument. This precludes the use of `global sopel_instance`.
  • Loading branch information
HumorBaby committed Apr 23, 2019
1 parent 2b93aaf commit c3a4409
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions sopel_modules/github/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@
else:
getargspec_ = inspect.getfullargspec

# Because I'm a horrible person
sopel_instance = None

def setup_webhook(sopel):
global sopel_instance
sopel_instance = sopel
host = sopel.config.github.webhook_host
port = sopel.config.github.webhook_port

Expand Down Expand Up @@ -86,8 +82,6 @@ def create_table(bot, c):


def shutdown_webhook(sopel):
global sopel_instance
sopel_instance = None
if sopel.memory.contains('gh_webhook_server'):
print('Stopping webhook server')
sopel.memory['gh_webhook_server'].stop()
Expand Down Expand Up @@ -154,8 +148,8 @@ def stop(self):
self.server.shutdown()


def get_targets(repo):
conn = sopel_instance.db.connect()
def get_targets(bot, repo):
conn = bot.db.connect()
c = conn.cursor()
c.execute('SELECT * FROM gh_hooks WHERE repo_name = ? AND enabled = 1', (repo.lower(), ))
return c.fetchall()
Expand All @@ -167,7 +161,7 @@ def show_hook_info():


@bottle.post("/webhook")
def webhook():
def webhook(bot):
event = bottle.request.headers.get('X-GitHub-Event') or 'ping'

try:
Expand All @@ -176,37 +170,37 @@ def webhook():
return bottle.abort(400, 'Something went wrong!')

if event == 'ping':
channels = get_targets(payload['repository']['full_name'])
channels = get_targets(bot, payload['repository']['full_name'])
for chan in channels:
sopel_instance.msg(chan[0], '[{}] {}: {} (Your webhook is now enabled)'.format(
bot.msg(chan[0], '[{}] {}: {} (Your webhook is now enabled)'.format(
fmt_repo(payload['repository']['name'], chan),
fmt_name(payload['sender']['login'], chan),
payload['zen']))
return '{"channels":' + json.dumps([chan[0] for chan in channels]) + '}'

payload['event'] = event

targets = get_targets(payload['repository']['full_name'])
targets = get_targets(bot, payload['repository']['full_name'])

for row in targets:
messages = get_formatted_response(payload, row)
# Write the formatted message(s) to the channel
for message in messages:
sopel_instance.msg(row[0], message)
bot.msg(row[0], message)

return '{"channels":' + json.dumps([chan[0] for chan in targets]) + '}'


@bottle.get('/auth')
def handle_auth_response():
def handle_auth_response(bot):
code = bottle.request.query.code
state = bottle.request.query.state

repo = state.split(':')[0]
channel = state.split(':')[1]

data = {'client_id': sopel_instance.config.github.client_id,
'client_secret': sopel_instance.config.github.secret,
data = {'client_id': bot.config.github.client_id,
'client_secret': bot.config.github.secret,
'code': code}
raw = requests.post('https://github.com/login/oauth/access_token', data=data, headers={'Accept': 'application/json'})
try:
Expand All @@ -224,7 +218,7 @@ def handle_auth_response():
"active": True,
"events": ["*"],
"config": {
"url": sopel_instance.config.github.external_url,
"url": bot.config.github.external_url,
"content_type": "json"
}
}
Expand Down

0 comments on commit c3a4409

Please sign in to comment.