Skip to content

Commit

Permalink
Automatically create PR to update the list of available extensions doc (
Browse files Browse the repository at this point in the history
  • Loading branch information
derekbekoe authored Mar 28, 2018
1 parent b9e7878 commit 5f37d86
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dist: trusty
sudo: off
language: python
services:
- docker
install:
- pip install pylint flake8 requests -q
jobs:
Expand Down Expand Up @@ -29,6 +31,11 @@ jobs:
env: PURPOSE='IndexRefDocVerify'
script: ./scripts/ci/test_index_ref_doc.sh
python: 3.6
- stage: publish
python: 3.6
script: docker run --rm -e TRAVIS_BUILD_ID=$TRAVIS_BUILD_ID -e TRAVIS_REPO_SLUG=$TRAVIS_REPO_SLUG -e TRAVIS_COMMIT=$TRAVIS_COMMIT -e GH_TOKEN=$GH_TOKEN -e DOC_REPO_SLUG='Azure/azure-docs-cli-python' -v $PWD:/repo derekbekoe/az-avail-ext-doc-sync:0.1.1
env: PURPOSE='SyncAvailableExtensionsDoc'
if: repo = Azure/azure-cli-extensions and branch = master and type = push
fast_finish: true
allow_failures:
- env: PURPOSE='SourceTests'
7 changes: 7 additions & 0 deletions scripts/avail-ext-doc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM python:3.6

RUN pip install Jinja2==2.10 GitPython==2.1.8 PyGithub>=1.36

ADD main.py /

CMD python /main.py
17 changes: 17 additions & 0 deletions scripts/avail-ext-doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Docker image for automate PRs to https://github.com/azure/azure-docs-cli-python

Automatically create PR to update the document listing the available CLI extensions.

```bash
docker build . -t derekbekoe/az-avail-ext-doc-sync:0.1.0
```

To run locally:
```bash
docker run --rm -e TRAVIS_BUILD_ID=$TRAVIS_BUILD_ID -e TRAVIS_REPO_SLUG=$TRAVIS_REPO_SLUG -e TRAVIS_COMMIT=$TRAVIS_COMMIT -e GH_TOKEN=$GH_TOKEN -e DOC_REPO_SLUG=$DOC_REPO_SLUG -v $PWD:/repo derekbekoe/az-avail-ext-doc-sync:0.1.1
```

For example:
```bash
docker run --rm -e TRAVIS_BUILD_ID=1234 -e TRAVIS_REPO_SLUG='azure/azure-cli-extensions' -e TRAVIS_COMMIT=1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9ol0p -e GH_TOKEN=qwertyuiopasdfghjklzxcvbnm1234567890 -e DOC_REPO_SLUG='azure/azure-docs-cli-python' -v /Repos/azure-cli-extensions:/repo derekbekoe/az-avail-ext-doc-sync:0.1.1
```
62 changes: 62 additions & 0 deletions scripts/avail-ext-doc/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import print_function

import os
import sys
import subprocess
import logging

from git import Repo
from github import Github

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)

GH_TOKEN = os.environ.get('GH_TOKEN')
TRAVIS_BUILD_ID = os.environ.get('TRAVIS_BUILD_ID')
TRAVIS_REPO_SLUG = os.environ.get('TRAVIS_REPO_SLUG')
TRAVIS_COMMIT = os.environ.get('TRAVIS_COMMIT')
DOC_REPO_SLUG = os.environ.get('DOC_REPO_SLUG')

DOC_GEN_SCRIPT = os.path.join('repo', 'scripts', 'ci', 'available_extensions_doc.py')

REPO_CLI_DOCS = 'https://{}@github.com/{}'.format(GH_TOKEN, DOC_REPO_SLUG)
CLONED_CLI_DOCS = os.path.join(os.sep, 'doc-repo')
AVAILABLE_EXTENSIONS_DOC = os.path.join(CLONED_CLI_DOCS, 'docs-ref-conceptual', 'azure-cli-extensions-list.md')

def main():
doc_repo = Repo.clone_from(REPO_CLI_DOCS, CLONED_CLI_DOCS)
with open(AVAILABLE_EXTENSIONS_DOC, 'w') as f:
subprocess.call([sys.executable, DOC_GEN_SCRIPT], stdout=f)
doc_repo.git.add(doc_repo.working_tree_dir)
if not doc_repo.git.diff(staged=True):
logger.warning('No changes. Exiting')
return
github_con = Github(GH_TOKEN)
user = github_con.get_user()
doc_repo.git.config('user.email', user.email or 'azpycli@microsoft.com')
doc_repo.git.config('user.name', user.name)
logger.info('Cloned %s', CLONED_CLI_DOCS)
local_branch = doc_repo.create_head('az-auto-ext-doc-{}'.format(TRAVIS_BUILD_ID))
local_branch.checkout()
commit_url = 'https://github.com/{}/commit/{}'.format(TRAVIS_REPO_SLUG, TRAVIS_COMMIT)
commit_msg = 'Update CLI extensions available doc.\n Triggered by {} - ' \
'TRAVIS_BUILD_ID={}\n{}'.format(TRAVIS_REPO_SLUG, TRAVIS_BUILD_ID, commit_url)
doc_repo.index.commit(commit_msg)
doc_repo.git.push('origin', local_branch.name, set_upstream=True)
gh_repo = github_con.get_repo(DOC_REPO_SLUG)
gh_repo.create_pull(
title='Update CLI extensions available doc',
body=commit_msg,
head='{}:{}'.format(user.login, local_branch.name),
base='master')

if __name__ == '__main__':
main()

71 changes: 71 additions & 0 deletions scripts/ci/available_extensions_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from __future__ import print_function

import sys
import datetime
import collections
from pkg_resources import parse_version

from jinja2 import Template # pylint: disable=import-error

from util import get_index_data

DOC_TEMPLATE = """---
title: Available extensions for the Azure CLI 2.0
description: A complete list of the officially supported extensions for the Azure CLI 2.0.
author: derekbekoe
ms.author: debekoe
manager: routlaw
ms.date: {{ date }}
ms.topic: article
ms.prod: azure
ms.technology: azure
ms.devlang: azurecli
ms.service: multiple
---
# Available extensions for the Azure CLI 2.0
This article is a complete list of the available extensions for the Azure CLI 2.0 which are offered and supported by Microsoft.
The list of extensions is also available directly from the CLI. To get it, run [az extension list-available](/cli/azure/extension?view=azure-cli-latest#az-extension-list-available):
```azurecli
az extension list-available --output table
```
| Name | Version | Summary | Preview |
|------|---------|---------|---------|{% for extension in extensions %}
| [{{ extension.name }}]({{ extension.project_url }}) | {{ extension.version }} | {{ extension.desc }} | {{ extension.preview }} |{% endfor %}
"""


def get_extensions():
extensions = []
index_extensions = collections.OrderedDict(sorted(get_index_data()['extensions'].items()))
for _, exts in index_extensions.items():
# Get latest version
exts = sorted(exts, key=lambda c: parse_version(c['metadata']['version']), reverse=True)
extensions.append({
'name': exts[0]['metadata']['name'],
'desc': exts[0]['metadata']['summary'],
'version': exts[0]['metadata']['version'],
'project_url': exts[0]['metadata']['extensions']['python.details']['project_urls']['Home'],
'preview': 'Yes' if exts[0]['metadata'].get('azext.isPreview') else ''
})
return extensions


def main():
extensions = get_extensions()
now = datetime.datetime.now()
template = Template(DOC_TEMPLATE)
print(template.render(extensions=extensions, date=now.strftime("%m/%d/%Y")), file=sys.stdout)


if __name__ == '__main__':
main()

0 comments on commit 5f37d86

Please sign in to comment.