diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index eb369e1..b686534 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,8 +1,9 @@ name: test -on: [push, pull_request] +on: pull_request permissions: contents: read + pull-requests: write jobs: test: @@ -18,7 +19,9 @@ jobs: - name: Run action uses: ./ - - name: Run action with version + - name: Run action with inputs uses: ./ with: - version: 2.3.4 + model: codellama + prompt: 'Summarize code diff in bullet points:' + token: ${{ github.token }} diff --git a/README.md b/README.md index ae32e83..18cc414 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ jobs: ## Usage -**Basic:** +Append summary to PR description: ```yaml - uses: ai-action/pull-request-summary@v1 @@ -34,14 +34,34 @@ See [action.yml](action.yml) ## Inputs -### `version` +### `model` -**Optional**: The version. Defaults to `1.2.3`: +**Optional**: The language [model](https://ollama.com/library) to use. Defaults to [codellama](https://ollama.com/library/codellama): ```yaml - uses: ai-action/pull-request-summary@v1 with: - version: 1.2.3 + model: codellama +``` + +### `prompt` + +**Optional**: The input prompt that comes before the PR diff. Defaults to: + +```yaml +- uses: ai-action/pull-request-summary@v1 + with: + prompt: 'Summarize the code diff concisely:' +``` + +### `token` + +**Optional**: The GitHub token. Defaults to `GITHUB_TOKEN`: + +```yaml +- uses: ai-action/pull-request-summary@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} ``` ## License diff --git a/action.yml b/action.yml index c5401b0..4d080b1 100644 --- a/action.yml +++ b/action.yml @@ -1,20 +1,51 @@ name: pull-request-summary -description: GitHub Action that reviews pull requests with AI (LLM) +description: GitHub Action that summarizes pull requests with AI (LLM) author: remarkablemark inputs: - version: - description: Description + model: + description: Language model name + default: codellama + required: false + prompt: + description: Input prompt before the PR diff + default: 'Summarize the code diff concisely:' + required: false + token: + description: GitHub token + default: ${{ github.token }} required: false - default: 1.2.3 runs: using: composite steps: - - name: Print version - shell: bash - run: | - echo 'version: ${{ inputs.version }}' + - name: Setup ollama + uses: ai-action/setup-ollama@v1 + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Summarize pull request + shell: bash + run: | + PROMPT=$(printf '%s\n%s' "$PROMPT" "$(gh pr diff $PR_NUMBER)") + RESPONSE=$(ollama run $MODEL "$PROMPT" | sed 's/^/> /') + BODY=$(gh pr view $PR_NUMBER --json body --jq .body) + COMMENT_START='' + COMMENT_END='' + + if [[ "$BODY" == *"$COMMENT_START"* ]]; then + # delete between patterns (inclusive) + BODY=$(echo "$BODY" | sed "/$COMMENT_START/,/$COMMENT_END/d") + fi + + BODY=$(printf '%s\n\n%s\n\n> [!NOTE]\n%s\n\n%s\n' "$BODY" $COMMENT_START "$RESPONSE" $COMMENT_END) + gh pr edit $PR_NUMBER --body "$BODY" + env: + GH_TOKEN: ${{ inputs.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + PROMPT: ${{ inputs.prompt }} + MODEL: ${{ inputs.model }} branding: icon: code