Skip to content

Commit ecca0b1

Browse files
committed
Show latest updates even without access_token
1 parent dbbfdaf commit ecca0b1

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

bot.py

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
SOFTWARE.
2323
'''
2424

25-
__version__ = '1.4.1'
25+
__version__ = '1.4.2'
2626

2727
from contextlib import redirect_stdout
2828
from urllib.parse import urlparse
@@ -122,7 +122,6 @@ async def on_ready(self):
122122
---------------
123123
'''))
124124

125-
126125
async def on_message(self, message):
127126
if message.author.bot:
128127
return
@@ -326,43 +325,47 @@ async def update(self, ctx):
326325
if access_token:
327326
user = await Github.login(self, access_token)
328327
em.set_author(name=user.username, icon_url=user.avatar_url, url=user.url)
329-
return await ctx.send(embed=em)
328+
329+
latest_commits = ''
330330

331-
if not access_token:
332-
em.title = 'Invalid Access Token'
333-
em.description = 'You have not properly set up GitHub credentials. '\
334-
'Create a config variable named `GITHUB_ACCESS_TOKEN`'\
335-
' and set the value as your personal access token which'\
336-
' can be generated in your GitHub account\'s [developer '\
337-
'settings](https://github.com/settings/tokens).'
331+
async for commit in Github(self).get_latest_commits():
338332

339-
em.color = discord.Color.red()
340-
return await ctx.send(embed=em)
341-
342-
user = await Github.login(self, access_token)
343-
resp = await user.request(user.head)
344-
sha, commit_url = resp['object']['sha'], resp['object']['url']
345-
data = await user.update_repository(sha)
333+
short_sha = commit['sha'][:6]
334+
html_url = commit['html_url']
335+
message = commit['commit']['message']
336+
author_name = commit['author']['login']
337+
author_url = commit['author']['html_url']
346338

347-
latest_update = await user.request(commit_url)
339+
latest_commits += f'[`{short_sha}`]({html_url}) {message} - [`{author_name}`]({author_url})\n'
348340

349-
em.title = 'Success'
350-
em.set_author(name=user.username, icon_url=user.avatar_url, url=user.url)
351-
352-
if data:
353-
em.description = 'Bot successfully updated, the bot will restart momentarily'
354-
message = data['commit']['message']
355-
html_url = data["html_url"]
356-
short_sha = data['sha'][:6]
357-
em.add_field(name='Merge Commit', value=f'[`{short_sha}`]({html_url}) - {message}')
358-
else:
359-
em.description = 'Already up to date with master repository.'
360-
361341

362-
short_sha = latest_update['sha'][:6]
363-
html_url = latest_update['html_url']
364-
message = latest_update['message']
365-
em.add_field(name='Latest Commit', value=f'[`{short_sha}`]({html_url}) - {message}', inline=False)
342+
if data['latest_version'] != __version__:
343+
if not access_token:
344+
em.title = 'Invalid Access Token'
345+
em.description = 'You have not properly set up GitHub credentials. '\
346+
'Create a config variable named `GITHUB_ACCESS_TOKEN`'\
347+
' and set the value as your personal access token which'\
348+
' can be generated in your GitHub account\'s [developer '\
349+
'settings](https://github.com/settings/tokens).'
350+
351+
em.color = discord.Color.red()
352+
return await ctx.send(embed=em)
353+
user = await Github.login(self, access_token)
354+
data = await user.update_repository()
355+
356+
em.title = 'Success'
357+
em.set_author(name=user.username, icon_url=user.avatar_url, url=user.url)
358+
359+
if data:
360+
em.description = 'Bot successfully updated, the bot will restart momentarily'
361+
message = data['commit']['message']
362+
html_url = data["html_url"]
363+
short_sha = data['sha'][:6]
364+
em.add_field(name='Merge Commit', value=f'[`{short_sha}`]({html_url}) {message} - [`{user.name}`]({user.url})')
365+
else:
366+
em.description = 'Already up to date with master repository.'
367+
368+
em.add_field(name='Latest Updates', value=latest_commits, inline=False)
366369

367370
await ctx.send(embed=em)
368371

utils/github.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
class Github:
22
head = 'https://api.github.com/repos/kyb3r/modmail/git/refs/heads/master'
33
merge_url = 'https://api.github.com/repos/{username}/modmail/merges'
4+
commit_url = 'https://api.github.com/repos/kyb3r/modmail/commits'
45

5-
def __init__(self, bot, access_token, username=None):
6+
def __init__(self, bot, access_token=None, username=None):
67
self.bot = bot
78
self.session = bot.session
89
self.access_token = access_token
910
self.username = username
1011
self.avatar_url = None
1112
self.url = None
12-
self.headers = {'Authorization': 'Bearer '+access_token}
13+
self.headers = None
14+
if self.access_token:
15+
self.headers = {'Authorization': 'Bearer '+ str(access_token)}
16+
17+
async def get_latest_commits(self, limit=3):
18+
resp = await self.request(self.commit_url)
19+
print(resp)
20+
for index in range(limit):
21+
yield resp[index]
1322

1423
async def update_repository(self, sha=None):
1524
if sha is None:
@@ -21,7 +30,7 @@ async def update_repository(self, sha=None):
2130
'head': sha,
2231
'commit_message': 'Updating bot'
2332
}
24-
33+
2534
merge_url = self.merge_url.format(username=self.username)
2635

2736
resp = await self.request(merge_url, method='POST', payload=payload)

0 commit comments

Comments
 (0)