Skip to content

Commit

Permalink
adds organization support
Browse files Browse the repository at this point in the history
  • Loading branch information
jayfk committed Mar 8, 2016
1 parent 1cf92cb commit f911e29
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 75 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -69,6 +69,16 @@ This will update your status page and show a *major outage* on your *Website*.
If you change the issue (eg. when you add a new label, create a comment or close the issue), you'll
need to run `statuspage update` again.

## Use Organization Account

In order to create/update a status page for an organization, add the name of the organization to
the `--org` flag, e.g.:

statuspage create --org=my-org --name=..
Please note: You need to have the proper permissions to create a new repository for the given
organization.

## Customizing

Expand Down
39 changes: 25 additions & 14 deletions statuspage.py
Expand Up @@ -42,22 +42,28 @@ def cli(): # pragma: no cover
@cli.command()
@click.option('--name', prompt='Name', help='')
@click.option('--token', prompt='GitHub API Token', help='')
@click.option('--org', help='GitHub Organization', default=False)
@click.option('--systems', prompt='Systems, eg (Website,API)', help='')
def create(token, name, systems):
run_create(name=name, token=token, systems=systems)
def create(token, name, systems, org):
run_create(name=name, token=token, systems=systems, org=org)


@cli.command()
@click.option('--name', prompt='Name', help='')
@click.option('--org', help='GitHub Organization', default=False)
@click.option('--token', prompt='GitHub API Token', help='')
def update(name, token):
run_update(name=name, token=token)
def update(name, token, org):
run_update(name=name, token=token, org=org)


def run_update(name, token):
def run_update(name, token, org):
click.echo("Generating..")
gh = Github(token)
repo = gh.get_user().get_repo(name=name)
if org:
repo = gh.get_organization(org).get_repo(name=name)
else:
repo = gh.get_user().get_repo(name=name)

systems, incidents, panels = {}, [], {}

# get all systems and mark them as operational
Expand Down Expand Up @@ -147,12 +153,16 @@ def run_update(name, token):
)


def run_create(name, token, systems):
def run_create(name, token, systems, org):
gh = Github(token)
user = gh.get_user()

if org:
entity = gh.get_organization(org)
else:
entity = gh.get_user()

# create the repo
repo = user.create_repo(name=name)
repo = entity.create_repo(name=name)

# get all labels an delete them
for label in tqdm(list(repo.get_labels()), "Deleting initial labels"):
Expand All @@ -172,7 +182,7 @@ def run_create(name, token, systems):
path="/README.md",
message="initial",
content="Visit this site at https://{login}.github.io/{name}/".format(
login=user.login,
login=entity.login,
name=name
),
)
Expand All @@ -195,14 +205,14 @@ def run_create(name, token, systems):
repo.edit(name=name, default_branch="gh-pages")

# run an initial update to add content to the index
run_update(token=token, name=name)
run_update(token=token, name=name, org=org)

click.echo("Create new issues at https://github.com/{login}/{name}/issues".format(
login=user.login,
login=entity.login,
name=name
))
click.echo("Visit your new status page at https://{login}.github.io/{name}/".format(
login=user.login,
login=entity.login,
name=name
))

Expand All @@ -211,7 +221,8 @@ def run_create(name, token, systems):
click.echo("# IMPORTANT: Whenever you add or close an issue you have to run the update #")
click.echo("# command to show the changes reflected on your status page. #")
click.echo("# Here's a one-off command for this repo to safe it somewhere safe: #")
click.echo("# statuspage update --name={name} --token={token}".format(name=name, token=token))
click.echo("# statuspage update --name={name} --token={token} {org}".format(
name=name, token=token, org="--org=" + entity.login if org else ""))
click.echo("###############################################################################")


Expand Down
130 changes: 69 additions & 61 deletions tests.py
Expand Up @@ -11,32 +11,9 @@

class CLITestCase(TestCase):

@patch("statuspage.run_update")
@patch("statuspage.Github")
def test_create(self, GithubMock, run_update):

gh = Mock()
GithubMock.return_value = gh

label = Mock()
gh.get_user().create_repo().get_labels.return_value = [label,]

runner = CliRunner()
result = runner.invoke(
create,
["--name", "testrepo", "--token", "token", "--systems", "sys1,sys2"]
)

self.assertEqual(result.exit_code, 0)

GithubMock.assert_called_once_with("token")


@patch("statuspage.Github")
def test_update(self, GithubMock):

gh = Mock()
GithubMock.return_value = gh
def setUp(self):
self.patcher = patch('statuspage.Github')
self.gh = self.patcher.start()

# setup mocked label
label = Mock()
Expand All @@ -47,7 +24,7 @@ def test_update(self, GithubMock):
label1.color = "171717"
label1.name = "API"

gh.get_user().get_repo().get_labels.return_value = [label, label1]
self.gh().get_user().get_repo().get_labels.return_value = [label, label1]

# set up mocked issue
issue = Mock()
Expand All @@ -66,55 +43,86 @@ def test_update(self, GithubMock):
issue1.get_labels.return_value = [issue_label, label1]
issue1.get_comments.return_value = [comment, ]

gh.get_user().get_repo().get_issues.return_value = [issue, issue1]
template = Mock()
template.decoded_content = b"some foo"
gh.get_user().get_repo().get_file_contents.return_value = template
self.gh().get_user().get_repo().get_issues.return_value = [issue, issue1]
self.template = Mock()
self.template.decoded_content = b"some foo"
self.gh().get_user().get_repo().get_file_contents.return_value = self.template
self.gh().get_organization().get_repo().get_file_contents.return_value = self.template

def tearDown(self):

self.patcher.stop()

@patch("statuspage.run_update")
def test_create(self, run_update):

label = Mock()
self.gh().get_user().create_repo().get_labels.return_value = [label,]

runner = CliRunner()
result = runner.invoke(
create,
["--name", "testrepo", "--token", "token", "--systems", "sys1,sys2"]
)

self.assertEqual(result.exit_code, 0)

self.gh.assert_called_with("token")

@patch("statuspage.run_update")
def test_create_org(self, run_update):

runner = CliRunner()
result = runner.invoke(
create,
["--name", "testrepo",
"--token", "token",
"--systems", "sys1,sys2",
"--org", "some"]
)

self.assertEqual(result.exit_code, 0)

self.gh.assert_called_with("token")
self.gh().get_organization.assert_called_with("some")

def test_update(self):

runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])

self.assertEqual(result.exit_code, 0)
GithubMock.assert_called_with("token")

gh.get_user().get_repo.assert_called_with(name="testrepo")
gh.get_user().get_repo().get_labels.assert_called_once_with()
self.gh.assert_called_with("token")

@patch("statuspage.Github")
def test_update_index_does_not_exist(self, GithubMock):
self.gh().get_user().get_repo.assert_called_with(name="testrepo")
self.gh().get_user().get_repo().get_labels.assert_called_once_with()

gh = Mock()
GithubMock.return_value = gh
def test_update_org(self):

# setup mocked label
label = Mock()
label.color = "171717"
label.name = "Website"
gh.get_user().get_repo().get_labels.return_value = [label, ]
runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token", "--org", "some"])

# set up mocked issue
issue = Mock()
issue.state = "open"
issue_label = Mock()
issue_label.color = "FF4D4D"
issue_label.name = "major outage"
issue.get_labels.return_value = [issue_label, label]
comment = Mock()
issue.get_comments.return_value = [comment, ]
gh.get_user().get_repo().get_issues.return_value = [issue, ]
template = Mock()
template.decoded_content = b"some foo"
gh.get_user().get_repo().get_file_contents.return_value = template
gh.get_user().get_repo().update_file.side_effect = UnknownObjectException(status=404, data="foo")
self.assertEqual(result.exit_code, 0)

self.gh.assert_called_with("token")

self.gh().get_organization().get_repo.assert_called_with(name="testrepo")
self.gh().get_organization().get_repo().get_labels.assert_called_once_with()

def test_update_index_does_not_exist(self):

self.gh().get_user().get_repo().update_file.side_effect = UnknownObjectException(status=404, data="foo")

runner = CliRunner()
result = runner.invoke(update, ["--name", "testrepo", "--token", "token"])
self.assertEqual(result.exit_code, 0)

GithubMock.assert_called_with("token")
self.gh.assert_called_with("token")

gh.get_user().get_repo.assert_called_with(name="testrepo")
gh.get_user().get_repo().get_labels.assert_called_once_with()
gh.get_user().get_repo().create_file.assert_called_once_with(
self.gh().get_user().get_repo.assert_called_with(name="testrepo")
self.gh().get_user().get_repo().get_labels.assert_called_once_with()
self.gh().get_user().get_repo().create_file.assert_called_once_with(
branch='gh-pages',
content='some foo',
message='initial',
Expand Down

0 comments on commit f911e29

Please sign in to comment.