2020
2121import { describe , expect , it } from 'bun:test' ;
2222import * as yaml from 'js-yaml' ;
23+ import { parseArgsStringToArgv } from 'string-argv' ;
2324
2425import {
25- CHECKOUT_ACTION ,
2626 generatePrContent ,
2727 generateTemplatePrContent ,
2828 generateWorkflow ,
2929 renderWorkflowTemplate ,
30- SCAN_ACTION ,
3130 validateWorkflowOptions ,
3231 validateWorkflowTemplate ,
3332} from '@/commands/admin/onboard-ci/github/templates.ts' ;
4443 SONAR_HOST_URL: \${{ vars.SERVER_URL }}
4544` ;
4645
47- function workflow ( triggerOn : 'main' | 'pr' | 'both' = 'both' ) {
46+ function workflow ( triggerOn : 'main' | 'pr' | 'both' = 'both' , defaultBranch = 'trunk' ) {
4847 return yaml . load (
49- generateWorkflow ( 'org:repo' , 'https://sonar.example.com/context' , 'trunk' , { triggerOn } ) ,
48+ generateWorkflow ( 'org:repo' , 'https://sonar.example.com/context' , defaultBranch , { triggerOn } ) ,
5049 ) as {
5150 on : Record < string , unknown > ;
5251 jobs : {
@@ -76,16 +75,23 @@ describe('generated GitHub workflow', () => {
7675 } ,
7776 ) ;
7877
78+ it . each ( [
79+ [ '!main' , '\\!main' ] ,
80+ [ 'release+stable' , 'release\\+stable' ] ,
81+ ] ) ( 'escapes default branch %s for literal matching' , ( branch , pattern ) => {
82+ expect ( workflow ( 'main' , branch ) . on . push ) . toEqual ( { branches : [ pattern ] } ) ;
83+ } ) ;
84+
7985 it ( 'sets Server attribution, pinned actions, full checkout and a secret reference' , ( ) => {
8086 const job = workflow ( ) . jobs . sonarqube ;
81- for ( const step of job . steps ) {
82- expect ( step . uses ) . toMatch ( / @ [ a - f 0 - 9 ] { 40 } $ / ) ;
83- }
8487 expect ( job [ 'runs-on' ] ) . toBe ( 'ubuntu-latest' ) ;
85- expect ( job . steps [ 0 ] ) . toEqual ( { uses : CHECKOUT_ACTION , with : { 'fetch-depth' : 0 } } ) ;
88+ expect ( job . steps [ 0 ] ) . toEqual ( {
89+ uses : expect . stringMatching ( / ^ a c t i o n s \/ c h e c k o u t @ [ a - f 0 - 9 ] { 40 } $ / ) ,
90+ with : { 'fetch-depth' : 0 } ,
91+ } ) ;
8692 expect ( job . steps [ 1 ] ) . toEqual ( {
8793 name : 'SonarQube scan' ,
88- uses : SCAN_ACTION ,
94+ uses : expect . stringMatching ( / ^ S o n a r S o u r c e \/ s o n a r q u b e - s c a n - a c t i o n @ [ a - f 0 - 9 ] { 40 } $ / ) ,
8995 env : {
9096 SONAR_HOST_URL : 'https://sonar.example.com/context' ,
9197 SONAR_TOKEN : '${{ secrets.SONAR_TOKEN }}' ,
@@ -97,21 +103,40 @@ describe('generated GitHub workflow', () => {
97103 it ( 'preserves YAML-sensitive values and quotes complete scanner arguments' , ( ) => {
98104 const result = generateWorkflow ( 'org:repo' , 'https://sonar.example.com' , 'feature: special' , {
99105 sonarTokenVarName : 'ANALYSIS_TOKEN' ,
100- scannerProperty : [
101- "sonar.projectName=Team: Bob's app #1" ,
102- 'sonar.exclusions=a/**,b/**' ,
103- 'sonar.projectVersion=${{ github.sha }}' ,
104- ] ,
106+ scannerProperty : [ "sonar.projectName=Team: Bob's app #1" , 'sonar.exclusions=a/**,b/**' ] ,
105107 } ) ;
106108 expect ( result ) . toContain ( '${{ secrets.ANALYSIS_TOKEN }}' ) ;
107- expect ( result ) . toContain ( '${{ github.sha }}' ) ;
108109 const parsed = yaml . load ( result ) as ReturnType < typeof workflow > ;
109110 expect ( parsed . on . push ) . toEqual ( { branches : [ 'feature: special' ] } ) ;
110111 expect ( parsed . jobs . sonarqube . steps [ 1 ] . with . args ) . toContain (
111112 '"-Dsonar.projectName=Team: Bob\'s app #1"' ,
112113 ) ;
113114 } ) ;
114115
116+ it ( 'blocks PR title expressions whose resolved quotes could add scanner arguments' , ( ) => {
117+ const expression = '${{ github.event.pull_request.title }}' ;
118+ const resolvedTitle = 'My app" "-Dsonar.host.url=https://other.example' ;
119+ // GitHub resolves the expression before the scan action parses its arguments.
120+ const args = `"-Dsonar.projectName=${ expression } "` . replace ( expression , resolvedTitle ) ;
121+
122+ expect ( parseArgsStringToArgv ( args ) ) . toEqual ( [
123+ '-Dsonar.projectName=My app' ,
124+ '-Dsonar.host.url=https://other.example' ,
125+ ] ) ;
126+ expect ( ( ) =>
127+ generateWorkflow ( 'org:repo' , 'https://sonar.example.com' , 'main' , {
128+ scannerProperty : [ `sonar.projectName=${ expression } ` ] ,
129+ } ) ,
130+ ) . toThrow ( 'Scanner properties cannot contain GitHub expressions' ) ;
131+ } ) ;
132+
133+ it ( 'rejects GitHub SHA expressions in scanner properties' , ( ) => {
134+ const property = 'sonar.projectVersion=${{ github.sha }}' ;
135+ expect ( ( ) => validateWorkflowOptions ( { scannerProperty : [ property ] } ) ) . toThrow (
136+ 'Scanner properties cannot contain GitHub expressions' ,
137+ ) ;
138+ } ) ;
139+
115140 it . each ( [ 'bad' , '=value' , 'sonar.x=a\nb' , `sonar.x=both'"` ] ) (
116141 'rejects unsafe property %s before repository processing' ,
117142 ( property ) => {
0 commit comments