Skip to content

Commit

Permalink
response ormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
kszucs committed Mar 10, 2020
1 parent 1b0b15d commit 4789ac5
Showing 1 changed file with 96 additions and 6 deletions.
102 changes: 96 additions & 6 deletions dev/archery/archery/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
# specific language governing permissions and limitations
# under the License.

import operator
import shlex
from pathlib import Path
from functools import partial

import click
import github
from ruamel.yaml import YAML

from .utils.crossbow import Crossbow
from .utils.git import Git
Expand Down Expand Up @@ -74,6 +77,81 @@ def parse_args(self, ctx, args):
return super().parse_args(ctx, args)


command = partial(click.command, cls=Command)
group = partial(click.group, cls=Group)


class CrossbowCommentFormatter:

_markdown_badge = '[![{title}]({badge})]({url})'

badges = {
'github': _markdown_badge.format(
title='Github Actions',
url='https://github.com/{repo}/actions?query=branch:{branch}',
badge=(
'https://github.com/{repo}/workflows/Crossbow/'
'badge.svg?branch={branch}'
),
),
'azure': _markdown_badge.format(
title='Azure',
url=(
'https://dev.azure.com/{repo}/_build/latest'
'?definitionId=1&branchName={branch}'
),
badge=(
'https://dev.azure.com/{repo}/_apis/build/status/'
'{repo_dotted}?branchName={branch}'
)
),
'travis': _markdown_badge.format(
title='TravisCI',
url='https://travis-ci.org/{repo}/branches',
badge='https://img.shields.io/travis/{repo}/{branch}.svg'
),
'circle': _markdown_badge.format(
title='CircleCI',
url='https://circleci.com/gh/{repo}/tree/{branch}',
badge=(
'https://img.shields.io/circleci/build/github'
'/{repo}/{branch}.svg'
)
),
'appveyor': _markdown_badge.format(
title='Appveyor',
url='https://ci.appveyor.com/project/{repo}/history',
badge='https://img.shields.io/appveyor/ci/{repo}/{branch}.svg'
)
}

def __init__(self, crossbow_repo):
self.crossbow_repo = crossbow_repo

def render(self, job):
url = 'https://github.com/{repo}/branches/all?query={branch}'
msg = f'Submitted crossbow builds: [{{repo}} @ {{branch}}]({url})\n'
msg += '\n|Task|Status|\n|----|------|'

tasks = sorted(job['tasks'].items(), key=operator.itemgetter(0))
for key, task in tasks:
branch = task['branch']

try:
template = self.badges[task['ci']]
badge = template.format(
repo=self.crossbow_repo,
repo_dotted=self.crossbow_repo.replace('/', '.'),
branch=branch
)
except KeyError:
badge = 'unsupported CI service `{}`'.format(task['ci'])

msg += f'\n|{key}|{badge}|'

return msg.format(repo=self.crossbow_repo, branch=job['branch'])


class CommentBot:

def __init__(self, name, handler, token=None):
Expand Down Expand Up @@ -147,10 +225,6 @@ def handle_review_comment(self, payload):
raise NotImplementedError()


command = partial(click.command, cls=Command)
group = partial(click.group, cls=Group)


@group(name='@ursabot')
@click.pass_context
def ursabot(ctx):
Expand Down Expand Up @@ -182,14 +256,30 @@ def submit(obj, task, group, dry_run):
See groups defined in arrow/dev/tasks/tests.yml
"""
args = []
args = ['--output-file', 'result.yaml']
for g in group:
args.extend(['-g', g])
for t in task:
args.append(t)

# clone crossbow
git = Git()
git.clone(obj['crossbow_repo'], 'crossbow')

# submit the crossbow tasks
xbow = Crossbow('arrow/dev/tasks/crossbow.py')
xbow.run('submit', *args)
xbow.run('submit', *args)

# parse the result yml describing the submitted job
yaml = YAML()
with Path('result.yml').open() as fp:
job = yaml.load(fp)

# render the response comment's content
formatter = CrossbowCommentFormatter(obj['crossbow_repo'])
response = formatter.render(job)

# send the response
obj['pull'].create_issue_comment(response)


0 comments on commit 4789ac5

Please sign in to comment.