Skip to content

Commit

Permalink
Code Review sample groovy script does not allow using custom rules (#66)
Browse files Browse the repository at this point in the history
* Code Review sample groovy script does not allow using custom rules
  • Loading branch information
jbyibm authored Dec 17, 2020
1 parent c8939f8 commit 0ddd67f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Pipeline/RunIDZCodeReview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ $DBB_HOME/bin/groovyz RunCodeReview.groovy --workDir /var/dbb/buildworkspace/zAp
options:
-w,--workDir <dir> Absolute path to the DBB build output directory
-cr,--crRulesFile (Optional) Absolute path of the rules file. If not provided, will look for it in the codereview.properties file
-ccr, --ccrRulesFile (Optional) Absolute path of the custom rules file. If not provided, will look for it in the codereview.properties file
-l,--logEncoding (Optional) Defines the Encoding for output files (JCL spool, reports), default UTF-8
-props,--properties (Optional) Absolute path to the codereview.properties file
-p,--preview (Optional) Preview JCL, do not submit it
Expand Down
88 changes: 71 additions & 17 deletions Pipeline/RunIDZCodeReview/RunCodeReview.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import java.nio.file.Path

/**
* This script invokes IDz CodeReview application via JCL based on the provided BuildReport.json
*
*
* usage: RunCodeReview.groovy --workDir <path-to-dbb-buildreport> [options]
*
* options:
Expand All @@ -23,11 +23,11 @@ import java.nio.file.Path
* -props,--properties (Optional) Absolute path to the codereview.properties file
* -p,--preview (Optional) Preview JCL, do not submit it
* -h,--help (Optional) Prints this message
*
*
* requires:
* codeview.properties file - externalizes the JCL jobcard, RuleFile and Mappings.
* If --properties not provided via cli, the script looks for it at the location of the script itself
*
* If --properties not provided via cli, the script looks for it at the location of the script itself
*
*/

def properties = parseInput(args)
Expand All @@ -45,7 +45,7 @@ def jsonOutputFile = new File("${properties.workDir}/BuildReport.json")

if(!jsonOutputFile.exists()){
println("** Build report data at $properties.workDir/BuildReport.json not found")
System.exit()
System.exit(1)
}

def buildReport= BuildReport.parse(new FileInputStream(jsonOutputFile))
Expand Down Expand Up @@ -89,7 +89,7 @@ else

println("** Create JCL Stream for IDZ Code Review")
String jobcard = properties.codereview_jobcard.replace("\\n", "\n")
JCLExec codeRev=createCodeReviewExec(jobcard, properties.codereview_crRulesFile, sources, steplib)
JCLExec codeRev=createCodeReviewExec(jobcard, properties.codereview_crRulesFile, properties.codereview_ccrRulesFile, sources, steplib)



Expand Down Expand Up @@ -125,35 +125,45 @@ else
System.exit(1)
}

println " Saving spool output to ${properties.workDir}"
println "** Saving spool output to ${properties.workDir}"
def logFile = new File("${properties.workDir}/CodeReviewSpool-${codeRev.getSubmittedJobId()}.txt")
codeRev.saveOutput(logFile, properties.logEncoding)

codeRev.getAllDDNames().each({ ddName ->
if (ddName == 'XML') {
def file = new File("${properties.workDir}/CodeReview${ddName}.xml")
codeRev.saveOutput(ddName, file, properties.logEncoding)
saveJobOutput(codeRev, ddName, file, properties.logEncoding)
}
if (ddName == 'JUNIT') {
def file = new File("${properties.workDir}/CodeReview${ddName}.xml")
codeRev.saveOutput(ddName, file, properties.logEncoding)
saveJobOutput(codeRev, ddName, file, properties.logEncoding)
}
if (ddName == 'CSV') {
def file = new File("${properties.workDir}/CodeReview${ddName}.csv")
codeRev.saveOutput(ddName, file, properties.logEncoding)
saveJobOutput(codeRev, ddName, file, properties.logEncoding)
}
})
}

}


/*
* Ensures backward compatibility
*/
def saveJobOutput ( JCLExec codeRev, String ddName, File file, String logEncoding) {
try {
codeRev.saveOutput(ddName, file, logEncoding, true)
} catch ( Exception ex ) {
println "*? Warning the output file $file\n*? will have an extra space at the beginning of each line.\n*? Updating DBB to the latest PTF with ASA control characters API for JCLExec is highly recommended."
codeRev.saveOutput(ddName, file, logEncoding)
}
}

/*
* CodeReviewExec - creates a JCLExec command for CodeReview
* TODO: Externalize SYSLIB
*/
def createCodeReviewExec(String jobcard, String ruleFile, List memberList,List steplib) {
def createCodeReviewExec(String jobcard, String ruleFile, String customRuleFile, List memberList,List steplib) {
// Execute JCL from a String value in the script
def jcl = jobcard
jcl += """\
Expand All @@ -166,9 +176,18 @@ def createCodeReviewExec(String jobcard, String ruleFile, List memberList,List s
steplib.eachWithIndex {it, index ->
if (index == 0 ) jcl+="//SYSLIB DD DISP=SHR,DSN=${it} \n"
else jcl+="// DD DISP=SHR,DSN=${it} \n"}
jcl +="""//RULES DD PATH='$ruleFile'
//LIST DD *
"""
if ( customRuleFile ) {
def lines = formatJCLPath("//CUSTRULE DD PATH='$customRuleFile'")
lines.each{
jcl += it + "\n"
}
}
def lines = formatJCLPath("//RULES DD PATH='$ruleFile'")
lines.each{
jcl += it + "\n"
}

jcl += "//LIST DD *\n"
def languageMapping = new PropertyMappings("codereview_languageMapping")
memberList.each{
hfsFile = CopyToPDS.createMemberName(it.getSource().getName())
Expand Down Expand Up @@ -200,6 +219,8 @@ def parseInput(String[] cliArgs){
def cli = new CliBuilder(usage: "RunCodeReview.groovy --workDir <path-to-dbb-buildreport> [options]", header: "Command Line Options:")
cli.w(longOpt:'workDir', args:1, argName:'dir', 'Absolute path to the DBB directory containing the BuildReport.json')
cli.props(longOpt:'properties', args:1, '(Optional) Absolute path to the codereview.properties file. If not provided, will look for it at the location of this script.')
cli.cr(longOpt:'crRulesFile', args:1, '(Optional) Absolute path of the rules file. If not provided, will look for it in the codereview.properties file.')
cli.ccr(longOpt:'ccrRulesFile', args:1, '(Optional) Absolute path of the custom rules file. If not provided, will look for it in the codereview.properties file.')
cli.l(longOpt:'logEncoding', args:1, '(Optional) Defines the Encoding for output files (JCL spool, reports), default UTF-8')
cli.p(longOpt:'preview', '(Optional) Preview JCL for CR, do not submit it')
cli.h(longOpt:'help', '(Optional) Prints this message.')
Expand Down Expand Up @@ -234,6 +255,12 @@ def parseInput(String[] cliArgs){
if (opts.w) properties.workDir = opts.w
properties.logEncoding = (opts.l) ? opts.l : "UTF-8"
properties.preview = (opts.p) ? 'true' : 'false'

if ( opts.cr )
properties.codereview_crRulesFile = opts.cr

if ( opts.ccr )
properties.codereview_ccrRulesFile = opts.ccr

// Validate required properties
try {
Expand All @@ -250,7 +277,7 @@ def parseInput(String[] cliArgs){
}

/**
*
*
*/
def createIncludePatterns(String includedFiles) {
List<PathMatcher> pathMatchers = new ArrayList<PathMatcher>()
Expand All @@ -266,7 +293,7 @@ def createIncludePatterns(String includedFiles) {
}

/**
*
*
*/
def matches(String file, List<PathMatcher> pathMatchers) {
def result = pathMatchers.any { matcher ->
Expand All @@ -278,3 +305,30 @@ def matches(String file, List<PathMatcher> pathMatchers) {
}
return result
}

def formatJCLPath(String line)
{
int len = line.length();
List<String> result = new ArrayList<String>();
int offset= 0;
int i = 0;
int j = 0;
while (i < len)
{
if ( offset == 71 ) {
result.add(line.substring(j,i));
j=i;
offset=16;
} else {
offset++;
}
i++;
}
if ( j < i )
result.add(line.substring(j,i));
for ( i=1; i < result.size(); i++ ) {
result.set(i, "// " + result.get(i));
}
return result;
}

20 changes: 10 additions & 10 deletions Pipeline/RunIDZCodeReview/codereview.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

#
# Job card, please use \n to indicate a line break and use \ to break the line in this property file
codereview_jobcard = //CODEREV JOB , \n\
// MSGCLASS=H,MSGLEVEL=(1,1),REGION=0M,COND=(16,LT) \n\
//* \n\
//PROCLIB JCLLIB ORDER=DBEHM.#CUST.PROCLIB \n
codereview_jobcard = //CODEREV JOB , \n\
// MSGCLASS=H,MSGLEVEL=(1,1),REGION=0M,COND=(16,LT)\n

#
## absolute path of the Rules file
codereview_crRulesFile=/var/dbb/integrations/idz-codereview/Rules.dat
## absolute path of the Rules file (can be overridden in the command line)
codereview_crRulesFile=

## (optional) absolute path of the Custom Rules file (can be overridden in the command line)
codereview_ccrRulesFile=

#
## Specify a comma separated list of file extensions to perform IDZ code review
## IDZ code review supports COBOL and PLI, Sample:codereview_includedFiles=**/*.cbl
codereview_includedFiles=**/*.cbl

#
## Specify a comma separated list of file extensions to perform IDZ code review
## IDZ code review supports COBOL and PLI, Sample:codereview_includedFiles=**/*.cbl
## Specify a comma separated list of file extensions to identify the PDS libraries for the Copybooks based on the file extension of the include files
## IDZ code review supports COBOL and PLI, Sample:codereview_includedIncludeFiles=**/*.cpy
codereview_includedIncludeFiles=**/*.cpy


#
## The generated CR JCL uses the the LIST DD, please. This mapping indicates to which language a given file extension is mapped
## The generated CR JCL uses the the LIST DD. This mapping indicates to which language a given file extension is mapped
## Specify the L=COBOL parameter for the Code Review List mapping
codereview_languageMapping=COBOL :: **/*.cbl
codereview_languageMapping=PLI :: **/*.pli

0 comments on commit 0ddd67f

Please sign in to comment.