diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..03cf2b6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +local.py +__pycache__/ +*.py[cod] +*$py.class diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cbc0991 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c493890 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# github-slackbot +Slack bot which sends notifications with organization's PR older than a number of days diff --git a/config.py b/config.py new file mode 100644 index 0000000..34f8dfd --- /dev/null +++ b/config.py @@ -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) diff --git a/githubslackbot/__init__.py b/githubslackbot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/githubslackbot/github.py b/githubslackbot/github.py new file mode 100644 index 0000000..a2a6673 --- /dev/null +++ b/githubslackbot/github.py @@ -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) + + + + + diff --git a/githubslackbot/slack.py b/githubslackbot/slack.py new file mode 100644 index 0000000..72e7e4d --- /dev/null +++ b/githubslackbot/slack.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..df27ee0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pygithub==1.34 +slackclient==1.0.5 diff --git a/run.py b/run.py new file mode 100644 index 0000000..393d118 --- /dev/null +++ b/run.py @@ -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)