Skip to content

API for loading decomposed productions #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.9.1] - Unreleased
## [2.10.0] - Unreleased

### Added
- LoadProductionsFromDirectory method to help custom deployment scripts load decomposed productions from the repository (#670)

### Fixed
- Fixed not showing warnings on Studio (#660)
Expand Down
8 changes: 8 additions & 0 deletions cls/SourceControl/Git/API.cls
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ ClassMethod BaselineProductions()
do ##class(SourceControl.Git.Util.Production).BaselineProductions()
}

/// Given the path to a directory that contains production items, this method will import them all
/// and delete any custom items from the production configuration that do not exist in the directory.
/// This method may be called on a namespace that is not configured with Embedded Git for source control.
ClassMethod LoadProductionsFromDirectory(pDirectoryName, Output pFailedItems) As %Status
{
return ##class(SourceControl.Git.Util.Production).LoadProductionsFromDirectory(pDirectoryName, .pFailedItems)
}

}
72 changes: 72 additions & 0 deletions cls/SourceControl/Git/Util/Production.cls
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,76 @@ ClassMethod ItemIsPTD(externalName) As %Boolean
return 0
}

/// Given the path to a directory that contains production items, this method will import them all
/// and delete any custom items from the production configuration that do not exist in the directory.
/// This method may be called on a namespace that is not configured with Embedded Git for source control.
ClassMethod LoadProductionsFromDirectory(pDirectoryName, Output pFailedItems) As %Status
{
set st = $$$OK
try {
set rs = ##class(%File).FileSetFunc(pDirectoryName,,,1)
throw:rs.%SQLCODE<0 ##class(%Exception.SQL).CreateFromSQLCODE(rs.%SQLCODE,rs.%Message)
kill itemsOnDisk
while rs.%Next() {
continue:rs.Type'="D"
set rs2 = ##class(%File).FileSetFunc(rs.Name,"*.xml",,0)
throw:rs2.%SQLCODE<0 ##class(%Exception.SQL).CreateFromSQLCODE(rs2.%SQLCODE,rs2.%Message)
while rs2.%Next() {
set filePath = rs2.Name
$$$ThrowOnError(##class(SourceControl.Git.Production).ParseExternalName(filePath,.internalName))
set itemName = "", itemClassName = "", productionName = ""
do ##class(SourceControl.Git.Production).ParseInternalName(internalName,,,.itemName,.itemClassName,.productionName)
quit:productionName=""
if (itemName'="") && (itemClassName'="") {
set itemsOnDisk(productionName, itemName, itemClassName) = 1
}
// if production does not exist, create it
if '$isobject(##class(Ens.Config.Production).%OpenId(productionName)) {
$$$ThrowOnError(##class(SourceControl.Git.Production).CreateProduction(productionName))
}
set st = ##class(SourceControl.Git.Production).ImportPTD(filePath, productionName)
if $$$ISERR(st) {
set pFailedItems(filePath) = st
}
}
}
// handle deletes by iterating through XDATA of each production class. for every config item that is not in itemsOnDisk, delete it.
set key = $order(itemsOnDisk(""))
while (key '= "") {
set classDef = ##class(%Dictionary.ClassDefinition).%OpenId(key)
if $isobject(classDef) {
set productionXData = $$$NULLOREF
for i=1:1:classDef.XDatas.Count() {
set xdata = classDef.XDatas.GetAt(i)
if xdata.Name = "ProductionDefinition" {
set productionXData = xdata
quit
}
}
if $isobject(productionXData) {
$$$ThrowOnError(##class(%XML.XPATH.Document).CreateFromStream(productionXData.Data,.xdoc))
$$$ThrowOnError(xdoc.EvaluateExpression("/Production","Item/@Name | Item/@ClassName",.results))
for i=1:2:results.Count() {
set itemName = results.GetAt(i).Value
set itemClassName = results.GetAt(i+1).Value
if (itemName'="") && (itemClassName'="") && '$get(itemsOnDisk(key,itemName, itemClassName)) {
write !, "Removing item from production ", key, ": ", itemName, ":", itemClassName
set internalName = ##class(SourceControl.Git.Production).CreateInternalName(key,itemName, itemClassName)
set st = ##class(SourceControl.Git.Production).RemoveItem(internalName)
if $$$ISERR(st) {
set pFailedItems(itemName, itemClassName) = st
}
}
}
}
}
set key = $order(itemsOnDisk(key))
}
} catch err {
set st = err.AsStatus()
}
if $data(pFailedItems) set st = $$$ADDSC($$$ERROR($$$GeneralError,"Some items failed to deploy."),st)
return st
}

}
2 changes: 1 addition & 1 deletion module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Document name="git-source-control.ZPM">
<Module>
<Name>git-source-control</Name>
<Version>2.9.1</Version>
<Version>2.10.0</Version>
<Description>Server-side source control extension for use of Git on InterSystems platforms</Description>
<Keywords>git source control studio vscode</Keywords>
<Packaging>module</Packaging>
Expand Down
16 changes: 16 additions & 0 deletions test/UnitTest/SourceControl/Git/Util/Production.cls
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ Method TestItemIsPTD()
do $$$AssertTrue(##class(SourceControl.Git.Util.Production).ItemIsPTD("ptd2\test.xml"))
}

Method TestLoadProductionsFromDirectory()
{
// load a production from a class file under resources
set packageRoot = ##class(SourceControl.Git.PackageManagerContext).ForInternalName("git-source-control.zpm").Package.Root
$$$ThrowOnError($System.OBJ.Load(packageRoot_"test/_resources/cls/UnitTest/SampleProduction.cls","ck"))
// call LoadProductionsFromDirectory on a directory under resources/ptd
do $$$AssertStatusOK(##class(SourceControl.Git.Util.Production).LoadProductionsFromDirectory(packageRoot_"test/_resources/ptd"))
// confirm items were deleted and added
set itemA = ##class(Ens.Config.Production).OpenItemByConfigName("UnitTest.SampleProduction||a")
do $$$AssertNotTrue($isobject(itemA),"item a was deleted")
set itemB = ##class(Ens.Config.Production).OpenItemByConfigName("UnitTest.SampleProduction||b")
do $$$AssertEquals(itemB.Settings.GetAt(1).Value,71)
set itemB = ##class(Ens.Config.Production).OpenItemByConfigName("UnitTest.SampleProduction||c")
do $$$AssertTrue($isobject(itemB),"item a was created")
}

Method OnBeforeAllTests() As %Status
{
merge ..Mappings = @##class(SourceControl.Git.Utils).MappingsNode()
Expand Down
16 changes: 16 additions & 0 deletions test/_resources/cls/UnitTest/SampleProduction.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Class UnitTest.SampleProduction Extends Ens.Production
{

XData ProductionDefinition
{
<Production Name="UnitTest.SampleProduction" LogGeneralTraceEvents="false">
<Item Name="a" Category="" ClassName="Ens.Activity.Operation.Local" PoolSize="1" Enabled="true" Foreground="false" Comment="" LogTraceEvents="false" Schedule="">
<Setting Target="Host" Name="RecordStatsInterval">60</Setting>
</Item>
<Item Name="b" Category="" ClassName="Ens.Activity.Operation.Local" PoolSize="1" Enabled="true" Foreground="false" Comment="" LogTraceEvents="false" Schedule="">
<Setting Target="Host" Name="RecordStatsInterval">61</Setting>
</Item>
</Production>
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="IRIS" version="26">
<Project name="ProductionSettings-UnitTest_SampleProduction" LastModified="1841-01-01 00:00:00.0000000">
<Items>
<ProjectItem name="EnsExportNotes.ProductionSettings-UnitTest_SampleProduction.PTD" type="PTD"></ProjectItem>
<ProjectItem name="ProductionSettings:UnitTest.SampleProduction.PTD" type="PTD"></ProjectItem>
</Items>
</Project>


<Document name="EnsExportNotes.ProductionSettings-UnitTest_SampleProduction.PTD"><ProjectTextDocument name="EnsExportNotes.ProductionSettings-UnitTest_SampleProduction" description="Export Notes for export ProductionSettings-UnitTest_SampleProduction">
<![CDATA[<Deployment>
<Creation>
<SourceProduction>UnitTest.SampleProduction</SourceProduction>
<UTC>1841-01-01 00:00:00.000</UTC>
</Creation>
<Notes>
</Notes>
<Contents>
<ExportProject>ProductionSettings-UnitTest_SampleProduction</ExportProject>
<Item num="1">ProductionSettings:UnitTest.SampleProduction.PTD</Item>
</Contents>
<ProductionClassInExport></ProductionClassInExport>
</Deployment>

]]></ProjectTextDocument>
</Document>

<Document name="ProductionSettings:UnitTest.SampleProduction.PTD"><ProjectTextDocument name="ProductionSettings:UnitTest.SampleProduction" description="Settings for production UnitTest.SampleProduction">
<![CDATA[<Production Name='UnitTest.SampleProduction' LogGeneralTraceEvents='false'/>
]]></ProjectTextDocument>
</Document></Export>
32 changes: 32 additions & 0 deletions test/_resources/ptd/UnitTest_SampleProduction/Stgs-b949C.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="IRIS" version="26">
<Project name="Settings-b" LastModified="1841-01-01 00:00:00.0000000">
<Items>
<ProjectItem name="EnsExportNotes.Settings-b.PTD" type="PTD"></ProjectItem>
<ProjectItem name="Settings:b.PTD" type="PTD"></ProjectItem>
</Items>
</Project>


<Document name="EnsExportNotes.Settings-b.PTD"><ProjectTextDocument name="EnsExportNotes.Settings-b" description="Export Notes for export Settings-b">
<![CDATA[<Deployment>
<Creation>
<SourceProduction>UnitTest.SampleProduction</SourceProduction>
<UTC>1841-01-01 00:00:00.000</UTC>
</Creation>
<Notes>
</Notes>
<Contents>
<ExportProject>Settings-b</ExportProject>
<Item num="1">Settings:b.PTD</Item>
</Contents>
<ProductionClassInExport></ProductionClassInExport>
</Deployment>

]]></ProjectTextDocument>
</Document>

<Document name="Settings:b.PTD"><ProjectTextDocument name="Settings:b" description="Settings for b in Production UnitTest.SampleProduction">
<![CDATA[<Item Name="b" Category="" ClassName="Ens.Activity.Operation.Local" PoolSize="1" Enabled="true" Foreground="false" Comment="" LogTraceEvents="false" Schedule=""><Setting Target="Host" Name="RecordStatsInterval">71</Setting></Item>
]]></ProjectTextDocument>
</Document></Export>
32 changes: 32 additions & 0 deletions test/_resources/ptd/UnitTest_SampleProduction/Stgs-c949C.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<Export generator="IRIS" version="26">
<Project name="Settings-c" LastModified="1841-01-01 00:00:00.0000000">
<Items>
<ProjectItem name="EnsExportNotes.Settings-c.PTD" type="PTD"></ProjectItem>
<ProjectItem name="Settings:c.PTD" type="PTD"></ProjectItem>
</Items>
</Project>


<Document name="EnsExportNotes.Settings-c.PTD"><ProjectTextDocument name="EnsExportNotes.Settings-c" description="Export Notes for export Settings-c">
<![CDATA[<Deployment>
<Creation>
<SourceProduction>UnitTest.SampleProduction</SourceProduction>
<UTC>1841-01-01 00:00:00.000</UTC>
</Creation>
<Notes>
</Notes>
<Contents>
<ExportProject>Settings-c</ExportProject>
<Item num="1">Settings:c.PTD</Item>
</Contents>
<ProductionClassInExport></ProductionClassInExport>
</Deployment>

]]></ProjectTextDocument>
</Document>

<Document name="Settings:c.PTD"><ProjectTextDocument name="Settings:c" description="Settings for c in Production UnitTest.SampleProduction">
<![CDATA[<Item Name="c" Category="" ClassName="Ens.Activity.Operation.Local" PoolSize="1" Enabled="false" Foreground="false" Comment="" LogTraceEvents="false" Schedule=""></Item>
]]></ProjectTextDocument>
</Document></Export>
Loading