Added the include pattern matching feature to the gh action

This commit is contained in:
Jimmy Royer 2024-09-05 17:57:16 -04:00
parent 89a44f2d65
commit 49d90aa697
9 changed files with 73 additions and 22 deletions

View file

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

View file

@ -12,11 +12,12 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3
- name: Code Review
uses: cds-snc/cds-ai-codereviewer@main
uses: cds-snc/cds-ai-codereviewer@feature/include-pattern
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }}
OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }}
OPENAI_BASE_URL: ${{ vars.OPENAI_BASE_URL }}
exclude: "yarn.lock,dist/**"
exclude: "*lock*,dist/**,**/*.js,**/*.js.map"
include: "**/*.ts,**/*.yml"

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
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).
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
name: CDS AI Code Reviewer
@ -44,11 +44,14 @@ jobs:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_MODEL: "gpt-4" # Optional: defaults to "gpt-4"
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

View file

@ -17,7 +17,11 @@ inputs:
description: "Base URL to the OpenAI API model (OpenAI or Azure)."
required: true
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
default: ""
runs:

View file

@ -22,4 +22,5 @@ jobs:
OPENAI_API_MODEL: ${{ vars.OPENAI_API_MODEL }}
OPENAI_API_VERSION: ${{ vars.OPENAI_API_VERSION }}
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)."
required: true
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
default: ""
runs:

23
dist/index.js vendored
View file

@ -331,15 +331,34 @@ ${chunk.changes
const excludePatterns = core
.getInput("exclude")
.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) => {
return !excludePatterns.some((pattern) => {
const excluded =
excludePatterns.length > 0 &&
excludePatterns.some((pattern) => {
var _a;
return (0, minimatch_1.default)(
(_a = file.to) !== null && _a !== void 0 ? _a : "",
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
);
});
// Exluded patterns take precedence over included patterns.
return !excluded && included;
});
const comments = yield analyzeCode(filteredDiff, prDetails);
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
.getInput("exclude")
.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) => {
return !excludePatterns.some((pattern) =>
minimatch(file.to ?? "", pattern)
);
const excluded: boolean =
excludePatterns.length > 0 &&
excludePatterns.some((pattern) => minimatch(file.to ?? "", pattern));
const included: boolean =
includePatterns.length === 0 ||
includePatterns.some((pattern) => minimatch(file.to ?? "", pattern));
// Exluded patterns take precedence over included patterns.
return !excluded && included;
});
const comments = await analyzeCode(filteredDiff, prDetails);