Skip to content

Commit

Permalink
Merge branch 'fixes/1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ypid committed Jul 18, 2016
2 parents 1e3f83e + edde007 commit 8028736
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ check: tests/api_data

tests/api_data: bin/debops-api tests/example_roles FORCE_MAKE
"$<" --test-mode --role-path tests/example_roles/ \
--docs-url-pattern 'http://docs.debops.org/en/latest/ansible/roles/ansible-{name}/docs/index.html' \
--docs-url-pattern 'http://docs.debops.org/en/latest/ansible/roles/ansible-{role_name}/docs/index.html' \
--role-owner debops \
--api-dir "$@"

Expand Down
79 changes: 54 additions & 25 deletions bin/debops-api
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ class DebOpsAPI:
"""

github_base_url = 'https://github.com'
return '/'.join(github_base_url, role_owner, role_name)
return '/'.join([
github_base_url,
role_owner,
'ansible-' + self._get_normalized_role_name(role_name),
])

def _read_github_repos_api_response(
self,
Expand Down Expand Up @@ -113,7 +117,7 @@ class DebOpsAPI:
'role_owner': role_owner,
'role_name': role_name,
'vcs_url': repo['html_url'],
'docs_format_version': '0.1.0',
'role_format_version': '0.1.0',
}
self._metadata.setdefault(role_full_name, {})
self._metadata[role_full_name].update(metadata_from_api)
Expand All @@ -140,21 +144,21 @@ class DebOpsAPI:
'0.2.0': re.compile(r'^ansible-(?P<role_name>[a-z0-9_-]+)$'),
}

for docs_format_version, pattern in version_by_pattern_map.items():
for role_format_version, pattern in version_by_pattern_map.items():
_re = pattern.search(role_dir_name)
if _re:
role_owner = None
if 'role_owner' in _re.groups():
role_owner = _re.group('role_owner')
role_name = _re.group('role_name')
logger.debug('Detected docs format version {} for owner: {}, name: {} from {}'.format(
docs_format_version,
role_format_version,
role_owner,
role_name,
role_dir_name,
))
return {
'docs_format_version': docs_format_version,
'role_format_version': role_format_version,
'role_owner': role_owner,
'role_name': role_name,
}
Expand Down Expand Up @@ -213,18 +217,30 @@ class DebOpsAPI:
# except git.exc.GitCommandError:
version = '0.0.0'

version = re.sub(r'^v', '', version)
# logger.debug(version)

if self._test_mode:
# Fake committer date in test mode
last_committer_date = '1970-01-01 00:00:00 +0000'

return {
'last_committer_date': last_committer_date,
'version': version,
metadata = {
'vcs_last_committer_date': last_committer_date,
'version': re.sub(r'^v', '', version),
}

if not self._test_mode and version != '0.0.0':
try:
commits_since_last_release = len(
g.log('{}...HEAD'.format(version), '--oneline').split('\n')
)
except:
commits_since_last_release = None

if commits_since_last_release is not None:
metadata.update({
'vcs_commits_since_last_release': commits_since_last_release,
})

return metadata

def _get_role_metadata(self, role_path):
"""
Read metadata for the given role.
Expand Down Expand Up @@ -261,6 +277,16 @@ class DebOpsAPI:

return metadata

def _get_normalized_role_name(self, role_name):
"""
Returns normalized role name as used in URLs
Example role name: `ansible`, returns: `role-ansible`.
"""

if role_name == 'ansible':
role_name = 'role-' + role_name
return role_name

def _get_normalized_meta_main(self, meta_main):
"""
Returns normalized meta/main.yml data intended for inclusion in
Expand Down Expand Up @@ -305,16 +331,17 @@ class DebOpsAPI:
role_name = metadata['role_name']

additonal_metadata = {
'normalized_role_name': self._get_normalized_role_name(role_name),
'ci_badge_url': 'https://api.travis-ci.org/{}/ansible-{}.png'.format(
role_owner,
role_name,
self._get_normalized_role_name(role_name),
),
'ci_url': 'https://travis-ci.org/{}/ansible-{}'.format(
role_owner,
role_name,
self._get_normalized_role_name(role_name),
),
'test_suite_url': 'https://github.com/debops/test-suite/tree/master/ansible-{}'.format(
role_name,
self._get_normalized_role_name(role_name),
),
'galaxy_url': 'https://galaxy.ansible.com/{}/{}'.format(
role_owner,
Expand All @@ -330,11 +357,12 @@ class DebOpsAPI:
})

if self._docs_url_pattern and StrictVersion('0.2.0') <= \
StrictVersion(metadata['docs_format_version']):
StrictVersion(metadata['role_format_version']):

additonal_metadata['docs_url'] = self._docs_url_pattern.format(
owner=role_owner,
name=role_name,
role_owner=role_owner,
role_name=role_name,
normalized_role_name=self._get_normalized_role_name(role_name),
)

self._metadata[role_full_name].update(additonal_metadata)
Expand All @@ -351,24 +379,24 @@ class DebOpsAPI:
# required to get the meta data from external servers (and to
# encourage conversion to the new docs format).
if role_dir_info:
if StrictVersion('0.2.0') <= StrictVersion(role_dir_info['docs_format_version']):
if StrictVersion('0.2.0') <= StrictVersion(role_dir_info['role_format_version']):
role_name = role_dir_info['role_name']
role_metadata = self._get_role_metadata(os.path.join(role_path, role_dir_name))
role_vcs_url = role_metadata['ansigenome']['github_url']
role_owner = self._get_owner_from_vcs_url(role_vcs_url)
role_full_name = self._get_role_full_name(role_owner, role_name)

vcs_info = self._get_vcs_info(os.path.join(role_path, role_dir_name))

metadata = {
'vcs_url': role_vcs_url,
'vcs_last_committer_date': vcs_info['last_committer_date'],
'version': vcs_info['version'],
'docs_format_version': role_dir_info['docs_format_version'],
'role_format_version': role_dir_info['role_format_version'],
'role_owner': role_owner,
'role_name': role_name,
}

metadata.update(
self._get_vcs_info(os.path.join(role_path, role_dir_name))
)

if 'meta' in role_metadata:
metadata.update(
self._get_normalized_meta_main(role_metadata['meta'])
Expand All @@ -385,15 +413,15 @@ class DebOpsAPI:
if not role_owner:
raise Exception('Default role owner not given but required.')

role_vcs_url = 'https://github.com/{}/ansible-{}'.format(
role_vcs_url = self._get_repo_url(
role_owner,
role_name,
)
role_full_name = self._get_role_full_name(role_owner, role_name)

metadata = {
'vcs_url': role_vcs_url,
'docs_format_version': '0.1.0',
'role_format_version': '0.1.0',
'role_owner': role_owner,
'role_name': role_name,
}
Expand Down Expand Up @@ -487,6 +515,7 @@ class DebOpsAPI:
outfile.write('\n'.join(sorted(role_list)) + '\n')
with open(os.path.join(api_work_dir, 'roles', role_owner + '.json'), 'w') as outfile:
json.dump(self.get_metadata(metadata), outfile, sort_keys=True)
outfile.write('\n')
with open(os.path.join(api_work_dir, 'roles', 'count:' + role_owner), 'w') as outfile:
outfile.write('{}\n'.format(len(self.get_metadata(metadata).keys())))

Expand Down
2 changes: 1 addition & 1 deletion docs/_static/api/role/debops.unattended_upgrades.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"role_owner": "debops", "role_name": "unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "vcs_last_committer_date": "2016-07-10 21:50:38 +0200", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "version": "0.2.0", "docs_format_version": "0.2.0", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades"}
{"role_owner": "debops", "role_name": "unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "vcs_last_committer_date": "2016-07-10 21:50:38 +0200", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "version": "0.2.0", "role_format_version": "0.2.0", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades"}
2 changes: 1 addition & 1 deletion docs/_static/api/roles/debops.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"debops.unattended_upgrades": {"role_owner": "debops", "role_name": "unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "vcs_last_committer_date": "2016-07-10 21:50:38 +0200", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "version": "0.2.0", "docs_format_version": "0.2.0", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades"}}
{"debops.unattended_upgrades": {"role_owner": "debops", "role_name": "unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "vcs_last_committer_date": "2016-07-10 21:50:38 +0200", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "version": "0.2.0", "role_format_version": "0.2.0", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades"}}
42 changes: 27 additions & 15 deletions docs/api-reference-v0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Example: https://api.debops.org/v0/roles/debops.list
~~~~~~~~~~~~~~~~~~~~~~

Returns the total number of roles in the given name space.
The ``[:<owner>]`` is optional and allows to limit the count to the role owner.
The ``[:<owner>]`` is optional and allows to limit the count to the given role
owner.

Examples:

Expand All @@ -65,10 +66,21 @@ described below.
:regexp:`^/roles/.*\.json$` API calls return a JSON object. The outer dict is a
full role name to meta data (described below) mapping.

.. note:: ``docs_format_version`` below ``0.2.0`` are not fully supported by
.. note:: ``role_format_version`` below ``0.2.0`` are not fully supported by
this API. Keys might be missing for roles with this version.
Do a version compare for ``0.2.0`` or higher of fixup the DebOps API.

``role_owner``
Ansible Galaxy role owner.

``role_name``
Ansible Galaxy role name.

``normalized_role_name``
Ansible role name as used in URLs. Currently the only case where this is
different to ``role_name`` is when ``role_name`` is ``ansible`` (in this case
``normalized_role_name`` will be ``role-ansible``).

``authors``
List of dicts, one dict for each author.

Expand All @@ -84,8 +96,9 @@ full role name to meta data (described below) mapping.
``description``
Description of the repository.

``docs_format_version``
Version of the DebOps documentation format used for the repository.
``role_format_version``
To which version of the DebOps role standard does the role comply to.
TODO: The versions are currently not documented elsewhere.

``docs_url``
HTML URL of the rendered documentation of the repository
Expand All @@ -108,12 +121,6 @@ full role name to meta data (described below) mapping.
``pr_url``
HTML URL on the VCS platform where Pull/Merge requests can be submitted to.

``role_name``
Ansible Galaxy role name.

``role_owner``
Ansible Galaxy role owner.

``tags``
List of tags of the repository, currently equivalent with Ansible Galaxy role tags.

Expand All @@ -125,14 +132,19 @@ full role name to meta data (described below) mapping.
repository is tested.

``ci_url``
HTML URL for the continues integration system on which the repository is
tested.

``vcs_last_committer_date``
Date of last commit in VCS.
HTML URL for the test page of the continues integration system which is used
for the repository.

``vcs_url``
HTML URL to the VCS platform where the repository is hosted.

``version``
Latest released version of the repository.
Is ``0.0.0`` when no release has been made.

``vcs_commits_since_last_release``
Number of commits since the last release.
Is missing when no release has been made.

``vcs_last_committer_date``
Date of last commit in VCS.
4 changes: 2 additions & 2 deletions docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Checkout the following example to get started:

.. code-block:: console
curl --silent https://api.debops.org/roles/debops.unattended_upgrades.json | jq '.'
curl https://api.debops.org/v0/role/debops.unattended_upgrades.json -k | jq '.'
You can also open that URL in your browser via HTTP GET:
https://api.debops.org/roles/debops.unattended_upgrades.json
https://api.debops.org/v0/role/debops.unattended_upgrades.json

But note that the encoded JSON object is not prettified.
7 changes: 4 additions & 3 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ internally by the project but can also be used by third parties.

API URL: https://api.debops.org/

Used by
-------
Example usages
--------------

* https://github.com/debops/debops.github.io/
* https://github.com/debops/debops.github.io
* https://github.com/debops/docs

..
Local Variables:
Expand Down
1 change: 1 addition & 0 deletions tests/api_data/v0/role/debops.ansible.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ci_badge_url": "https://api.travis-ci.org/debops/ansible-role-ansible.png", "ci_url": "https://travis-ci.org/debops/ansible-role-ansible", "clone_url": "https://github.com/debops/ansible-role-ansible.git", "galaxy_url": "https://galaxy.ansible.com/debops/ansible", "issue_url": "https://github.com/debops/ansible-role-ansible/issues", "normalized_role_name": "role-ansible", "pr_url": "https://github.com/debops/ansible-role-ansible/pulls", "role_format_version": "0.1.0", "role_name": "ansible", "role_owner": "debops", "test_suite_url": "https://github.com/debops/test-suite/tree/master/ansible-role-ansible", "vcs_url": "https://github.com/debops/ansible-role-ansible"}
2 changes: 1 addition & 1 deletion tests/api_data/v0/role/debops.unattended_upgrades.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"authors": [{"name": "Maciej Delmanowski", "nick": "drybjed"}, {"name": "Robin Schneider", "nick": "ypid"}], "ci_badge_url": "https://api.travis-ci.org/debops/ansible-unattended_upgrades.png", "ci_url": "https://travis-ci.org/debops/ansible-unattended_upgrades", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "description": "Configure unattended upgrades on Debian-based hosts", "docs_format_version": "0.2.0", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "galaxy_url": "https://galaxy.ansible.com/debops/unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "license": "GPL-3.0", "min_ansible_version": "2.0.0", "platforms": [{"name": "Ubuntu", "versions": ["precise", "quantal", "raring", "saucy", "trusty"]}, {"name": "Debian", "versions": ["wheezy", "jessie"]}], "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "role_name": "unattended_upgrades", "role_owner": "debops", "tags": ["apt", "system", "security"], "test_suite_url": "https://github.com/debops/test-suite/tree/master/ansible-unattended_upgrades", "vcs_last_committer_date": "1970-01-01 00:00:00 +0000", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades", "version": "0.0.0"}
{"authors": [{"name": "Maciej Delmanowski", "nick": "drybjed"}, {"name": "Robin Schneider", "nick": "ypid"}], "ci_badge_url": "https://api.travis-ci.org/debops/ansible-unattended_upgrades.png", "ci_url": "https://travis-ci.org/debops/ansible-unattended_upgrades", "clone_url": "https://github.com/debops/ansible-unattended_upgrades.git", "description": "Configure unattended upgrades on Debian-based hosts", "docs_url": "http://docs.debops.org/en/latest/ansible/roles/ansible-unattended_upgrades/docs/index.html", "galaxy_url": "https://galaxy.ansible.com/debops/unattended_upgrades", "issue_url": "https://github.com/debops/ansible-unattended_upgrades/issues", "license": "GPL-3.0", "min_ansible_version": "2.0.0", "normalized_role_name": "unattended_upgrades", "platforms": [{"name": "Ubuntu", "versions": ["precise", "quantal", "raring", "saucy", "trusty"]}, {"name": "Debian", "versions": ["wheezy", "jessie"]}], "pr_url": "https://github.com/debops/ansible-unattended_upgrades/pulls", "role_format_version": "0.2.0", "role_name": "unattended_upgrades", "role_owner": "debops", "tags": ["apt", "system", "security"], "test_suite_url": "https://github.com/debops/test-suite/tree/master/ansible-unattended_upgrades", "vcs_last_committer_date": "1970-01-01 00:00:00 +0000", "vcs_url": "https://github.com/debops/ansible-unattended_upgrades", "version": "0.0.0"}
2 changes: 1 addition & 1 deletion tests/api_data/v0/role/debops.users.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"ci_badge_url": "https://api.travis-ci.org/debops/ansible-users.png", "ci_url": "https://travis-ci.org/debops/ansible-users", "clone_url": "https://github.com/debops/ansible-users.git", "docs_format_version": "0.1.0", "galaxy_url": "https://galaxy.ansible.com/debops/users", "issue_url": "https://github.com/debops/ansible-users/issues", "pr_url": "https://github.com/debops/ansible-users/pulls", "role_name": "users", "role_owner": "debops", "test_suite_url": "https://github.com/debops/test-suite/tree/master/ansible-users", "vcs_url": "https://github.com/debops/ansible-users"}
{"ci_badge_url": "https://api.travis-ci.org/debops/ansible-users.png", "ci_url": "https://travis-ci.org/debops/ansible-users", "clone_url": "https://github.com/debops/ansible-users.git", "galaxy_url": "https://galaxy.ansible.com/debops/users", "issue_url": "https://github.com/debops/ansible-users/issues", "normalized_role_name": "users", "pr_url": "https://github.com/debops/ansible-users/pulls", "role_format_version": "0.1.0", "role_name": "users", "role_owner": "debops", "test_suite_url": "https://github.com/debops/test-suite/tree/master/ansible-users", "vcs_url": "https://github.com/debops/ansible-users"}
2 changes: 1 addition & 1 deletion tests/api_data/v0/roles/count
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2
3
2 changes: 1 addition & 1 deletion tests/api_data/v0/roles/count:debops
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2
3
Loading

0 comments on commit 8028736

Please sign in to comment.