From 45358366f2a4d80a4e6136258783b13b54cbcc4e Mon Sep 17 00:00:00 2001 From: Florian Lanthaler Date: Wed, 20 Jan 2021 19:53:55 +0100 Subject: [PATCH] S-74365 CLI to Add directives to a project --- bin/ctm-set-project-directives | 6 +++ ctmcommands/flow/setprojectdirectives.py | 69 ++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 bin/ctm-set-project-directives create mode 100644 ctmcommands/flow/setprojectdirectives.py diff --git a/bin/ctm-set-project-directives b/bin/ctm-set-project-directives new file mode 100755 index 0000000..ebb8ef2 --- /dev/null +++ b/bin/ctm-set-project-directives @@ -0,0 +1,6 @@ +#!/usr/bin/env python +import ctmcommands.flow.setprojectdirectives + +if __name__ == '__main__': + cmd = ctmcommands.flow.setprojectdirectives.SetProjectDirectives() + cmd.main() diff --git a/ctmcommands/flow/setprojectdirectives.py b/ctmcommands/flow/setprojectdirectives.py new file mode 100644 index 0000000..0e558d9 --- /dev/null +++ b/ctmcommands/flow/setprojectdirectives.py @@ -0,0 +1,69 @@ +######################################################################### +# +# Copyright 2021 VersionOne +# All Rights Reserved. +# http://www.versionone.com +# +# +######################################################################### + +import json +import ctmcommands.cmd +from ctmcommands.param import Param + + +class SetProjectDirectives(ctmcommands.cmd.CSKCommand): + """ + Sets a projects' directives. This removes any already existing directives from the project. + + Example JSON document: + +``` +{ + "project_id": "5fda00c4c78ea1fe8779cf8e", + "directives": [ + { + "type": "jira_workitem_lookup", + "when": "always", + "details": { + "instance_name": "" + } + }, + { + "type": "assign_to_pipeline", + "when": "always", + "details": { + "project": "", + "group": "a group", + "definition": "a definition" + } + } + ] +} +``` + """ + + Description = """Sets a project's directives from a JSON document. + +Returns {"result": "success"} on success.""" + + API = 'set_project_directives' + Examples = '''''' + Options = [Param(name='directivesfile', short_name='d', long_name='directivesfile', + optional=False, ptype='string', + doc='A JSON document containing a project_id and directives.') + ] + + def main(self): + import os + + directives = None + if self.directivesfile: + fn = os.path.expanduser(self.directivesfile) + with open(fn, 'r') as f_in: + if not f_in: + print("Unable to open file [{}].".format(fn)) + directives = f_in.read() + + results = self.call_api(self.API, data=directives, verb='POST', content_type="application/json") + print(results)