Skip to content

Commit

Permalink
Merge pull request #1469 from dmach/tokens
Browse files Browse the repository at this point in the history
Improve working with tokens
  • Loading branch information
dmach authored Jan 16, 2024
2 parents de26a24 + 86c9127 commit 16158e4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
22 changes: 22 additions & 0 deletions behave/features/steps/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
38 changes: 38 additions & 0 deletions behave/features/token.feature
Original file line number Diff line number Diff line change
@@ -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>
"""
12 changes: 8 additions & 4 deletions osc/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ def do_showlinked(self, subcmd, opts, *args):
@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',
Expand All @@ -1673,7 +1673,7 @@ def do_token(self, subcmd, opts, *args):
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>]
"""
Expand All @@ -1688,6 +1688,8 @@ def do_token(self, subcmd, opts, *args):
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)
Expand Down Expand Up @@ -1716,12 +1718,14 @@ def do_token(self, subcmd, opts, *args):
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,
Expand Down

0 comments on commit 16158e4

Please sign in to comment.