Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 21 additions & 35 deletions actions/greptile/auto-approve/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ description: |
path, it has the "AUTOREVIEW: RESTRICTED" label; the label goes away when the PR
touches none again. The action never edits the PR description.

The score is trusted only when Greptile made the last edit of the PR description, and it is trusted even when Greptile reviewed an older
commit. The changed files are always checked at the PR head.
The score is trusted only when Greptile made the last edit of the PR description,
and it is trusted even when Greptile reviewed an older commit. The changed files
are always checked at the PR head.

The protected list is read from the base branch through the API, so a PR cannot
change the list it is checked against, and the caller needs no checkout. Format:
one path or bash glob per line, relative to the repo root; '*' also matches '/';
'#' starts a comment; a '!' line excludes paths: a path is protected when it
matches a pattern and no '!' pattern. Matching is case-sensitive.
change the list it is checked against, and the caller needs no checkout. The
action checks each changed path, and the old path of a renamed file, with the
@actions/glob globber (followSymbolicLinks: false, implicitDescendants: true,
matchDirectories: false). Rules of the list:
- One pattern on each line, relative to the repository root. A line that starts
with '#' is a comment. A '#' after a pattern is part of the pattern.
- A pattern that starts with '/' or '~', or that has a '..' segment, is an error:
the action then protects all paths and the job fails.
- '*' and '?' do not match '/'. '**' matches any number of directories.
'[abc]' matches one of the characters. Braces and extglob are not supported.
- A pattern that matches a directory also matches all files in it:
'dir', 'dir/', 'dir/*' and 'dir/**' all protect the full directory.
'*.sh' matches only the .sh files in the root; use '**/*.sh' for all of them.
- A '!' line excludes paths. The last line that matches a path wins, so put a
'!' line after the lines it limits.
- Matching is case-insensitive.

Call it from a `pull_request_target` workflow with `pull-requests: write`, on
`opened, edited, synchronize, reopened, ready_for_review, converted_to_draft`:
Expand All @@ -43,32 +56,5 @@ inputs:
default: ${{ github.token }}

runs:
using: 'composite'

steps:
- name: Fetch protected list from the base branch
shell: bash
env:
GH_TOKEN: ${{ inputs.github-token }}
REPO: ${{ github.repository }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
PROTECTED_PATHS_SOURCE: ${{ inputs.protected-paths }}
run: |
gh api \
-H 'Accept: application/vnd.github.raw' \
"repos/${REPO}/contents/${PROTECTED_PATHS_SOURCE}?ref=${BASE_SHA}" \
>"${RUNNER_TEMP}/greptile-auto-approve-protected.txt"

- name: Approve or withdraw approval
shell: bash
env:
ACTION_PATH: ${{ github.action_path }}
GH_TOKEN: ${{ inputs.github-token }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PROTECTED_PATHS_LABEL: ${{ inputs.protected-paths }}
MIN_SCORE: ${{ inputs.min-score }}
DRY_RUN: ${{ inputs.dry-run }}
run: |
PROTECTED_PATHS="${RUNNER_TEMP}/greptile-auto-approve-protected.txt" \
"${ACTION_PATH}/greptile-auto-approve.sh"
using: 'node24'
main: 'lib/index.js'
217 changes: 0 additions & 217 deletions actions/greptile/auto-approve/greptile-auto-approve.sh

This file was deleted.

4 changes: 4 additions & 0 deletions actions/greptile/auto-approve/lib/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions actions/greptile/auto-approve/lib/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions actions/greptile/auto-approve/lib/sourcemap-register.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions actions/greptile/auto-approve/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@milaboratories/actions.greptile.auto-approve",
"version": "1.0.0",
"private": true,
"description": "Approve a PR after a Greptile review when the PR touches no protected path",
"main": "./lib/index.js",
"scripts": {
"build": "rm -rf lib dist && tsc && ncc build dist/main.js --minify --out lib --source-map",
"format": "prettier --write '**/*.ts'",
"format-check": "prettier --check '**/*.ts'",
"lint": "eslint src/**/*.ts",
"test": "vitest run",
"release": "npm run build && npm run format && npm run lint",
"all": "npm run release && npm test"
},
"dependencies": {
"@actions/core": "catalog:",
"@actions/github": "catalog:",
"@actions/glob": "catalog:"
},
"devDependencies": {
"@types/node": "catalog:",
"typescript": "catalog:",
"@vercel/ncc": "catalog:",
"vitest": "catalog:"
}
}
61 changes: 61 additions & 0 deletions actions/greptile/auto-approve/src/decision.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from "vitest";

import { decide, PullRequestState, restrictedReason } from "./decision";

const scored = (score: number): PullRequestState => ({
state: "OPEN",
isDraft: false,
editor: "greptile-apps",
body: `Summary\n<!-- greptile_confidence_score:${score} -->\n`,
});

describe("decide", () => {
it("approves a high score on an open PR with no protected path", () => {
expect(decide(scored(4), 4, "")).toEqual({ reason: "", score: 4 });
expect(decide({ ...scored(5), editor: "greptile-apps[bot]" }, 4, "")).toEqual({ reason: "", score: 5 });
});

it("does not approve a closed or draft PR", () => {
expect(decide({ ...scored(5), state: "MERGED" }, 4, "").reason).toBe("PR is MERGED");
expect(decide({ ...scored(5), isDraft: true }, 4, "").reason).toBe("PR is a draft");
});

it("trusts the score only when Greptile edited the body last", () => {
expect(decide({ ...scored(5), editor: "someone" }, 4, "").reason).toBe(
"PR body was last edited by 'someone', not by Greptile",
);
expect(decide({ ...scored(5), editor: "" }, 4, "").reason).toBe(
"PR body was last edited by '<nobody>', not by Greptile",
);
});

it("needs exactly one score marker", () => {
expect(decide({ ...scored(5), body: "no score" }, 4, "").reason).toBe("no Greptile confidence score in PR body");
expect(decide({ ...scored(5), body: scored(5).body + scored(4).body }, 4, "").reason).toBe(
"more than one Greptile confidence score in PR body",
);
});

it("does not approve a score below min-score", () => {
expect(decide(scored(3), 4, "")).toEqual({ reason: "Greptile confidence score 3/5 is below 4", score: 3 });
});

it("does not approve a restricted PR", () => {
expect(decide(scored(5), 4, "PR touches protected path x")).toEqual({
reason: "PR touches protected path x",
score: 5,
});
});
});

describe("restrictedReason", () => {
it("names the first protected path and the count of the others", () => {
expect(restrictedReason(3, [])).toBe("");
expect(restrictedReason(3, ["a"])).toBe("PR touches protected path a");
expect(restrictedReason(3, ["a", "b", "c"])).toBe("PR touches protected path a and 2 more");
});

it("restricts a PR that is too large for the files API", () => {
expect(restrictedReason(3000, [])).toBe("PR changes 3000 files, more than the files API lists");
});
});
Loading
Loading