AI Azure DevOps PR1
This extension provides an AI-powered task for Azure DevOps pipelines that integrates with Azure OpenAI to analyze Pull Requests and generate intelligent feedback based on PR diffs and custom prompts.
Features
- Azure OpenAI integration for generating AI-powered responses based on prompts
- Read markdown files as system and user prompts for customizable AI interactions
- Automatic Pull Request diff generation to use as prompt content
- Save AI-generated responses to markdown files
- Post responses as comments directly on Pull Requests
- Easy integration with your existing CI/CD workflows
Requirements
- Azure DevOps Services or Azure DevOps Server 2020 or later
- Azure OpenAI account and API key
- Permissions to comment on Pull Requests (for postToPR functionality)
- Git installed on pipeline agents (for autoDiff functionality)
Installation
From the Marketplace
- Go to the Azure DevOps Marketplace
- Search for "AI Azure DevOps PR"
- Click "Get it free"
- Select the organization where you want to install the extension
Manual Installation
- Download the VSIX file from the project releases
- Navigate to the Extensions section in your Azure DevOps organization (
https://dev.azure.com/{organization}/_settings/extensions
)
- Click "Upload a new extension" and select the VSIX file
- Install the extension in your organization
Usage
Basic Configuration
# azure-pipelines.yml
steps:
- task: AIAzureDevOpsPR@1
displayName: 'Generate content with Azure OpenAI'
inputs:
endpoint: 'https://your-azure-openai-endpoint.cognitiveservices.azure.com/'
modelName: 'gpt-4'
deployment: 'your-deployment-name'
apiKey: '$(AZURE_OPENAI_API_KEY)' # Use a secret variable
systemPromptFile: '$(Build.SourcesDirectory)/prompts/system-prompt.md'
userPromptFile: '$(Build.SourcesDirectory)/prompts/user-prompt.md'
outputFile: '$(Build.ArtifactStagingDirectory)/openai-response.md'
postToPR: false
autoDiff: false
# azure-pipelines.yml
trigger:
- none
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
persistCredentials: true # Important for Git access in autoDiff
- task: AIAzureDevOpsPR@1
displayName: 'Analyze PR with Azure OpenAI'
inputs:
endpoint: 'https://your-azure-openai-endpoint.cognitiveservices.azure.com/'
modelName: 'gpt-4'
deployment: 'deployment-name'
apiKey: '$(AZURE_OPENAI_API_KEY)'
systemPromptFile: '$(Build.SourcesDirectory)/prompts/code-review-prompt.md'
userPromptFile: '$(Build.SourcesDirectory)/prompts/additional-context.md'
outputFile: '$(Build.ArtifactStagingDirectory)/code-review.md'
autoDiff: true # Automatically generate PR diff as prompt
postToPR: true # Post response as comment on PR
IMPORTANT: To use the postToPR
functionality, make sure to enable the "Allow scripts to access OAuth token" option in your pipeline settings.
Sample File Structure
To facilitate setup, you can use this file structure:
repository/
├── .azure-pipelines/
│ └── openai-pr-review.yml # PR review pipeline
├── prompts/
│ ├── system-prompt.md # System prompt for OpenAI
│ └── user-prompt.md # User prompt (optional if using autoDiff)
└── README.md
Parameter |
Required |
Default |
Description |
endpoint |
Yes |
- |
Azure OpenAI endpoint URL |
deployment |
Yes |
- |
Azure OpenAI deployment name |
apiKey |
Yes |
- |
API Key for authentication (use secret variables) |
systemPromptFile |
Yes |
- |
Path to markdown file with system prompt |
userPromptFile |
No* |
- |
Path to markdown file with user prompt (*requerido solo si autoDiff = false) |
modelName |
No |
gpt-4 |
OpenAI model name a utilizar |
outputFile |
No |
- |
Path donde se guardará el archivo markdown de salida (si no se especifica, solo se mostrará en Extensions) |
autoDiff |
No |
false |
Si es true , genera automáticamente un diff (en PR: entre ramas del PR; en pipeline regular: entre último merge y HEAD) |
postToPR |
No |
true |
Si se debe publicar el resultado como comentario en el PR actual |
Nota: El análisis de OpenAI siempre se muestra automáticamente en la sección "Extensions" de Azure DevOps para facilitar la visualización de los resultados.
Using autoDiff
When autoDiff
is set to true
, the task generates an automatic diff that varies depending on the execution context:
In Pull Request context
- Identifies that it's running in a PR context
- Executes the diff generation script between the source and target branches of the PR
- Uses the generated diff as the user prompt content, instead of the
userPromptFile
In regular pipeline runs (non-PR context)
- Identifies that it's running in a regular pipeline (not a PR)
- Automatically finds the last merge commit to the main branch (main/master)
- Generates a diff between that merge commit and the latest commit on the current branch
- Uses this diff as the user prompt content, instead of the
userPromptFile
- This allows analyzing changes since the last integration with the main branch
Fallback behavior
If for any reason the diff cannot be generated (e.g., Git history issues, first commit, etc.), the task falls back to using the userPromptFile
content.
This functionality is ideal for:
- Automated code reviews in PRs
- Change summaries in regular pipeline runs
- Generating documentation based on recent changes
- Analyzing code quality across branch development
To use the postToPR
functionality, make sure to:
- Enable the "Allow scripts to access OAuth token" option in your pipeline settings
- Have sufficient permissions to comment on PRs
- Configure the trigger correctly to run in a PR context
Development
Prerequisites
- Node.js 16 or later
- npm 7 or later
- Visual Studio Code (recommended for development)
- Azure DevOps CLI (optional, for publishing)
Project Structure
azure-devops-task/
├── dist/ # Compiled files (generated)
├── images/ # Extension images
├── scripts/ # Utility scripts
├── src/ # Source code
│ ├── index.ts # Main entry point
│ ├── task.json # Task definition
│ └── scripts/ # Scripts included with the task
├── tests/ # Tests
├── package.json # Project configuration
├── tsconfig.json # TypeScript configuration
└── vss-extension.json # Extension manifest
Available Commands
Development
npm run build
: Compile the TypeScript project
npm run clean
: Remove generated files
npm run prepare-package
: Prepare files for packaging
Packaging
npm run package
: Create VSIX package for distribution
npm run package:dev
: Create VSIX package with development version
Publishing
npm run publish
: Publish extension to the marketplace
npm run publish:dev
: Publish development version (private)
Version Management
npm run increment-version
: Increment project version (patch by default)
npm run increment-version -- major
: Increment major version
npm run increment-version -- minor
: Increment minor version
Development Process
- Clone this repository
- Run
npm install
to install dependencies
- Modify the source code in the
src
folder
- Run
npm run build
to compile and check for errors
- Run
npm run package
to create the VSIX package
- Test the extension in your Azure DevOps environment
Publishing to the Marketplace
To publish the extension to the Azure DevOps Marketplace, you need:
- A publisher account on the Visual Studio Marketplace
- Update the
publisher
field in the vss-extension.json
file with your publisher ID
- Run
npm run increment-version
to update the version
- Run
npm run publish
to package and publish the extension
Local Testing
To test the task locally before publishing:
# Build the project
npm run build
# Copy task.json to the dist folder
cp src/task.json dist/
# Run the test script
node tests/runTest.js
Note: For complete testing with Azure OpenAI, you'll need to provide a valid API key in the test script.
License
ISC
Contributions
Contributions are welcome. Please follow these steps:
- Fork the repository
- Create a branch with your feature name (
git checkout -b feature/new-feature
)
- Make your changes and commit (
git commit -am 'Add new feature'
)
- Push to the branch (
git push origin feature/new-feature
)
- Create a Pull Request