Skip to content

Commit

Permalink
Add Project-to-Milestone sync
Browse files Browse the repository at this point in the history
Tickets added to GitHub projects will be added to Milestones of the same name. Changes on a project
board which is closed will not have an affect on the milestone.
  • Loading branch information
ltankey authored and monkeypants committed Sep 27, 2018
1 parent 3cc7338 commit 7d0cad3
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
3 changes: 1 addition & 2 deletions .envs/.local/.flask
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
GITHUB_USER=supmanbot
GITHUB_PSX="ha! wouldn't you like to know"
READ_DOT_ENV_FILE=True
9 changes: 5 additions & 4 deletions config/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# from os import environ
from envparse import env
from environ import Env as EnvironEnv
from .kms import string_or_b64kms


class Env(object):
def __call__(self, var, default=None, cast=None):
value = env(var, default=default, cast=cast)
class Env(EnvironEnv):
def __call__(self, var, default=None, cast=None, parse_default=False):
value = self.get_value(
var, default=default, cast=cast, parse_default=parse_default)
return string_or_b64kms(value)

# class Env(object):
Expand Down
7 changes: 7 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Load our conf
from . import Env
import environ

env = Env()

READ_DOT_ENV_FILE = env('READ_DOT_ENV_FILE', default=False)
if READ_DOT_ENV_FILE:
# OS environment variables take precedence over variables from .env
ROOT_DIR = environ.Path(__file__) - 2
env.read_env(str(ROOT_DIR.path('.env')))

DEBUG = env('DEBUG', default=False, cast=bool)
GITHUB_USER = env('GITHUB_USER')
GITHUB_PSX = env('GITHUB_PSX')
Empty file added gnome/__init__.py
Empty file.
7 changes: 4 additions & 3 deletions gnome/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def move_to_milestone(self, new_milestone):
if not self.repo.milestone_exists(new_milestone):
self.repo.create_milestone(new_milestone)
m = self.repo.get_milestone(new_milestone)
self._issue.milestone = m._milestone
self._issue.edit(milestone=m._milestone)


class Repo:
Expand All @@ -96,7 +96,8 @@ def __init__(self, repo_name):

def update_milestones(self):
self._milestones = {
x.title: Milestone(self._repo, x) for x in self._repo.get_milestones(state='all')
x.title: Milestone(self._repo, x)
for x in self._repo.get_milestones(state='all')
}

@property
Expand All @@ -114,7 +115,7 @@ def upsert_milestone(self, title, **kwargs):
self.create_milestone(title, **kwargs)

def get_config(self):
conf_file = self._repo.get_contents('.bugflow/.gnome.yml')
conf_file = self._repo.get_contents('.gnome.yml')
if conf_file:
return yaml.load(base64.b64decode(conf_file.content))

Expand Down
Empty file added gnome/plugins/__init__.py
Empty file.
35 changes: 34 additions & 1 deletion gnome/plugins/sync_project_milestones.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
from gnome.policies import Policy
from gnome.gh import Repo, Issue, gh


class SyncProjectMilestones(Policy):
def dispatch_gnome(self):
pass
headers = self.callback.headers()
event = headers['X-GitHub-Event']

if event not in ('project_card'):
return

payload = self.callback.payload()
card = payload['project_card']
if not payload['action'] == 'deleted':
if 'content_url' in card:
# Is a ticket
repo_name = payload['repository']['full_name']
issue_url = card['content_url']
issue_number = issue_url.split('/')[-1]
repo = Repo(repo_name)
gh_issue = repo._repo.get_issue(int(issue_number))
issue = Issue(repo, gh_issue)

# Project details
project_url = payload['project_card']['project_url']
project_id = project_url.split('/')[-1]
project = gh.get_project(int(project_id))
project_name = project.name

if project.state == 'closed':
return

if gh_issue.milestone \
and gh_issue.milestone.title == project_name:
print('Milestone already set - aborting')
return

issue.move_to_milestone(project_name)
5 changes: 4 additions & 1 deletion gnome/policies/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import abc


class Policy(abc.ABC):
# __metaclass__ = abc.ABCMeta
"""
Expand Down Expand Up @@ -28,10 +29,12 @@ class AbstractBaseGnomePolicyCanNotBeDispatchedError(Exception): pass
from gnome.plugins.verbose_callback_logging import VerboseCallbackLogging
from gnome.plugins.sorting_hat import SortingHat
from gnome.plugins.propagate_milestones import PropagateMilestones
from gnome.plugins.sync_project_milestones import SyncProjectMilestones

# TODO: make a decision about case for config
MANIFEST = {
'SortingHat': SortingHat,
'VerboseCallbackLogging': VerboseCallbackLogging,
'propagate_milestones': PropagateMilestones
'propagate_milestones': PropagateMilestones,
'SyncProjectMilestones': SyncProjectMilestones
}
10 changes: 0 additions & 10 deletions gnome/requirements.txt

This file was deleted.

5 changes: 2 additions & 3 deletions requirements/base.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
flask
zappa
PyGithub
PyGithub==1.43.2
requests
ipaddress
PyYAML==3.12
python-dotenv
envparse==0.2.0
django-environ==0.4.5 # https://github.com/joke2k/django-environ

0 comments on commit 7d0cad3

Please sign in to comment.