Skip to content

Commit

Permalink
Merge pull request #30 from transtats/newpkgcmd
Browse files Browse the repository at this point in the history
transtats add --package cmd
  • Loading branch information
pnemade authored Jun 29, 2022
2 parents 08e93d8 + 278ac5f commit 36a79ee
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 41 deletions.
12 changes: 12 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ def mock_package_status_health_in_sync():
return mock_rep


def mock_package_add():
"""
package_add mock value
"""
mock_rep = Mock()
mock_rep.ok = True
mock_rep.json.return_value = {
"abrt": "Package added Successfully."
}
return mock_rep


def mock_coverage_rule():
"""
rule_coverage mock value
Expand Down
46 changes: 35 additions & 11 deletions tests/test_tscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,68 +60,92 @@ def test_server_version(self):

def test_package_status(self):
"""
transtats package <package>
transtats package stats <package>
"""
from tscli import entry_point

with patch('requests.get') as mock_request_get:
mock_request_get.return_value = \
test_data.mock_package_status()
runner = CliRunner()
result = runner.invoke(entry_point, ['package', 'systemd'])
result = runner.invoke(entry_point, ['package', 'status', 'systemd'])
self.assertEqual(result.exit_code, 0)
self.assertIn('systemd', result.output)
self.assertIn('Branch', result.output)
self.assertIn('percentage_calculated_on', result.output)

def test_package_status_exist(self):
"""
transtats package --exist <package>
transtats package stats --exist <package>
"""
from tscli import entry_point

with patch('requests.get') as mock_request_get:
mock_request_get.return_value = \
test_data.mock_package_status_exist()
runner = CliRunner()
result = runner.invoke(entry_point, ['package', '--exist',
'--json', 'gnome-shell'])
result = runner.invoke(entry_point, ['package', 'status', '--exist',
'gnome-shell', '--json'])
self.assertEqual(result.exit_code, 0)
self.assertIn('gnome-shell', result.output)
self.assertIn('true', result.output)

def test_package_status_health(self):
"""
transtats package --health <package>
transtats package stats --health <package>
"""
from tscli import entry_point

with patch('requests.get') as mock_request_get:
mock_request_get.return_value = \
test_data.mock_package_status_health()
runner = CliRunner()
result = runner.invoke(entry_point, ['package', '--health',
'--json', 'abrt'])
result = runner.invoke(entry_point, ['package', 'status', '--health',
'abrt', '--json'])
self.assertEqual(result.exit_code, 0)
self.assertIn('abrt', result.output)
self.assertIn('fedora-30', result.output)

def test_package_status_health_in_sync(self):
"""
transtats package --health <package>
transtats package stats --health <package>
"""
from tscli import entry_point

with patch('requests.get') as mock_request_get:
mock_request_get.return_value = \
test_data.mock_package_status_health_in_sync()
runner = CliRunner()
result = runner.invoke(entry_point, ['package', '--health',
'--json', 'authconfig'])
result = runner.invoke(entry_point, ['package', 'status', '--health',
'authconfig', '--json'])
self.assertEqual(result.exit_code, 0)
self.assertIn('authconfig', result.output)
self.assertIn('are in sync', result.output)

def test_package_add(self):
"""
transtats package add <package-name>
--upstream-url <upstream-url>
--transplatform-slug <platform-slug>
--release-stream <product>
"""
from tscli import entry_point

with patch('requests.post') as mock_request_post:
mock_request_post.return_value = \
test_data.mock_package_add()
runner = CliRunner()
result = runner.invoke(
entry_point, ['package', 'add', 'abrt',
'--upstream-url', 'https://github.com/abrt/abrt',
'--transplatform-slug', 'WLTEFED',
'--release-stream', 'fedora',
'--json']
)
self.assertEqual(result.exit_code, 0)
self.assertIn('abrt', result.output)
self.assertIn('Package added Successfully', result.output)

def test_coverage_rule(self):
"""
transtats coverage <graph_rule>
Expand Down
3 changes: 2 additions & 1 deletion tscli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tscli.config import get_config, get_config_item
from tscli.stats import commands as trans
from tscli.jobs import commands as jobs
from tscli.packages import commands as pkgs

APP_VERSION = "0.5.0"

Expand Down Expand Up @@ -50,7 +51,7 @@ def entry_point(ctx):


entry_point.add_command(common.version)
entry_point.add_command(trans.package)
entry_point.add_command(pkgs.package)
entry_point.add_command(trans.release)
entry_point.add_command(trans.coverage)
entry_point.add_command(jobs.job)
Empty file added tscli/packages/__init__.py
Empty file.
77 changes: 77 additions & 0 deletions tscli/packages/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright 2018 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import click
from tscli.restapi import ConsumeAPIs
from tscli.textoutput import TextOutputAPIs


@click.group()
def package():
"""Package related operations"""


@package.command()
@click.option(
'--server-url', envvar='TRANSTATS_SERVER', help="Transtats Server URL")
@click.option(
'--token', envvar='TOKEN', help="Transtats API token")
@click.option("--upstream-url", is_flag=False,
help="source repository url like github or gitlab")
@click.option("--transplatform-slug", is_flag=False,
help="platform slug like WLTEFED for fedora weblate")
@click.option("--release-stream", is_flag=False,
help="product like fedora")
@click.option(
'--json', is_flag=True, envvar='JSON_OUTPUT', help="Print in JSON format")
@click.argument('package_name')
@click.pass_obj
def add(app_context, server_url, token, package_name, upstream_url,
transplatform_slug, release_stream, json):
"""Add a package to Transtats server."""
api_obj = ConsumeAPIs(server_url or app_context.server_url) if json \
else TextOutputAPIs(server_url or app_context.server_url)

response = api_obj.add_package(package_name, upstream_url,
transplatform_slug, release_stream)
if isinstance(response, dict):
app_context.print_r(response)


@package.command()
@click.option(
'--server-url', envvar='TRANSTATS_SERVER', help="Transtats Server URL")
@click.option("--exist", is_flag=True,
help="Determine if the package exist in Transtats or not.")
@click.option("--health", is_flag=True,
help="Get package health")
@click.option(
'--json', is_flag=True, envvar='JSON_OUTPUT', help="Print in JSON format")
@click.argument('package-name')
@click.pass_obj
def status(app_context, server_url, package_name, exist, health, json):
"""Translation status of a package.
e.g. transtats package anaconda """
api_obj = ConsumeAPIs(server_url or app_context.server_url) if json \
else TextOutputAPIs(server_url or app_context.server_url)

if exist:
response = api_obj.package_status(package_name, exist=True, health=None)
elif health:
response = api_obj.package_status(package_name, exist=None, health=True)
else:
response = api_obj.package_status(package_name, exist=None, health=None)
if isinstance(response, dict):
app_context.print_r(response)
18 changes: 16 additions & 2 deletions tscli/restapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConsumeAPIs(object):
base_URL = None
middle_URL = "api"

ERR_JSON = {"Error": "Some thing unexpected happened."}
ERR_JSON = {"Error": "Something unexpected happened."}

def __init__(self, base_url):
"""
Expand Down Expand Up @@ -68,7 +68,7 @@ def _send_api(self, endpoint, payload):
except Exception:
return self.ERR_JSON

return response.json()
return response.json() if response.ok else self.ERR_JSON

@property
def server_version(self):
Expand Down Expand Up @@ -141,3 +141,17 @@ def job_run(self, job_type, package_name, build_system=None, build_tag=None,
return {"pkg_error": "Given package does not exists"}

return self._send_api(ENDPOINT, payload)

def add_package(self, package_name, upstream_url, transplatform_slug, release_stream):
"""
Create a new package at Transtats Server
"""
ENDPOINT = "/package/create"
payload = {
"package_name": package_name,
"upstream_url": upstream_url or "",
"transplatform_slug": transplatform_slug or "",
"release_streams": release_stream or ""
}

return self._send_api(ENDPOINT, payload)
27 changes: 0 additions & 27 deletions tscli/stats/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,6 @@
from tscli.textoutput import TextOutputAPIs


@click.command()
@click.option(
'--server-url', envvar='TRANSTATS_SERVER', help="Transtats Server URL")
@click.option("--exist", is_flag=True,
help="Determine if the package exist in Transtats or not.")
@click.option("--health", is_flag=True,
help="Get package health")
@click.option(
'--json', is_flag=True, envvar='JSON_OUTPUT', help="Print in JSON format")
@click.argument('package-name')
@click.pass_obj
def package(app_context, server_url, package_name, exist, health, json):
"""Translation status of a package.
e.g. transtats package anaconda """
api_obj = ConsumeAPIs(server_url or app_context.server_url) if json \
else TextOutputAPIs(server_url or app_context.server_url)

if exist:
response = api_obj.package_status(package_name, exist=True, health=None)
elif health:
response = api_obj.package_status(package_name, exist=None, health=True)
else:
response = api_obj.package_status(package_name, exist=None, health=None)
if isinstance(response, dict):
app_context.print_r(response)


@click.command()
@click.option(
'--server-url', envvar='TRANSTATS_SERVER', help="Transtats Server URL")
Expand Down
10 changes: 10 additions & 0 deletions tscli/textoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,13 @@ def job_run(self, job_type, package_name, build_system, build_tag,

print("\n".join([": ".join([key.title(), value]) for key, value in json_data.items()]))
return

def add_package(self, package_name, upstream_url, transplatform_slug, release_stream):
"""
Create a new package at Transtats Server
"""
json_data = self.raw_data.add_package(package_name, upstream_url,
transplatform_slug, release_stream)
for k, v in json_data.items():
print("{}: {}".format(k, v))
return

0 comments on commit 36a79ee

Please sign in to comment.