diff --git a/.github/workflows/format-code.yml b/.github/workflows/format-code.yml index 1967c4881..388d90f73 100644 --- a/.github/workflows/format-code.yml +++ b/.github/workflows/format-code.yml @@ -1,4 +1,4 @@ ---- + name: 'Format Code: ensure code formatting guidelines are met' on: @@ -33,7 +33,6 @@ jobs: date=$(date +%Y_%m_%d) branch_name="date_$date" git checkout -b $branch_name - - name: Run linter id: ml # You can override MegaLinter flavor used to have faster performances @@ -54,26 +53,79 @@ jobs: YAML_PRETTIER_FILTER_REGEX_EXCLUDE: (.github/*) MARKDOWN_MARKDOWNLINT_FILTER_REGEX_EXCLUDE: (terraform/modules/.*/.*.md) REPORT_OUTPUT_FOLDER: none - + - name: Check for changes run: | + # Show the status and diff before attempting to pull/push + echo "===== Git Status =====" + git status + + echo "===== Git Diff =====" + git diff + git add . - git commit -m "Updates from GitHub Actions Format Code workflow" || true - branch_name=$(git branch --show-current) - changes=$(git diff origin/main...$branch_name --name-only) + changes=$(git diff --staged --name-only) if [ -z "$changes" ]; then echo "No changes detected." else echo "Changes detected." echo "changes=true" >> $GITHUB_ENV + git diff --staged --name-only > changed_files.txt fi - - - name: Push changes + - name: Store the current commit OID and branch name + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + # Store the current commit OID and branch name + commit_oid=$(git rev-parse HEAD) + branch_name=$(git branch --show-current) + # Fetch the repository ID using GitHub CLI + repo_id=$(gh api graphql -f query=' + query { + repository(owner:"ministryofjustice", name:"modernisation-platform") { + id + } + }' --jq '.data.repository.id') + echo "Repository ID: $repo_id" + # Ensure that changed_files.txt is not empty + if [ ! -s changed_files.txt ]; then + echo "No files to commit." + exit 0 + fi + # Prepare the list of changed files for commit + files_for_commit=$(jq -Rn ' + [inputs | { "path": ., "contents": (inputs | @base64) }] + ' < changed_files.txt) + echo "Files for commit: $files_for_commit" + # Create the GraphQL mutation payload + mutation_payload=$(jq -n --arg branch_name "$branch_name" --arg commit_oid "$commit_oid" --arg repo_id "$repo_id" --arg commit_message "Automated code formatting fixes" --argjson files_for_commit "$files_for_commit" '{ + query: "mutation($input: CreateCommitOnBranchInput!) { createCommitOnBranch(input: $input) { commit { oid } } }", + variables: { + input: { + branch: { + repositoryNameWithOwner: "ministryofjustice/modernisation-platform", + branchName: $branch_name + }, + message: { + headline: $commit_message + }, + fileChanges: { + additions: $files_for_commit + }, + expectedHeadOid: $commit_oid + } + } + }') + # Send the mutation request to GitHub's GraphQL API + curl -X POST -H "Authorization: bearer $GITHUB_TOKEN" \ + -H "Content-Type: application/json" \ + -d "$mutation_payload" https://api.github.com/graphql + - name: Push changes using GraphQL if: env.changes == 'true' run: | git config --global push.autoSetupRemote true git push - + echo "Commit pushed successfully." - name: Create pull request if: env.changes == 'true' env: @@ -84,4 +136,4 @@ jobs: branch_name=$(git branch --show-current) pr_head="${{ github.repository_owner }}:${branch_name}" pr_base="main" - gh pr create --title "$pr_title" --body "$pr_body" --head "$pr_head" --base "$pr_base" --label "code quality" + gh pr create --title "$pr_title" --body "$pr_body" --head "$pr_head" --base "$pr_base" --label "code quality" \ No newline at end of file diff --git a/terraform/environments/sprinkler/locals.tf b/terraform/environments/sprinkler/locals.tf index d972e4b36..4977c6e5a 100644 --- a/terraform/environments/sprinkler/locals.tf +++ b/terraform/environments/sprinkler/locals.tf @@ -17,11 +17,12 @@ locals { { "is-production" = local.is-production }, { "environment-name" = terraform.workspace }, { "source-code" = "https://github.com/ministryofjustice/modernisation-platform" } + ) - environment = "sandbox" + environment = "sandbox" vpc_name = var.networking[0].business-unit - subnet_set = var.networking[0].set + subnet_set = var.networking[0].set vpc_all = "${local.vpc_name}-${local.environment}" subnet_set_name = "${var.networking[0].business-unit}-${local.environment}-${var.networking[0].set}"