From c3e1d58b5eec95d4eab41a36556d406f07e368fb Mon Sep 17 00:00:00 2001 From: Mathieu Cartoixa Date: Tue, 4 Jun 2024 15:04:07 +0200 Subject: [PATCH] Add references property to the force-data-tree-import task --- .github/workflows/build.yml | 95 +++++++++++++++++++ .travis.yml | 39 -------- README.md | 8 +- azure-pipelines.yml | 38 -------- build.sh | 2 +- build/{.bashrc => setenv.sh} | 2 +- docs/force-data-tree-import.html | 5 + map_drive.cmd | 16 ++++ .../ant/sfdx/force/data/tree/ImportTask.java | 26 ++++- .../sfdx/force/data/tree/ImportTaskTest.java | 6 ++ .../ant/sfdx/force/data/tree/import.xml | 2 +- 11 files changed, 151 insertions(+), 88 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .travis.yml delete mode 100644 azure-pipelines.yml rename build/{.bashrc => setenv.sh} (98%) create mode 100644 map_drive.cmd diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..9c65142 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,95 @@ +name: build + +on: + push: + paths-ignore: + - '.vscode/**' + - '.editorconfig' + - '**.md' + +jobs: + build: + runs-on: ubuntu-latest + env: + PACKAGE_VERSION: 1.2.${{ github.run_number }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up JDK 8 + uses: actions/setup-java@v3 + with: + java-version: 8 + java-package: jdk + distribution: temurin + - name: Cache binary dependencies + uses: actions/cache@v3 + with: + path: | + .tmp/* + .tmp/cloc.pl + !.tmp/*.gz + !.tmp/*.xz + !.tmp/*.zip + key: bin-${{ hashFiles('build/versions.env') }} + - name: Cache Ivy dependencies + uses: actions/cache@v3 + with: + path: ivy + key: ivy-${{hashFiles('ivy.xml')}} + - name: Prepare build environment + shell: bash + run: | + source build/setenv.sh + # Only way to share environment variables between steps... + echo "ANT_HOME=$ANT_HOME" >> $GITHUB_ENV + echo "PMD_HOME=$PMD_HOME" >> $GITHUB_ENV + echo "$ANT_HOME/bin" >> $GITHUB_PATH + + mkdir -p ivy + if [ ! -f ivy/ivy.jar ]; then wget -nv -O ivy/ivy.jar https://repo1.maven.org/maven2/org/apache/ivy/ivy/$_IVY_VERSION/ivy-$_IVY_VERSION.jar; fi + java -jar ivy/ivy.jar -retrieve "ivy/lib/[conf]/[artifact].[ext]" -confs build + - name: Build + shell: bash + run: | + ant -noclasspath -nouserlib -noinput -logger org.apache.tools.ant.listener.AnsiColorLogger -Dverbosity=debug -f build.xml release + - name: JUnit Tests Report + if: success() || failure() + uses: dorny/test-reporter@v1 + with: + name: JUnit Tests + path: tmp/junit-results.xml + reporter: java-junit + fail-on-error: true + - name: Archive artifacts + uses: actions/upload-artifact@v4 + with: + name: artifacts + path: | + tmp/out/bin/**.* + - name: Archive logs + if: ${{ always() }} + uses: actions/upload-artifact@v4 + with: + name: logs + path: | + build.log + retention-days: 2 + - name: Archive results + if: ${{ always() }} + uses: actions/upload-artifact@v4 + with: + name: results + path: | + tmp/junit-results + tmp/*-results.html + tmp/*-results.xml + retention-days: 8 + - name: Create release + if: startsWith(github.ref, 'refs/tags/') + uses: softprops/action-gh-release@v2 + with: + draft: true + files: | + tmp/out/bin/**.* diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ea021cf..0000000 --- a/.travis.yml +++ /dev/null @@ -1,39 +0,0 @@ -language: java -jdk: - - oraclejdk8 -dist: trusty -sudo: required -cache: - directories: - - .tmp/ - - ivy/ - - $HOME/.ivy2/ - -env: - global: - - PACKAGE_VERSION=1.1.$TRAVIS_BUILD_NUMBER - - CODECOV_TOKEN=7eb11ac4-c9b4-45d3-bbb2-a34c0dd7b723 - -install: - - . build/.bashrc - - # Apache Ivy - - if [ ! -d ivy ]; then mkdir ivy; fi - - if [ ! -f ivy/ivy.jar ]; then wget -nv -O ivy/ivy.jar https://repo1.maven.org/maven2/org/apache/ivy/ivy/$_IVY_VERSION/ivy-$_IVY_VERSION.jar; fi - - $JAVA_HOME/bin/java -jar ivy/ivy.jar -retrieve "ivy/lib/[conf]/[artifact].[ext]" - -script: - - $ANT_HOME/bin/ant -noclasspath -nouserlib -noinput -lib "ivy/lib/test" -logger org.apache.tools.ant.listener.AnsiColorLogger -f build.xml release - -after_success: - - bash <(curl -s https://codecov.io/bash) -f tmp/jacoco-results.xml - -deploy: - provider: releases - api_key: - secure: wShPQnxchn5rc99eRCrGZ034/nhTixhTKNyz+QnP0v+FSlnA0bj/QhOtnaBkLDcagyE2662FKoLDPkghq4TWBjKpkFyTgaplCA3kFH473b8uNPCCa1WpJnDTpy5atkxK/E2V27E+TYGJSKz43+lw/7wNHnEmH+6boqudztwcvRHf5ibD5NRrZLuuz1hXCWNw97C2xMayTjawLRyV5/EgTF4TzetxLeulygn0K1gt8tCKx2jgC6i1qxuD8Ed7/y0ka4dDck5uC+XQfYr/rrsldE4eVhKorw9Z6pZz/UjQjFaF5g2NtwjhME5Pp7Drofetx+Mr5vrTJWSvpHSTOldijAymNGIlRORFYNKNfiuyD4RdeymtU7nvxkfMtxV6w0b9Q3D5L48ajUXRdCcF/TG/MyVjYgVf3oV2UMqi4E+NoWxCU3Vq/r0anNW0AijOtaoU6ygJcu8zthDypXJq/RbrmEqJsy/Sfr+VFCN0LJdTOkm8WBB5ni7ukfpA0lKiSeyNdokSjh/tFuHc/STdjWyyS4CUZ7b4Cefy3eq1pNXTJTvwVSrmM0UHoSC1qITJMegGNE2zY10F29hSXWnlC2g4gwZFQ2YZDQcfeM8ZjWsU+WxQ7GawfQzrAIhVJjauFX7cTCJoJYoytkAe69GwqcIpDk9uDyexRq6kqS6aFNI+Q5Q= - file_glob: true - file: tmp/out/bin/* - skip_cleanup: true - on: - tags: true diff --git a/README.md b/README.md index ca718fc..ddb0b19 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # ant-sfdx -[![Build status](https://travis-ci.org/mcartoixa/ant-sfdx.svg?branch=master)](https://travis-ci.org/mcartoixa/ant-sfdx) -[![Build Status](https://dev.azure.com/mcartoixa/ant-sfdx/_apis/build/status/ant-sfdx-CI)](https://dev.azure.com/mcartoixa/ant-sfdx/_build/latest?definitionId=1) +[![Build status](https://github.com/mcartoixa/ant-sfdx/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/mcartoixa/ant-sfdx/actions/workflows/build.yml) [![Code coverage](https://codecov.io/gh/mcartoixa/ant-sfdx/branch/master/graph/badge.svg)](https://codecov.io/gh/mcartoixa/ant-sfdx) -[![BCH compliance](https://bettercodehub.com/edge/badge/mcartoixa/ant-sfdx?branch=master)](https://bettercodehub.com/) Ant tasks that encapsulate the Salesforce DX CLI @@ -35,8 +33,8 @@ But if you can handle it, this project allows you to: * with native JSON support in Ant [scripts](http://ant.apache.org/manual/Tasks/script.html) if necessary. * easily integrate third-party Java based tools like [PMD](https://pmd.github.io/) or [ApexDoc](https://github.com/SalesforceFoundation/ApexDoc). * read your build more easily (if you can accept [the angle bracket tax](https://blog.codinghorror.com/xml-the-angle-bracket-tax/)). What's more readable: - * `sfdx force:org:create -v HubOrg -s -f config/project-scratch-def.json -a ciorg --wait 2` ? - * or `` ? + * `sfdx force:org:create -v HubOrg -s -f config/project-scratch-def.json -a ciorg --wait 2`? + * or ``? ## Usage diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index b2d87fd..0000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,38 +0,0 @@ -pool: - # cf. https://github.com/actions/virtual-environments/blob/master/images/win/Windows2019-Readme.md - vmImage: 'windows-2019' - -steps: -- task: BatchScript@1 - inputs: - filename: build\SetEnv.bat - arguments: /useCurrentJavaHome - modifyEnvironment: True - workingFolder: $(Build.Repository.LocalPath) -- script: | - IF NOT EXIST ivy MKDIR ivy - PUSHD ivy - IF NOT EXIST ivy.jar ( - powershell.exe -NoLogo -NonInteractive -ExecutionPolicy ByPass -Command "& { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest https://repo1.maven.org/maven2/org/apache/ivy/ivy/$Env:_IVY_VERSION/ivy-$Env:_IVY_VERSION.jar -OutFile ivy.jar; }" - IF ERRORLEVEL 1 GOTO END_ERROR - ) - POPD - "%JAVA_HOME%\bin\java.exe" -jar ivy\ivy.jar -retrieve "ivy\lib\[conf]\[artifact].[ext]" - displayName: Install and execute Ivy - -- task: Ant@1 - inputs: - buildFile: 'build.xml' - options: -noclasspath -nouserlib -noinput -lib ivy/lib/test -Dverbosity=verbose - targets: release - publishJUnitResults: true - testResultsFiles: '$(Build.Repository.LocalPath)\tmp\obj\test\results\**\TEST-*.xml' - antHomeDirectory: '$(ANT_HOME)' - jdkVersionOption: 1.8 - -- task: PublishCodeCoverageResults@1 - inputs: - codeCoverageTool: 'jaCoCo' - summaryFileLocation: '$(Build.Repository.LocalPath)\tmp\jacoco-results.xml' - pathToSources: '$(Build.Repository.LocalPath)\src\main' - reportDirectory: '$(Build.Repository.LocalPath)\tmp\jacoco-results' diff --git a/build.sh b/build.sh index 9920e9e..ebf3de7 100755 --- a/build.sh +++ b/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -source build/.bashrc +source build/setenv.sh diff --git a/build/.bashrc b/build/setenv.sh similarity index 98% rename from build/.bashrc rename to build/setenv.sh index 30b48d0..5f69c0d 100755 --- a/build/.bashrc +++ b/build/setenv.sh @@ -29,7 +29,7 @@ esac #Ant export ANT_HOME=$(pwd)/.tmp/apache-ant-$_ANT_VERSION -if [ ! -f $_ANT_HOME/bin/ant ]; then +if [ ! -f $ANT_HOME/bin/ant ]; then wget -nv $_wget_interactive_options -O .tmp/apache-ant-$_ANT_VERSION-bin.tar.gz https://archive.apache.org/dist/ant/binaries/apache-ant-$_ANT_VERSION-bin.tar.gz tar -xzvf .tmp/apache-ant-$_ANT_VERSION-bin.tar.gz -C .tmp fi diff --git a/docs/force-data-tree-import.html b/docs/force-data-tree-import.html index 9f332de..d8d2fca 100644 --- a/docs/force-data-tree-import.html +++ b/docs/force-data-tree-import.html @@ -52,6 +52,11 @@

Parameters

A username or alias for the target org. No + + referencesproperty + The name of a property in which the references of the imported data should be stored. Subproperties are named according to the (lowercased) references ids of the imported data. + No + diff --git a/map_drive.cmd b/map_drive.cmd new file mode 100644 index 0000000..5bfc5a0 --- /dev/null +++ b/map_drive.cmd @@ -0,0 +1,16 @@ +@ECHO OFF +::-------------------------------------------------------------------- +:: Usage: "mad_drive " +:: +:: - Optional. W: by default. +::-------------------------------------------------------------------- + +SETLOCAL +SET MAPPED_DRIVE_NAME=%~1 +IF "x%MAPPED_DRIVE_NAME%x"=="xx" SET MAPPED_DRIVE_NAME=W: + +NET USE %MAPPED_DRIVE_NAME% /DELETE +SUBST %MAPPED_DRIVE_NAME% /D >NUL +SUBST %MAPPED_DRIVE_NAME% "%~dp0." + +ENDLOCAL diff --git a/src/main/com/mcartoixa/ant/sfdx/force/data/tree/ImportTask.java b/src/main/com/mcartoixa/ant/sfdx/force/data/tree/ImportTask.java index f2cb1b3..82cfadf 100644 --- a/src/main/com/mcartoixa/ant/sfdx/force/data/tree/ImportTask.java +++ b/src/main/com/mcartoixa/ant/sfdx/force/data/tree/ImportTask.java @@ -23,6 +23,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; @@ -41,6 +42,7 @@ * * @author Mathieu Cartoixa */ +@SuppressWarnings("PMD.LongVariable") public class ImportTask extends SfdxTask { /* default */ class JsonParser extends SfdxTask.JsonParser { @@ -49,7 +51,7 @@ public class ImportTask extends SfdxTask { super(); } - @SuppressWarnings("PMD.EmptyCatchBlock") + @SuppressWarnings({"PMD.EmptyCatchBlock", "PMD.LongVariable"}) @Override protected void doParse(final JSONObject json) { final JSONArray result = json.optJSONArray("result"); @@ -58,9 +60,17 @@ protected void doParse(final JSONObject json) { final Object value = result.get(i); if (value instanceof JSONObject) { final JSONObject object = (JSONObject) value; + final String refId = object.getString("refId"); + final String id = object.getString("id"); + final String referencesProperty = ImportTask.this.getReferencesProperty(); + if (referencesProperty != null && !referencesProperty.isEmpty()) { + ImportTask.this.getProject().setNewProperty(referencesProperty + "." + refId.toLowerCase(Locale.ROOT), id); + } + final String message = String.format( - "%s imported.", - object.getString("refId") + "%s imported (%s).", + refId, + id ); this.log(message, Project.MSG_INFO); } @@ -151,6 +161,10 @@ public void setPlan(final File plan) { } } + public void setReferencesProperty(final String referencesProperty) { + this.referencesProperty = referencesProperty; + } + public void setTargetUserName(final String userName) { if (userName != null && !userName.isEmpty()) { getCommandline().createArgument().setValue("-u"); @@ -210,6 +224,12 @@ protected ISfdxJsonParser getParser() { return new ImportTask.JsonParser(); } + @SuppressWarnings("PMD.DefaultPackage") + /* default */ String getReferencesProperty() { + return this.referencesProperty; + } + private transient final List fileSets = new ArrayList<>(); + private transient String referencesProperty; private transient Union resources = null; } diff --git a/src/test/com/mcartoixa/ant/sfdx/force/data/tree/ImportTaskTest.java b/src/test/com/mcartoixa/ant/sfdx/force/data/tree/ImportTaskTest.java index 3223a89..abeafaf 100644 --- a/src/test/com/mcartoixa/ant/sfdx/force/data/tree/ImportTaskTest.java +++ b/src/test/com/mcartoixa/ant/sfdx/force/data/tree/ImportTaskTest.java @@ -49,6 +49,12 @@ public void executeShouldSetStatusProperty() { Assert.assertEquals("Status property should be set", "0", buildRule.getProject().getProperty("execute.status")); } + @Test + public void executeShouldSetReferencesProperties() { + buildRule.executeTarget("execute"); + Assert.assertEquals("References properties should be set", "a063N000004OhI8QAK", buildRule.getProject().getProperty("execute.references.object__cref2")); + } + @Test public void executeShouldAddJsonArgument() { buildRule.executeTarget("execute"); diff --git a/src/test/com/mcartoixa/ant/sfdx/force/data/tree/import.xml b/src/test/com/mcartoixa/ant/sfdx/force/data/tree/import.xml index dfe6b04..a7de0a6 100644 --- a/src/test/com/mcartoixa/ant/sfdx/force/data/tree/import.xml +++ b/src/test/com/mcartoixa/ant/sfdx/force/data/tree/import.xml @@ -13,7 +13,7 @@ - +