Skip to content

Commit

Permalink
Merge pull request #9 from KainosSoftwareLtd/chore/add-aep-actions
Browse files Browse the repository at this point in the history
chore: add aep actions
  • Loading branch information
Joshua-K1 authored Jul 8, 2024
2 parents 78af3e3 + 8ad9fae commit efe4cd3
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 4 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/gha_pullrequest_reviewer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: CI PR Reviewer Pipeline

# Trigger the workflow when a pull request is opened or synchronized with the master branch,
# or manually triggered using the workflow_dispatch event.
on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
review:
runs-on: ubuntu-latest
env:
# Set environment variables for the job
X_API_KEY: ${{ secrets.AEP_API_KEY }}
X_API_CONSUMER: ${{ secrets.AEP_CONSUMER_UUID }}
API_HOST: "https://api.aep.clouddev.works/"
WORKING_DIRECTORY: ${{ github.workspace }}/

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Create a diff file
run: |
# Generate a diff file containing the changes between the current branch and the master branch
git diff origin/main...remotes/origin/${{ github.head_ref }} > ${{ env.working_directory }}diff.txt && cat ${{ env.working_directory }}diff.txt
- name: Generate a response
run: |
# Set variables for API request
API_HOST=$(printenv API_HOST)
WORKING_DIRECTORY=$(printenv WORKING_DIRECTORY)
X_API_CONSUMER=$(printenv X_API_CONSUMER)
X_API_KEY=$(printenv X_API_KEY)
DIFF_FILE="diff.txt"
RESPONSE_MD_FILE="response.md"
# Check if the diff file exists
if [ ! -f "${WORKING_DIRECTORY}${DIFF_FILE}" ]; then
echo "File ${WORKING_DIRECTORY}${DIFF_FILE} not found."
exit 1
fi
# Read the contents of the diff file and create a JSON body for the API request
file_contents=$(cat "${WORKING_DIRECTORY}${DIFF_FILE}")
json_body=$(jq -n --arg pt "pullrequest-review" --arg p "$file_contents" '{prompt_type: $pt, prompt: $p}')
# Send the API request and store the response
response=$(curl -s -i -X POST "${API_HOST}/predefined" \
-H "Content-Type: application/json" \
-H "X-API-CONSUMER: ${X_API_CONSUMER}" \
-H "X-API-KEY: ${X_API_KEY}" \
-d "$json_body")
echo "Response: $response"
# Extract the response code from the HTTP response headers
response_code=$(echo "$response" | awk -F' ' '/HTTP\/1.1/{print $2}' | head -n 1)
# Process the response based on the response code
if [ "$response_code" -eq 200 ]; then
echo "File contents sent successfully."
# Remove unnecessary headers from the response
response_body=$(echo "$response" | tail -n +2)
response_body=$(echo "$response_body" | sed '/^date: /Id' | sed '/^server: /Id' | sed '/^content-length: /Id' | sed '/^content-type: /Id')
# Remove trailing and leading quotes
response_body=$(echo "$response_body" | sed 's/^"\(.*\)"$/\1/')
# Remove the initial markdown code block identifier if it exists
response_body=$(echo "$response_body" | sed 's/```markdown//')
# Remove the last code block identifier
response_body=$(echo "$response_body" | sed 's/```//')
# Write the response body to a file
echo -e "$response_body" > "${WORKING_DIRECTORY}${RESPONSE_MD_FILE}"
else
echo "Error sending file contents: $response_code"
echo -e "Request to AEP failed to process" > "${WORKING_DIRECTORY}${RESPONSE_MD_FILE}"
fi
# Check if the response was written to the file successfully
if [ $? -eq 0 ]; then
echo "Response saved as response.md"
else
echo "Error writing to file in ${WORKING_DIRECTORY}."
exit 1
fi
- name: Get the response as a variable
id: get_response
run: |
# Read the response from the file and set it as an environment variable
{
echo 'response<<EOF'
cat ${WORKING_DIRECTORY}response.md
echo EOF
} >> "$GITHUB_ENV"
# Uses the response to create a comment on the pull request
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.response
})
112 changes: 112 additions & 0 deletions .github/workflows/gha_pullrequest_summarise.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: CI PR Summary Pipeline

# Trigger the workflow when a pull request is opened or updated on the 'master' branch,
# or manually triggered using the workflow_dispatch event.
on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
review:
runs-on: ubuntu-latest
env:
# Set environment variables
X_API_KEY: ${{ secrets.AEP_API_KEY }}
X_API_CONSUMER: ${{ secrets.AEP_CONSUMER_UUID }}
API_HOST: "https://api.aep.clouddev.works/"
WORKING_DIRECTORY: ${{ github.workspace }}/

steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Create a diff file
run: |
# Generate a diff file between the current branch and 'origin/master'
git diff origin/main...remotes/origin/${{ github.head_ref }} > ${{ env.working_directory }}diff.txt && cat ${{ env.working_directory }}diff.txt
- name: Generate a response
run: |
# Set variables
API_HOST=$(printenv API_HOST)
WORKING_DIRECTORY=$(printenv WORKING_DIRECTORY)
X_API_CONSUMER=$(printenv X_API_CONSUMER)
X_API_KEY=$(printenv X_API_KEY)
DIFF_FILE="diff.txt"
RESPONSE_MD_FILE="response.md"
# Check if the diff file exists
if [ ! -f "${WORKING_DIRECTORY}${DIFF_FILE}" ]; then
echo "File ${WORKING_DIRECTORY}${DIFF_FILE} not found."
exit 1
fi
# Read the contents of the diff file
file_contents=$(cat "${WORKING_DIRECTORY}${DIFF_FILE}")
# Create a JSON body for the API request
json_body=$(jq -n --arg pt "pullrequest-summary-perfile" --arg p "$file_contents" '{prompt_type: $pt, prompt: $p}')
# Send a POST request to the API
response=$(curl -s -i -X POST "${API_HOST}/predefined" \
-H "Content-Type: application/json" \
-H "X-API-CONSUMER: ${X_API_CONSUMER}" \
-H "X-API-KEY: ${X_API_KEY}" \
-d "$json_body")
echo "Response: $response"
# Extract the response code from the HTTP response
response_code=$(echo "$response" | awk -F' ' '/HTTP\/1.1/{print $2}' | head -n 1)
if [ "$response_code" -eq 200 ]; then
echo "File contents sent successfully."
# Remove headers from the response
response_body=$(echo "$response" | tail -n +2)
response_body=$(echo "$response_body" | sed '/^date: /Id' | sed '/^server: /Id' | sed '/^content-length: /Id' | sed '/^content-type: /Id')
response_body=$(echo "$response_body" | sed 's/^"\(.*\)"$/\1/') # Remove trailing and leading quotes
response_body=$(echo "$response_body" | sed 's/```markdown//') # Remove the initial markdown code block identifier if it exists
response_body=$(echo "$response_body" | sed 's/```//') # Remove the last code block identifier
# Write the response body to a file
echo -e "$response_body" > "${WORKING_DIRECTORY}${RESPONSE_MD_FILE}"
else
echo "Error sending file contents: $response_code"
echo -e "Request to AEP failed to process" > "${WORKING_DIRECTORY}${RESPONSE_MD_FILE}"
fi
if [ $? -eq 0 ]; then
echo "Response saved as response.md"
else
echo "Error writing to file in ${WORKING_DIRECTORY}."
exit 1
fi
- name: Get the response as a variable
id: get_response
run: |
# Set the 'response' environment variable with the contents of the response.md file
{
echo 'response<<EOF'
cat ${WORKING_DIRECTORY}response.md
echo EOF
} >> "$GITHUB_ENV"
# Update the PR body with the AEP API response
- uses: actions/github-script@v7
with:
script: |
const prBody = context.payload.pull_request.body || '';
const updatedBody = prBody.includes('## 🤖AEP PR SUMMARY🤖')
? prBody.replace(/## 🤖AEP PR SUMMARY🤖[\s\S]*/, '') + '\n\n## 🤖AEP PR SUMMARY🤖\n\n' + process.env.response
: prBody + '\n\n## 🤖AEP PR SUMMARY🤖\n\n' + process.env.response;
github.rest.pulls.update({
pull_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: updatedBody
})
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ jobs:
echo EOF
} >> "$GITHUB_ENV"
- uses: actions/github-script@v6
# Uses the response to create a comment on the pull request
- uses: actions/github-script@v7
with:
script: |
# Create a comment on the pull request with the response
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ jobs:
echo EOF
} >> "$GITHUB_ENV"
- uses: actions/github-script@v6
# Update the PR body with the AEP API response
- uses: actions/github-script@v7
with:
script: |
# Update the pull request body with the response
const prBody = context.payload.pull_request.body || '';
const updatedBody = prBody.includes('## 🤖AEP PR SUMMARY🤖')
? prBody.replace(/## 🤖AEP PR SUMMARY🤖[\s\S]*/, '') + '\n\n## 🤖AEP PR SUMMARY🤖\n\n' + process.env.response
Expand Down

0 comments on commit efe4cd3

Please sign in to comment.