1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-01 04:36:13 +01:00

Merge pull request #1469 from dmach/tokens

Improve working with tokens
This commit is contained in:
Daniel Mach 2024-01-16 09:11:31 +01:00 committed by GitHub
commit 16158e406a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 4 deletions

View File

@ -124,6 +124,28 @@ def step_impl(context):
raise AssertionError(f"Stdout is not:\n{expected_str}\n\nActual stdout:\n{found_str}")
@behave.step("stdout matches")
def step_impl(context):
expected = context.text.format(context=context).rstrip()
found = context.cmd_stdout.rstrip()
if re.match(expected, found, re.MULTILINE):
return
raise AssertionError(f"Stdout doesn't match:\n{expected}\n\nActual stdout:\n{found}")
@behave.step("I search '{pattern}' in stdout and store named groups in '{context_attribute}'")
def step_impl(context, pattern, context_attribute):
pattern = r"{}".format(pattern)
result = []
for match in re.finditer(pattern, context.cmd_stdout):
result.append(match.groupdict())
setattr(context, context_attribute, result)
@behave.step("stderr is")
def step_impl(context):
expected = context.text.format(context=context).rstrip().split("\n")

View File

@ -0,0 +1,38 @@
Feature: `osc token` command
Scenario: Run `osc token` with no arguments
When I execute osc with args "token"
Then stdout is
"""
<directory count="0"/>
"""
Scenario: Run `osc token --operation rebuild`
When I execute osc with args "token --create --operation rebuild test:factory test-pkgA"
Then stdout matches
"""
Create a new token
<status code="ok">
<summary>Ok</summary>
<data name="token">.*</data>
<data name="id">1</data>
</status>
"""
Given I execute osc with args "token"
And stdout matches
"""
<directory count="1">
<entry id="1" string=".*" kind="rebuild" description="" triggered_at="" project="test:factory" package="test-pkgA"/>
</directory>
"""
And I search 'string="(?P<token>[^"]+)' in stdout and store named groups in 'tokens'
When I execute osc with args "token --trigger {context.tokens[0][token]}"
Then stdout is
"""
Trigger token
<status code="ok">
<summary>Ok</summary>
</status>
"""

View File

@ -1659,7 +1659,7 @@ class Osc(cmdln.Cmdln):
@cmdln.option('-d', '--delete', metavar='TOKENID',
help='Delete a token')
@cmdln.option('-o', '--operation', metavar='OPERATION',
help='Default is "runservice", but "branch", "release", "rebuild", or "workflow" can also be used')
help="Operation associated with the token. Choices: runservice, branch, release, rebuild, workflow")
@cmdln.option('-t', '--trigger', metavar='TOKENSTRING',
help='Trigger the action of a token')
@cmdln.option('', '--scm-token', metavar='SCM_TOKEN',
@ -1673,7 +1673,7 @@ class Osc(cmdln.Cmdln):
usage:
osc token
osc token --create [--operation <OPERATION>] [<PROJECT> <PACKAGE>]
osc token --create --operation <OPERATION> [<PROJECT> <PACKAGE>]
osc token --delete <TOKENID>
osc token --trigger <TOKENSTRING> [--operation <OPERATION>] [<PROJECT> <PACKAGE>]
"""
@ -1688,6 +1688,8 @@ class Osc(cmdln.Cmdln):
url_path = ['person', conf.get_apiurl_usr(apiurl), 'token']
if opts.create:
if not opts.operation:
self.argparser.error("Please specify --operation")
if opts.operation == 'workflow' and not opts.scm_token:
msg = 'The --operation=workflow option requires a --scm-token=<token> option'
raise oscerr.WrongOptions(msg)
@ -1716,12 +1718,14 @@ class Osc(cmdln.Cmdln):
http_DELETE(url)
elif opts.trigger:
print("Trigger token")
operation = opts.operation or "runservice"
query = {}
if len(args) > 1:
query['project'] = args[0]
query['package'] = args[1]
url = makeurl(apiurl, ['trigger', operation], query)
if opts.operation:
url = makeurl(apiurl, ["trigger", opts.operation], query)
else:
url = makeurl(apiurl, ["trigger"], query)
headers = {
'Content-Type': 'application/octet-stream',
'Authorization': "Token " + opts.trigger,