Skip to content

Commit

Permalink
creating a new pr and adding the config files
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorOKane-Kainos committed Aug 22, 2024
1 parent 30f3553 commit 47f64f8
Show file tree
Hide file tree
Showing 11 changed files with 547 additions and 95 deletions.
97 changes: 97 additions & 0 deletions .github/workflows/pr-reviewer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: CI PR Reviewer Pipeline
on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
review:
runs-on: ubuntu-latest
env:
X_API_KEY: ${{ secrets.SYSTEM_API_KEY }}
X_API_CONSUMER: ${{ secrets.SYSTEM_CONSUMER_UUID }}
API_HOST: "https://app-gippi-api-s-latest-uksouth.azurewebsites.net/"
WORKING_DIRECTORY: ${{ github.workspace }}/
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Create a diff file
run: |
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: |
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"
if [ ! -f "${WORKING_DIRECTORY}${DIFF_FILE}" ]; then
echo "File ${WORKING_DIRECTORY}${DIFF_FILE} not found."
exit 1
fi
file_contents=$(cat "${WORKING_DIRECTORY}${DIFF_FILE}")
json_body=$(jq -n --arg pt "pullrequest-review" --arg p "$file_contents" '{prompt_type: $pt, prompt: $p}')
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"
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
response_body=$(echo "$response" | tail -n +2)
# Remove more headers
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 ident if it exists
response_body=$(echo "$response_body" | sed 's/```markdown//')
# remove the last code block ident
response_body=$(echo "$response_body" | sed 's/```//')
# Write to 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: |
{
echo 'response<<EOF'
cat ${WORKING_DIRECTORY}response.md
echo EOF
} >> "$GITHUB_ENV"
- uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.response
})
101 changes: 101 additions & 0 deletions .github/workflows/pr-summary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: CI PR Summary Pipeline
on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
review:
runs-on: ubuntu-latest
env:
X_API_KEY: ${{ secrets.SYSTEM_API_KEY }}
X_API_CONSUMER: ${{ secrets.SYSTEM_CONSUMER_UUID }}
API_HOST: "https://app-gippi-api-s-latest-uksouth.azurewebsites.net/"
WORKING_DIRECTORY: ${{ github.workspace }}/
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Create a diff file
run: |
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: |
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"
if [ ! -f "${WORKING_DIRECTORY}${DIFF_FILE}" ]; then
echo "File ${WORKING_DIRECTORY}${DIFF_FILE} not found."
exit 1
fi
file_contents=$(cat "${WORKING_DIRECTORY}${DIFF_FILE}")
json_body=$(jq -n --arg pt "pullrequest-summary-perfile" --arg p "$file_contents" '{prompt_type: $pt, prompt: $p}')
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"
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
response_body=$(echo "$response" | tail -n +2)
# Remove more headers
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 ident if it exists
response_body=$(echo "$response_body" | sed 's/```markdown//')
# remove the last code block ident
response_body=$(echo "$response_body" | sed 's/```//')
# Write to 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: |
{
echo 'response<<EOF'
cat ${WORKING_DIRECTORY}response.md
echo EOF
} >> "$GITHUB_ENV"
- uses: actions/github-script@v6
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
})
33 changes: 28 additions & 5 deletions .github/workflows/update-repos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
workflow_dispatch:

jobs:
update-file:
update-files:
runs-on: ubuntu-latest

steps:
Expand All @@ -23,15 +23,38 @@ jobs:
python -m pip install --upgrade pip
pip install pyyaml requests
- name: Run update script
- name: Run set_org_custom_properties script
env:
OAUTH_TOKEN: ${{ secrets.OAUTH_TOKEN }}
run: python custom-properties/set_org_custom_properties.py

- name: Run update-repo-list script
run: python scripts/update-repo-list.py

- name: Run update-readme script
run: python scripts/update-readme.py

- name: Install jq
run: sudo apt-get install jq -y

- name: List Repositories
run: |
echo "Listing Repositories"
for repo in $(jq -r '.[]' < ./production-repos.json); do
echo "Listing repository: $repo"
curl -H "Authorization: token ${{ secrets.OAUTH_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/hmcts/$repo
done
shell: bash
continue-on-error: true

- name: Commit and push changes
run: |
git config --global user.name 'hmcts-platform-operations'
git config --global user.email 'github-platform-operations@HMCTS.NET'
git add production-repos.json
git commit -m 'Update repository list'
git add production-repos.json readme.md
git commit -m 'Update repository list and readme'
git push
env:
GITHUB_TOKEN: ${{ secrets.OAUTH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.OAUTH_TOKEN }}
12 changes: 2 additions & 10 deletions components/data.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
data "github_team" "admin" {
slug = "test"
slug = "platform-operations" # Add more teams here if you want to exlcude them from the rulesets
}

data "local_file" "repos_json" {
filename = "${path.module}./production-repos.json"
}

data "github_branch" "existing_branches" {
for_each = {
for combo in local.repo_branch_combinations : "${combo.repo}:${combo.branch}" => combo
}
repository = each.value.repo
branch = each.value.branch
}
}
43 changes: 2 additions & 41 deletions components/locals.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
locals {
# List of repositories to exclude from the production-repos.json file
excluded_repositories = [
"test-repo-uteppyig",
]
excluded_repositories = [] # Add any repositories here you would like to exclude

# Read repositories from JSON file
all_repositories = jsondecode(data.local_file.repos_json.content)
Expand All @@ -12,41 +10,8 @@ locals {
for repo in local.all_repositories : repo
if !contains(local.excluded_repositories, repo)
]

branches_to_check = ["main", "master"]
batch_size = 10

# Split repositories into batches of 10 to help handle the API Rate limits
repo_batches = chunklist(local.included_repositories, local.batch_size)

repo_branch_combinations = flatten([
for batch in local.repo_batches : [
for repo in batch : [
for branch in local.branches_to_check : {
repo = repo
branch = branch
}
]
]
])

# Create a map of existing branches
existing_branches = {
for key, branch in data.github_branch.existing_branches :
key => branch
}

# Checks if a main/master branch exists on the repositories
branch_summary = {
for repo in local.included_repositories :
repo => {
main = contains(keys(local.existing_branches), "${repo}:main")
master = contains(keys(local.existing_branches), "${repo}:master")
}
}
}


locals {
env_display_names = {
sbox = "Sandbox"
Expand All @@ -64,8 +29,4 @@ locals {
"costCentre" = ""
}
enforced_tags = module.tags.common_tags
}




}
1 change: 0 additions & 1 deletion components/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ resource "github_organization_ruleset" "default_ruleset" {
bypass_mode = "always"
}
}

22 changes: 1 addition & 21 deletions components/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,4 @@ output "common_tags" {
Product = var.product
BuiltFrom = var.builtFrom
}
}


# This outout below will summarise how many repos have a master, main or both branches on the repos
output "branch_count" {
value = {
total_repos = length(local.included_repositories)
repos_with_main = sum([for repo, branches in local.branch_summary : branches.main ? 1 : 0])
repos_with_master = sum([for repo, branches in local.branch_summary : branches.master ? 1 : 0])
repos_with_both = sum([for repo, branches in local.branch_summary : (branches.main && branches.master) ? 1 : 0])
}
description = "Summary of branch counts"
}

# output "existing_branches" {
# value = keys(local.existing_branches)
# }

# output "branch_summary" {
# value = local.branch_summary
# }
}
Loading

0 comments on commit 47f64f8

Please sign in to comment.