Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misleading config param github_url/base_url #29

Merged
merged 5 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog


## 1.0.0

* Clean up `enterprise_url` params, fixing github enterprise support

## 0.8.4

* Version bump to fix tagging issues, no code changes
Expand All @@ -14,7 +19,7 @@

## 0.8.1

* Make `repository_sensor` section in config schema optional
* Make `repository_sensor` section in config schema optional

## 0.8.0

Expand All @@ -23,7 +28,7 @@

## 0.7.1

* Update sensor to use ``base_url`` option.
* Update sensor to use ``base_url`` option.

## 0.7.0

Expand Down Expand Up @@ -54,7 +59,7 @@
* Migrate config.yaml to config.schema.yaml.
* Add actions and aliases managing releases (list, create, latest).
* Add actions and aliases for managing deployments.
* Add action and aliases for sorting a user scoped GitHub oauth token
* Add action and aliases for sorting a user scoped GitHub oauth token
for GitHub.com and GitHub Enterprise.

## v0.4.0
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ Currently supported event types:
* ``ReleaseEvent`` - Triggered when new release is available.
* ``PushEvent`` - Triggered when a repository branch is pushed to. In addition to branch pushes, webhook push events are also triggered when repository tags are pushed.

**Note** : The sensor will only listen events for the `github_type` you chosen
in config.yaml


#### github.repository_event trigger

Example trigger payload:
Expand Down Expand Up @@ -131,7 +135,6 @@ command:
### Limitations

- You need to have logged an OAuth key with StackStorm (via `github.store_oauth_token`).
- It only works for the default `github_type`.
- If using with GitHub.com your ST2 server needs to be contactable via the internet!
- Deployment Statuses will be logged as the creating user in GitHub.
- With StackStorm v2.1+ you should be able to deploy tags.
5 changes: 3 additions & 2 deletions actions/get_clone_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class GetCloneStatsAction(BaseGithubAction):
def run(self, repo):
clone_data = self._get_analytics(category='clone-activity-data', repo=repo)
def run(self, repo, github_type):
clone_data = self._get_analytics(
category='clone-activity-data', repo=repo, enterprise=self._is_enterprise(github_type))
return clone_data['summary']
4 changes: 4 additions & 0 deletions actions/get_clone_stats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ parameters:
type: string
description: "Repository to query for clone stats (org/repo)"
required: true
github_type:
type: "string"
description: "The type of github installation to target, if unset will use the configured default."
default: ~
5 changes: 3 additions & 2 deletions actions/get_traffic_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


class GetTrafficStatsAction(BaseGithubAction):
def run(self, repo):
traffic_data = self._get_analytics(category='traffic-data', repo=repo)
def run(self, repo, github_type):
traffic_data = self._get_analytics(
category='traffic-data', repo=repo, enterprise=self._is_enterprise(github_type))
return traffic_data['summary']
4 changes: 4 additions & 0 deletions actions/get_traffic_stats.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ parameters:
type: string
description: "Repository to query for traffic stats (org/repo)"
required: true
github_type:
type: "string"
description: "The type of github installation to target, if unset will use the configured default."
default: ~
5 changes: 3 additions & 2 deletions actions/get_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@


class GetUserAction(BaseGithubAction):
def run(self, user, token_user):
def run(self, user, token_user, github_type):
enterprise = self._is_enterprise(github_type)
if token_user:
self._change_to_user_token(token_user)
self._change_to_user_token(token_user, enterprise)

user = self._client.get_user(user)
result = user_to_dict(user=user)
Expand Down
4 changes: 4 additions & 0 deletions actions/get_user.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ parameters:
type: "string"
description: "The"
default: "{{action_context.api_user|default(None)}}"
github_type:
type: "string"
description: "The type of github installation to target, if unset will use the configured default."
default: ~
39 changes: 25 additions & 14 deletions actions/lib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ def __init__(self, config):
super(BaseGithubAction, self).__init__(config=config)
token = self.config.get('token', None)
self.token = token or None
self.github_url = self.config.get('github_url', DEFAULT_API_URL)
self.enterprise_url = self.config.get('enterprise_url', None)

self.web_url = self.config.get('web_url', None)
self.base_url = self.config.get('base_url', None)

self.default_github_type = self.config.get('github_type', None)

self._client = Github(self.token, base_url=self.github_url)
if self.default_github_type == 'online':
self._client = Github(self.token, base_url=DEFAULT_API_URL)
else:
self._client = Github(self.token, base_url=self.base_url)

self._session = requests.Session()

def _web_session(self):
def _web_session(self, web_url=DEFAULT_WEB_URL):
'''Returns a requests session to scrape off the web'''
login_url = DEFAULT_WEB_URL + '/login'
login_url = web_url + '/login'
session = requests.Session()
request = session.get(login_url).text
html = BeautifulSoup(request)
Expand All @@ -46,13 +52,18 @@ def _web_session(self):
'authenticity_token': token
}

session_url = DEFAULT_WEB_URL + session_path
session_url = web_url + session_path
session.post(session_url, data=login_data)
return session

def _get_analytics(self, category, repo):
url = DEFAULT_WEB_URL + repo + '/graphs/' + category + '.json'
s = self._web_session()
def _get_analytics(self, category, repo, enterprise):
if enterprise:
url = self.web_url + repo + '/graphs/' + category + '.json'
s = self._web_session(self.web_url)
else:
url = DEFAULT_WEB_URL + repo + '/graphs/' + category + '.json'
s = self._web_session()

response = s.get(url)
return response.json()

Expand Down Expand Up @@ -90,23 +101,23 @@ def _get_user_token(self, user, enterprise):

return token

def _change_to_user_token(self, user, enterprise=False):
def _change_to_user_token(self, user, enterprise):
token = self._get_user_token(user, enterprise)

if enterprise:
self._client = Github(token, base_url=self.enterprise_url)
self._client = Github(token, base_url=self.base_url)
else:
self._client = Github(token, base_url=self.github_url)
self._client = Github(token, base_url=DEFAULT_API_URL)

return True

def _request(self, method, uri, payload, token, enterprise):
headers = {'Authorization': 'token {}'.format(token)}

if enterprise:
url = "{}{}".format(self.enterprise_url, uri)
url = "{}{}".format(self.base_url, uri)
else:
url = "{}{}".format(self.github_url, uri)
url = "{}{}".format(DEFAULT_API_URL, uri)

try:
r = self._session.request(method,
Expand Down
16 changes: 9 additions & 7 deletions config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ github_type:
- "enterprise"
required: true

base_url:
description: "The GitHub URL, for GitHub Enterprise please set enterprise_url."
web_url:
description: "The GitHub URL."
type: "string"
default: "https://api.github.com"
required: true
default: "https://github.example.com"
required: false

enterprise_url:
description: "GitHub API url (including /api/v3) of your GitHub Enterprise hostname."
base_url:
description: "GitHub API url, include /api/v3 for GitHub Enterprise. exp: "
type: "string"
default: "https://github.example.com/api/v3"
required: false

deployment_environment:
description: "The environment for this StackStorm server."
Expand All @@ -39,7 +41,7 @@ deployment_environment:
default: "production"

repository_sensor:
description: "Sensor specific settings."
description: "Sensor specific settings, with default github type."
type: "object"
required: false
additionalProperties: false
Expand Down
4 changes: 2 additions & 2 deletions github.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ token: "{{system.github_oauth_token}}"
user: "Stanley"
password: "P4ssw0rd"
github_type: "online"
base_url: "https://api.github.com"
enterprise_url: ""
web_url: "https://github.example.com"
base_url: "https://github.example.com/api/v3"
deployment_environment: "production"
repository_sensor:
event_type_whitelist:
Expand Down
2 changes: 1 addition & 1 deletion pack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ keywords:
- git
- scm
- serverless
version : 0.9.0
version : 1.0.0
python_versions:
- "2"
- "3"
Expand Down
11 changes: 10 additions & 1 deletion sensors/github_repository_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
DATE_FORMAT_STRING = '%Y-%m-%d %H:%M:%S'


# Default Github API url
DEFAULT_API_URL = 'https://api.github.com'


class GithubRepositorySensor(PollingSensor):
def __init__(self, sensor_service, config=None, poll_interval=None):
super(GithubRepositorySensor, self).__init__(sensor_service=sensor_service,
Expand All @@ -29,7 +33,12 @@ def __init__(self, sensor_service, config=None, poll_interval=None):

def setup(self):
# Empty string '' is not ok but None is fine. (Sigh)
config_base_url = self._config.get('base_url', None) or None
github_type = self._config.get('github_type', None)
if github_type == 'online':
config_base_url = DEFAULT_API_URL
else:
config_base_url = self._config.get('base_url', None) or None

config_token = self._config.get('token', None) or None
self._client = Github(config_token or None, base_url=config_base_url)

Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures/full-enterprise.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
enterprise_url: "https://github.exmple.com/api/v3"
web_url: "https://github.exmple.com"
base_url: "https://github.exmple.com/api/v3"
token: "foobar"

default: "enterprise"
github_type: "enterprise"
5 changes: 2 additions & 3 deletions tests/fixtures/full.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
enterprise_url: "https://github.exmple.com/api/v3"
token: "foobar"

default: "online"
github_type: "online"

deployment_environment: "production"
deployment_environment: "production"