diff --git a/CHANGES.md b/CHANGES.md index 55ff741..069c225 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 @@ -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 @@ -23,7 +28,7 @@ ## 0.7.1 -* Update sensor to use ``base_url`` option. +* Update sensor to use ``base_url`` option. ## 0.7.0 @@ -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 diff --git a/README.md b/README.md index cbdbb0e..9588966 100755 --- a/README.md +++ b/README.md @@ -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: @@ -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. diff --git a/actions/get_clone_stats.py b/actions/get_clone_stats.py index d02314b..0d235af 100755 --- a/actions/get_clone_stats.py +++ b/actions/get_clone_stats.py @@ -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'] diff --git a/actions/get_clone_stats.yaml b/actions/get_clone_stats.yaml index d63f847..6b684c5 100755 --- a/actions/get_clone_stats.yaml +++ b/actions/get_clone_stats.yaml @@ -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: ~ diff --git a/actions/get_traffic_stats.py b/actions/get_traffic_stats.py index b0129bd..0a76e42 100755 --- a/actions/get_traffic_stats.py +++ b/actions/get_traffic_stats.py @@ -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'] diff --git a/actions/get_traffic_stats.yaml b/actions/get_traffic_stats.yaml index 3c4135a..a091ba7 100755 --- a/actions/get_traffic_stats.yaml +++ b/actions/get_traffic_stats.yaml @@ -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: ~ diff --git a/actions/get_user.py b/actions/get_user.py index 3813b89..9b25dd0 100755 --- a/actions/get_user.py +++ b/actions/get_user.py @@ -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) diff --git a/actions/get_user.yaml b/actions/get_user.yaml index d7c37f4..acb15f7 100755 --- a/actions/get_user.yaml +++ b/actions/get_user.yaml @@ -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: ~ diff --git a/actions/lib/base.py b/actions/lib/base.py index 627c4ed..0d236b4 100755 --- a/actions/lib/base.py +++ b/actions/lib/base.py @@ -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) @@ -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() @@ -90,13 +101,13 @@ 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 @@ -104,9 +115,9 @@ 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, diff --git a/config.schema.yaml b/config.schema.yaml index 44d1007..44e2abe 100755 --- a/config.schema.yaml +++ b/config.schema.yaml @@ -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." @@ -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 diff --git a/github.yaml.example b/github.yaml.example index 2f88319..1bfe3ca 100755 --- a/github.yaml.example +++ b/github.yaml.example @@ -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: diff --git a/pack.yaml b/pack.yaml index 4feb5b9..cc2369b 100755 --- a/pack.yaml +++ b/pack.yaml @@ -8,7 +8,7 @@ keywords: - git - scm - serverless -version : 0.9.0 +version : 1.0.0 python_versions: - "2" - "3" diff --git a/sensors/github_repository_sensor.py b/sensors/github_repository_sensor.py index 603eb33..4ac0c30 100755 --- a/sensors/github_repository_sensor.py +++ b/sensors/github_repository_sensor.py @@ -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, @@ -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) diff --git a/tests/fixtures/full-enterprise.yaml b/tests/fixtures/full-enterprise.yaml index 0491460..ef9f0d4 100755 --- a/tests/fixtures/full-enterprise.yaml +++ b/tests/fixtures/full-enterprise.yaml @@ -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" diff --git a/tests/fixtures/full.yaml b/tests/fixtures/full.yaml index 1a9bde4..3f67084 100755 --- a/tests/fixtures/full.yaml +++ b/tests/fixtures/full.yaml @@ -1,6 +1,5 @@ -enterprise_url: "https://github.exmple.com/api/v3" token: "foobar" -default: "online" +github_type: "online" -deployment_environment: "production" \ No newline at end of file +deployment_environment: "production"