Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fran committed Apr 11, 2017
0 parents commit dedefc1
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local.py
__pycache__/
*.py[cod]
*$py.class
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Francisco Ortiz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# github-slackbot
Slack bot which sends notifications with organization's PR older than a number of days
7 changes: 7 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

SLACK_CHANNEL = os.environ.get('SLACK_CHANNEL', None)
SLACK_TOKEN = os.environ.get('SLACK_TOKEN', None)
GITHUB_ORGANIZATION = os.environ.get('GITHUB_ORGANIZATION', None)
GITHUB_USERNAME = os.environ.get('GITHUB_USERNAME', None)
GITHUB_PASSWORD = os.environ.get('GITHUB_PASSWORD', None)
Empty file added githubslackbot/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions githubslackbot/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import datetime

from github import Github


class GitHubIntegration:
def __init__(self, username, password):
self.username = username
self.password = password
# self.github_service = github_service

def get_org_pull_requests_older_than_num_days(self, organization_name, num_days):
client = self._get_client()
organization = client.get_organization(organization_name)
repos = organization.get_repos()
return self._get_older_pull_requests(repos, num_days)

def _get_older_pull_requests(self, repos, num_days):
old_pulls = []
for repo in repos:
pulls = repo.get_pulls()
for pull in pulls:
if pull.created_at < datetime.datetime.now() - datetime.timedelta(days=num_days):
old_pulls.append(pull.html_url)

return old_pulls

def _get_client(self):
return Github(self.username, self.password)





27 changes: 27 additions & 0 deletions githubslackbot/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from slackclient import SlackClient


class SlackIntegration:
def __init__(self, slack_token):
self.slack_token = slack_token

def send_message(self, channel_id, message):
client = self._get_client()
self._auth(client)
result = client.api_call(
"chat.postMessage",
channel_id=channel_id,
text=message,
username='github-slack-bot',
icon_emoji=':robot_face:'
)
print(result)

def _get_client(self):
return SlackClient(self.slack_token)

def _auth(self, client):
auth1 = client.api_call("api.test")
auth2 = client.api_call("auth.test")
print(auth1)
print(auth2)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pygithub==1.34
slackclient==1.0.5
19 changes: 19 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from config import (
GITHUB_USERNAME,
GITHUB_PASSWORD,
GITHUB_ORGANIZATION,
SLACK_CHANNEL,
SLACK_TOKEN
)

from githubslackbot.github import GitHubIntegration
from githubslackbot.slack import SlackIntegration

if __name__ == '__main__':
github_integration = GitHubIntegration(GITHUB_USERNAME, GITHUB_PASSWORD)
pr = github_integration.get_org_pull_requests_older_than_num_days(GITHUB_ORGANIZATION, 3)

slack_integration = SlackIntegration(SLACK_TOKEN)
message = "Repos with PR older than 3 days\n" + '\n'.join(pr)
print("sending {} to channel {}".format(message, SLACK_CHANNEL))
slack_integration.send_message(SLACK_CHANNEL, message)

0 comments on commit dedefc1

Please sign in to comment.