From 1ddfeb7b9d225914b6141b8236a4d897f9e21773 Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Thu, 22 Aug 2024 18:21:22 -0500 Subject: [PATCH 01/12] separate workflow for summarize --- .github/workflows/summarize-log.yml | 44 +++++++++++++++++++++++++++++ .github/workflows/test-workflow.yml | 32 +++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .github/workflows/summarize-log.yml create mode 100644 .github/workflows/test-workflow.yml diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml new file mode 100644 index 0000000..d481b9d --- /dev/null +++ b/.github/workflows/summarize-log.yml @@ -0,0 +1,44 @@ +name: Summarize + +on: + workflow_call: + inputs: + job_name: # New input to specify the job to summarize + required: true + type: string + secrets: + github-token: + required: true + +jobs: + post-comment: + runs-on: ubuntu-latest + steps: + - name: Download logs artifact + uses: actions/download-artifact@v3 + with: + name: ${{ inputs.job_name }}-logs # Use the job_name input to construct the artifact name + + - name: Summarize logs and post comment + env: + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} + run: | + LOGS=$(cat ${{ inputs.job_name }}-logs/*.txt | tr '\n' '\r') + PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" + + # ... (rest of your logic to call the Gemini API and post the comment) ... + + - name: Post PR comment + uses: actions/github-script@v6 + if: github.event_name == 'pull_request' + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: '${{ inputs.summary }}' + }); + + - name: Log summary + run: 'echo "Workflow summary: ${{ inputs.summary }}" >> workflow_summary.log' \ No newline at end of file diff --git a/.github/workflows/test-workflow.yml b/.github/workflows/test-workflow.yml new file mode 100644 index 0000000..1848b3c --- /dev/null +++ b/.github/workflows/test-workflow.yml @@ -0,0 +1,32 @@ +name: Test Summarize Workflow + +on: + workflow_dispatch: # Allows manual triggering + +jobs: + generate-logs: + runs-on: ubuntu-latest + steps: + - name: Generate fake logs + run: | + echo "Step 1: Cloning repository..." > fake_logs.txt + echo "Step 2: Installing dependencies..." >> fake_logs.txt + echo "Step 3: Building project..." >> fake_logs.txt + echo "Step 4: Running tests..." >> fake_logs.txt + echo " Test 1: Passed" >> fake_logs.txt + echo " Test 2: Failed" >> fake_logs.txt + echo "Step 5: Uploading artifacts..." >> fake_logs.txt + + - name: Upload logs artifact + uses: actions/upload-artifact@v3 + with: + name: test-job-logs # Name the artifact to match the 'job_name' you'll pass later + path: fake_logs.txt + + trigger-summarize: + needs: generate-logs # Ensure this runs after the logs are generated + uses: ./.github/workflows/summarize-log.yml + with: + job_name: test-job # Match the artifact name prefix + secrets: + github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From 478194fa37a75f99c798c2450d9e7f232cdc78e7 Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 13:24:55 -0500 Subject: [PATCH 02/12] Update workflow triggers and add log summary --- .github/workflows/summarize-log.yml | 2 +- .github/workflows/test-workflow.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index d481b9d..a03f0bb 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -41,4 +41,4 @@ jobs: }); - name: Log summary - run: 'echo "Workflow summary: ${{ inputs.summary }}" >> workflow_summary.log' \ No newline at end of file + run: 'echo "Workflow summary: ${{ inputs.summary }}" >> workflow_summary.log' diff --git a/.github/workflows/test-workflow.yml b/.github/workflows/test-workflow.yml index 1848b3c..a72d523 100644 --- a/.github/workflows/test-workflow.yml +++ b/.github/workflows/test-workflow.yml @@ -1,7 +1,7 @@ name: Test Summarize Workflow on: - workflow_dispatch: # Allows manual triggering + pull_request jobs: generate-logs: @@ -29,4 +29,4 @@ jobs: with: job_name: test-job # Match the artifact name prefix secrets: - github-token: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + github-token: ${{ secrets.GITHUB_TOKEN }} From 3dc052ae0c68dee1ff35732f8b0550ae06809eaa Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 13:27:30 -0500 Subject: [PATCH 03/12] add permission --- .github/workflows/summarize-log.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index a03f0bb..d4a320c 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -12,6 +12,10 @@ on: jobs: post-comment: + permissions: + issues: write + pull-requests: write + contents: write runs-on: ubuntu-latest steps: - name: Download logs artifact From b388273dc02fc1a24b74930cca03187dbde252c9 Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 13:30:25 -0500 Subject: [PATCH 04/12] add permission to calling job --- .github/workflows/summarize-log.yml | 9 +++++---- .github/workflows/test-workflow.yml | 5 +++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index d4a320c..6959c0c 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -10,12 +10,13 @@ on: github-token: required: true +permissions: + issues: write + pull-requests: write + contents: write + jobs: post-comment: - permissions: - issues: write - pull-requests: write - contents: write runs-on: ubuntu-latest steps: - name: Download logs artifact diff --git a/.github/workflows/test-workflow.yml b/.github/workflows/test-workflow.yml index a72d523..82ae620 100644 --- a/.github/workflows/test-workflow.yml +++ b/.github/workflows/test-workflow.yml @@ -3,6 +3,11 @@ name: Test Summarize Workflow on: pull_request +permissions: + issues: write + pull-requests: write + contents: write + jobs: generate-logs: runs-on: ubuntu-latest From 28326753b19c743ef5ff5629d6aecd1f658a8a1d Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 13:39:05 -0500 Subject: [PATCH 05/12] add actual summary logic back in --- .github/workflows/summarize-log.yml | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 6959c0c..2dc1910 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -1,9 +1,9 @@ -name: Summarize +name: Summarize on: workflow_call: inputs: - job_name: # New input to specify the job to summarize + job_name: required: true type: string secrets: @@ -22,7 +22,7 @@ jobs: - name: Download logs artifact uses: actions/download-artifact@v3 with: - name: ${{ inputs.job_name }}-logs # Use the job_name input to construct the artifact name + name: ${{ inputs.job_name }}-logs - name: Summarize logs and post comment env: @@ -31,7 +31,14 @@ jobs: LOGS=$(cat ${{ inputs.job_name }}-logs/*.txt | tr '\n' '\r') PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" - # ... (rest of your logic to call the Gemini API and post the comment) ... + # Call Gemini API using curl + SUMMARY=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $GEMINI_API_KEY" -d "{\"prompt\": \"$PROMPT\"}" https://api.gemini.com/v1/completions) + + # Extract summary from Gemini response using jq + SUMMARY=$(echo $SUMMARY | jq -r '.completions[0].text') + + # Set the summary as an environment variable for the next step + echo "SUMMARY=$SUMMARY" >> $GITHUB_ENV - name: Post PR comment uses: actions/github-script@v6 @@ -42,8 +49,8 @@ jobs: issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, - body: '${{ inputs.summary }}' + body: '${{ env.SUMMARY }}' }); - name: Log summary - run: 'echo "Workflow summary: ${{ inputs.summary }}" >> workflow_summary.log' + run: 'echo "Workflow summary: ${{ env.SUMMARY }}" >> workflow_summary.log' \ No newline at end of file From 12249806f0438debb9331c8eef25417398183190 Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 13:52:50 -0500 Subject: [PATCH 06/12] Fix formatting in summarize-log.yml --- .github/workflows/summarize-log.yml | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 2dc1910..98680dd 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -3,7 +3,7 @@ name: Summarize on: workflow_call: inputs: - job_name: + job_name: required: true type: string secrets: @@ -26,7 +26,7 @@ jobs: - name: Summarize logs and post comment env: - GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} + GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} run: | LOGS=$(cat ${{ inputs.job_name }}-logs/*.txt | tr '\n' '\r') PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" @@ -40,9 +40,10 @@ jobs: # Set the summary as an environment variable for the next step echo "SUMMARY=$SUMMARY" >> $GITHUB_ENV - - name: Post PR comment + - name: Post PR comment OR Log summary + # Use a conditional step to either post a comment or log the summary + if: github.event_name == 'pull_request' uses: actions/github-script@v6 - if: github.event_name == 'pull_request' with: script: | github.rest.issues.createComment({ @@ -51,6 +52,10 @@ jobs: repo: context.repo.repo, body: '${{ env.SUMMARY }}' }); + env: + SUMMARY: ${{ env.SUMMARY }} - - name: Log summary - run: 'echo "Workflow summary: ${{ env.SUMMARY }}" >> workflow_summary.log' \ No newline at end of file + - name: Log summary (if not a pull request) + # This step will run if it's NOT a pull request + if: github.event_name != 'pull_request' + run: 'echo "Workflow summary: ${{ env.SUMMARY }}" >> workflow_summary.log' From 8566b9ad28e8d1485423894c4089a5cb756b5399 Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 14:02:09 -0500 Subject: [PATCH 07/12] debug log --- .github/workflows/summarize-log.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 98680dd..000558f 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -23,6 +23,9 @@ jobs: uses: actions/download-artifact@v3 with: name: ${{ inputs.job_name }}-logs + + - name: List artifact contents + run: ls -lR # List all files and directories recursively - name: Summarize logs and post comment env: From 0197ae6a369412f56616f9b70fbfa72cc81ce9bd Mon Sep 17 00:00:00 2001 From: Chris Freels Date: Wed, 11 Sep 2024 14:08:22 -0500 Subject: [PATCH 08/12] logs dont come in a directory --- .github/workflows/summarize-log.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 000558f..94e7c4a 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -23,7 +23,7 @@ jobs: uses: actions/download-artifact@v3 with: name: ${{ inputs.job_name }}-logs - + - name: List artifact contents run: ls -lR # List all files and directories recursively @@ -31,7 +31,7 @@ jobs: env: GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} run: | - LOGS=$(cat ${{ inputs.job_name }}-logs/*.txt | tr '\n' '\r') + LOGS=$(cat *.txt) PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" # Call Gemini API using curl From 48fa37cedd9d2f09b8011084a05e593d3f18b447 Mon Sep 17 00:00:00 2001 From: chrypnotoad Date: Tue, 24 Sep 2024 17:41:27 -0500 Subject: [PATCH 09/12] use latest gemini api --- .github/workflows/summarize-log.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 94e7c4a..74c3bbb 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -35,10 +35,10 @@ jobs: PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" # Call Gemini API using curl - SUMMARY=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $GEMINI_API_KEY" -d "{\"prompt\": \"$PROMPT\"}" https://api.gemini.com/v1/completions) + SUMMARY=$(curl -s -X POST -H "Content-Type: application/json" -d `{"contents":[{"parts":[{"text":"$PROMPT"}]}]}` 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=$GEMINI_API_KEY') # Extract summary from Gemini response using jq - SUMMARY=$(echo $SUMMARY | jq -r '.completions[0].text') + SUMMARY=$(echo $SUMMARY | jq -r '.candidates[0].content.parts[0].text') # Set the summary as an environment variable for the next step echo "SUMMARY=$SUMMARY" >> $GITHUB_ENV From cb42212c5442dab59e59fad63000e66f36f14377 Mon Sep 17 00:00:00 2001 From: chrypnotoad Date: Tue, 24 Sep 2024 17:46:31 -0500 Subject: [PATCH 10/12] Update summarize-log.yml --- .github/workflows/summarize-log.yml | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 74c3bbb..e409a49 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -34,14 +34,35 @@ jobs: LOGS=$(cat *.txt) PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" + JSON_PAYLOAD=$(jq -n --arg text "$PROMPT" '{"contents":[{"parts":[{"text":$text}]}]}') + + # Call Gemini API using curl - SUMMARY=$(curl -s -X POST -H "Content-Type: application/json" -d `{"contents":[{"parts":[{"text":"$PROMPT"}]}]}` 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=$GEMINI_API_KEY') + RESPONSE=$(curl -s -X POST \ + -H "Content-Type: application/json" \ + -d "$JSON_PAYLOAD" \ + "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}") + + # Check if curl command was successful + if [ $? -ne 0 ]; then + echo "Error: Failed to call Gemini API." + exit 1 + fi # Extract summary from Gemini response using jq - SUMMARY=$(echo $SUMMARY | jq -r '.candidates[0].content.parts[0].text') + SUMMARY=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text') + + # Validate SUMMARY + if [ -z "$SUMMARY" ] || [ "$SUMMARY" == "null" ]; then + echo "Error: Summary is empty or null." + exit 1 + fi + # Set the summary as an environment variable for the next step - echo "SUMMARY=$SUMMARY" >> $GITHUB_ENV + echo "SUMMARY<> $GITHUB_ENV + echo "$SUMMARY" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV - name: Post PR comment OR Log summary # Use a conditional step to either post a comment or log the summary From 817364b7d8a35361f3f75e89b0be02b276c18c77 Mon Sep 17 00:00:00 2001 From: chrypnotoad Date: Tue, 24 Sep 2024 17:52:48 -0500 Subject: [PATCH 11/12] Update summarize-log.yml --- .github/workflows/summarize-log.yml | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index e409a49..0b7766a 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -43,9 +43,22 @@ jobs: -d "$JSON_PAYLOAD" \ "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}") - # Check if curl command was successful - if [ $? -ne 0 ]; then - echo "Error: Failed to call Gemini API." + echo "Gemini API Response: $RESPONSE" + + # Extract HTTP status code + HTTP_STATUS=$(echo "$RESPONSE" | grep HTTP_STATUS | awk -F: '{print $2}') + # Extract the JSON body + RESPONSE_BODY=$(echo "$RESPONSE" | sed -e 's/HTTP_STATUS\:.*//g') + + echo "HTTP Status Code: $HTTP_STATUS" + echo "Response Body: $RESPONSE_BODY" + + # Check if the HTTP status code indicates success + if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -lt 300 ]]; then + echo "Gemini API call successful." + else + echo "Error: Gemini API returned status code $HTTP_STATUS." + echo "Response Body: $RESPONSE_BODY" exit 1 fi @@ -58,7 +71,6 @@ jobs: exit 1 fi - # Set the summary as an environment variable for the next step echo "SUMMARY<> $GITHUB_ENV echo "$SUMMARY" >> $GITHUB_ENV From 0265eefa3f76f3d255ce1e9c21cc40e2c36f534e Mon Sep 17 00:00:00 2001 From: chrypnotoad Date: Wed, 25 Sep 2024 10:00:51 -0500 Subject: [PATCH 12/12] lots of logging --- .github/workflows/summarize-log.yml | 50 ++++++++++++++++++----------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/.github/workflows/summarize-log.yml b/.github/workflows/summarize-log.yml index 0b7766a..b55af92 100644 --- a/.github/workflows/summarize-log.yml +++ b/.github/workflows/summarize-log.yml @@ -31,28 +31,38 @@ jobs: env: GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }} run: | + set -e # Exit immediately if a command exits with a non-zero status + + # Combine all .txt files into LOGS LOGS=$(cat *.txt) - PROMPT="Summarize the following GitHub workflow logs:\r$LOGS" - + echo "Logs Content: $LOGS" + + # Prepare the prompt with proper newline handling + PROMPT="Summarize the following GitHub workflow logs:\n$LOGS" + echo -e "Prompt: $PROMPT" + + # Create the JSON payload safely using jq JSON_PAYLOAD=$(jq -n --arg text "$PROMPT" '{"contents":[{"parts":[{"text":$text}]}]}') - - - # Call Gemini API using curl - RESPONSE=$(curl -s -X POST \ + echo "JSON Payload: $JSON_PAYLOAD" + + # Call Gemini API using curl and capture both response body and HTTP status code + HTTP_RESPONSE=$(curl -s -w "HTTP_STATUS:%{http_code}" -X POST \ -H "Content-Type: application/json" \ -d "$JSON_PAYLOAD" \ "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=${GEMINI_API_KEY}") - - echo "Gemini API Response: $RESPONSE" - - # Extract HTTP status code - HTTP_STATUS=$(echo "$RESPONSE" | grep HTTP_STATUS | awk -F: '{print $2}') + + # Log the raw API response + echo "Gemini API Response: $HTTP_RESPONSE" + + # Extract the HTTP status code + HTTP_STATUS=$(echo "$HTTP_RESPONSE" | grep HTTP_STATUS | awk -F: '{print $2}') + # Extract the JSON body - RESPONSE_BODY=$(echo "$RESPONSE" | sed -e 's/HTTP_STATUS\:.*//g') - + RESPONSE_BODY=$(echo "$HTTP_RESPONSE" | sed -e 's/HTTP_STATUS\:.*//g') + echo "HTTP Status Code: $HTTP_STATUS" echo "Response Body: $RESPONSE_BODY" - + # Check if the HTTP status code indicates success if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -lt 300 ]]; then echo "Gemini API call successful." @@ -61,20 +71,22 @@ jobs: echo "Response Body: $RESPONSE_BODY" exit 1 fi - + # Extract summary from Gemini response using jq - SUMMARY=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text') - - # Validate SUMMARY + SUMMARY=$(echo "$RESPONSE_BODY" | jq -r '.candidates[0].content.parts[0].text') + echo "Extracted Summary: $SUMMARY" + + # Validate SUMMARY if [ -z "$SUMMARY" ] || [ "$SUMMARY" == "null" ]; then echo "Error: Summary is empty or null." exit 1 fi - + # Set the summary as an environment variable for the next step echo "SUMMARY<> $GITHUB_ENV echo "$SUMMARY" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV + shell: /usr/bin/bash -e {0} - name: Post PR comment OR Log summary # Use a conditional step to either post a comment or log the summary