razure-devopsazure-pipelines

Azure pipeline to read files in a pull request


I'm trying to build a azure pipeline to lint R scripts in a pull request as one of the steps to allow pull requests to be approved and completed.

I can get the pipeline to run R and library everything I need, but I am struggling to find a way to access the R scripts changed in a pull request via the pipeline.

Any help would be greatly appreciated.


Solution

  • You can find changed files of PR with git commands. Add it to the PR validation build, as an example for PR to the master branch:

    git diff --name-only --relative --diff-filter AMR origin/master -- . > $(Build.StagingDirectory)/repo_changes.txt
    

    Here is the example to copy only changed files with PowerShell (PowerShell task)

    $targetfolder = "$(Build.StagingDirectory)" + "/"
    
    function CopyFiles{
        param( [string]$source )
    
        $target = $targetfolder + $source
    
        New-Item -Force $target
        copy-item $source $target -Force
    }
    
    $changes = git diff --name-only --relative --diff-filter AMR origin/master --name-only -- .
    
    if ($changes -is [string]){ CopyFiles $changes }
    else
    {
        if ($changes -is [array])
        {       
            foreach ($change in $changes){ CopyFiles $change }
        }
    }
    

    The part of yaml file may be like that:

    jobs:
    - job: get_changed_files
      steps:  
       - task: PowerShell@2
         inputs:
           targetType: 'inline'
           script: |
             $targetfolder = "$(Build.StagingDirectory)" + "/"
             
             function CopyFiles{
                 param( [string]$source )
             
                 $target = $targetfolder + $source
             
                 New-Item -Force $target
                 copy-item $source $target -Force
             }
             
             $changes = git diff --name-only --relative --diff-filter AMR origin/master -- .
             
             if ($changes -is [string]){ CopyFiles $changes }
             else
             {
                 if ($changes -is [array])
                 {       
                     foreach ($change in $changes){ CopyFiles $change }
                 }
             }
       - task: PublishBuildArtifacts@1
         inputs:
          pathToPublish: $(Build.StagingDirectory)
          artifactName: MyChangedFiles