Merge pull request #6 from cds-snc/feature/include-pattern

Add the `include` Github action parameter to only review matching files
This commit is contained in:
Jimmy Royer 2024-09-09 13:48:43 -04:00 committed by GitHub
commit 71dac9ec6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 102 additions and 52 deletions

View file

@ -9,5 +9,10 @@
// "ghcr.io/guiyomh/features/just:0": {}, // "ghcr.io/guiyomh/features/just:0": {},
// "ghcr.io/jungaretti/features/ripgrep:1": {}, // "ghcr.io/jungaretti/features/ripgrep:1": {},
// "ghcr.io/lukewiwa/features/shellcheck:0": {}, // "ghcr.io/lukewiwa/features/shellcheck:0": {},
},
"customizations": {
"vscode": {
"extensions": ["yzhang.markdown-all-in-one"]
}
} }
} }

View file

@ -28,10 +28,10 @@ jobs:
uses: peter-evans/create-pull-request@v3 uses: peter-evans/create-pull-request@v3
with: with:
token: ${{ steps.generate_token.outputs.token}} token: ${{ steps.generate_token.outputs.token}}
commit-message: 'Add catalog-info.yaml' commit-message: "Add catalog-info.yaml"
branch: 'backstage/catalog-info' branch: "backstage/catalog-info"
title: 'Add catalog-info.yaml' title: "Add catalog-info.yaml"
body: 'Adding a basic catalog-info.yaml to start populating the backstage catalog with your components.' body: "Adding a basic catalog-info.yaml to start populating the backstage catalog with your components."
labels: 'backstage' labels: "backstage"
add-paths: | add-paths: |
catalog-info.yaml catalog-info.yaml

View file

@ -19,4 +19,5 @@ jobs:
OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }} OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }}
OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }} OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }}
OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }} OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }}
exclude: "yarn.lock,dist/**" exclude: "*lock*,dist/**,**/*.js,**/*.js.map"
include: "**/*.ts,**/*.yml"

View file

@ -8,7 +8,6 @@ jobs:
s3-backup: s3-backup:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with: with:

View file

@ -16,10 +16,10 @@ review process.
1. To use this GitHub Action, you need an OpenAI API key. If you don't have one, sign up for an API key 1. To use this GitHub Action, you need an OpenAI API key. If you don't have one, sign up for an API key
at [OpenAI](https://beta.openai.com/signup). at [OpenAI](https://beta.openai.com/signup).
2. Add the OpenAI API key as a GitHub Secret in your repository with the name `OPENAI_API_KEY`. You can find more 1. Add the OpenAI API key as a GitHub Secret in your repository with the name `OPENAI_API_KEY`. You can find more
information about GitHub Secrets [here](https://docs.github.com/en/actions/reference/encrypted-secrets). information about GitHub Secrets [here](https://docs.github.com/en/actions/reference/encrypted-secrets).
3. Create a `.github/workflows/main.yml` file in your repository and add the following content: 1. Create a `.github/workflows/main.yml` file in your repository and add the following content:
```yaml ```yaml
name: CDS AI Code Reviewer name: CDS AI Code Reviewer
@ -44,11 +44,14 @@ jobs:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4" # Optional: defaults to "gpt-4" OPENAI_API_MODEL: "gpt-4" # Optional: defaults to "gpt-4"
exclude: "**/*.json, **/*.md" # Optional: exclude patterns separated by commas exclude: "**/*.json, **/*.md" # Optional: exclude patterns separated by commas
include: "**/*.ts" # Optional: include patterns separated by commas
``` ```
4. Customize the `exclude` input if you want to ignore certain file patterns from being reviewed. 1. Customize the `exclude` input if you want to ignore certain file patterns from being reviewed.
5. Commit the changes to your repository, and CDS AI Code Reviewer will start working on your future pull requests. 1. Customize the `include` input if you want to add only certain file patterns to be reviewed. Any file matching the include and which also matches the `exclude` will favor the latter: `exclude` > `include`.
1. Commit the changes to your repository, and CDS AI Code Reviewer will start working on your future pull requests.
## How It Works ## How It Works

View file

@ -17,7 +17,11 @@ inputs:
description: "Base URL to the OpenAI API model (OpenAI or Azure)." description: "Base URL to the OpenAI API model (OpenAI or Azure)."
required: true required: true
exclude: exclude:
description: "Glob patterns to exclude files from the diff analysis" description: "Glob patterns to exclude files from the diff analysis. This has precedence over the `include` matches."
required: false
default: ""
include:
description: "Glob patterns to only include certain files from the diff analysis."
required: false required: false
default: "" default: ""
runs: runs:

View file

@ -22,4 +22,5 @@ jobs:
OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }} OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }}
OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }} OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }}
OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }} OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }}
exclude: "yarn.lock,dist/**" exclude: "*lock*,dist/**,**/*.js,**/*.js.map"
include: "**/*.ts,**/*.yml"

View file

@ -17,7 +17,11 @@ inputs:
description: "Base URL to the OpenAI API model (OpenAI or Azure)." description: "Base URL to the OpenAI API model (OpenAI or Azure)."
required: true required: true
exclude: exclude:
description: "Glob patterns to exclude files from the diff analysis" description: "Glob patterns to exclude files from the diff analysis. This has precedence over the `include` matches."
required: false
default: ""
include:
description: "Glob patterns to only include certain files from the diff analysis."
required: false required: false
default: "" default: ""
runs: runs:

23
dist/index.js vendored
View file

@ -331,15 +331,34 @@ ${chunk.changes
const excludePatterns = core const excludePatterns = core
.getInput("exclude") .getInput("exclude")
.split(",") .split(",")
.map((s) => s.trim()); .map((s) => s.trim())
.filter((s) => s.length > 0); // Filter out empty strings;
const includePatterns = core
.getInput("include")
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0); // Filter out empty strings;
const filteredDiff = parsedDiff.filter((file) => { const filteredDiff = parsedDiff.filter((file) => {
return !excludePatterns.some((pattern) => { const excluded =
excludePatterns.length > 0 &&
excludePatterns.some((pattern) => {
var _a; var _a;
return (0, minimatch_1.default)( return (0, minimatch_1.default)(
(_a = file.to) !== null && _a !== void 0 ? _a : "", (_a = file.to) !== null && _a !== void 0 ? _a : "",
pattern pattern
); );
}); });
const included =
includePatterns.length === 0 ||
includePatterns.some((pattern) => {
var _a;
return (0, minimatch_1.default)(
(_a = file.to) !== null && _a !== void 0 ? _a : "",
pattern
);
});
// Excluded patterns take precedence over included patterns.
return !excluded && included;
}); });
const comments = yield analyzeCode(filteredDiff, prDetails); const comments = yield analyzeCode(filteredDiff, prDetails);
if (comments.length > 0) { if (comments.length > 0) {

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -247,12 +247,26 @@ async function main() {
const excludePatterns = core const excludePatterns = core
.getInput("exclude") .getInput("exclude")
.split(",") .split(",")
.map((s) => s.trim()); .map((s) => s.trim())
.filter((s) => s.length > 0); // Filter out empty strings;
const includePatterns = core
.getInput("include")
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0); // Filter out empty strings;
const filteredDiff = parsedDiff.filter((file) => { const filteredDiff = parsedDiff.filter((file) => {
return !excludePatterns.some((pattern) => const excluded: boolean =
minimatch(file.to ?? "", pattern) excludePatterns.length > 0 &&
); excludePatterns.some((pattern) => minimatch(file.to ?? "", pattern));
const included: boolean =
includePatterns.length === 0 ||
includePatterns.some((pattern) => minimatch(file.to ?? "", pattern));
// Excluded patterns take precedence over included patterns.
return !excluded && included;
}); });
const comments = await analyzeCode(filteredDiff, prDetails); const comments = await analyzeCode(filteredDiff, prDetails);