Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Actions Code Formatter workflow #7940

Merged
merged 38 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7101340
Initial commit
ep-93 Sep 12, 2024
018f66b
Initial commit
ep-93 Sep 12, 2024
89960d0
Initial commit
ep-93 Sep 12, 2024
10514f7
Debugging
ep-93 Sep 12, 2024
47b5e18
Debugging
ep-93 Sep 12, 2024
e8391dd
Debugging
ep-93 Sep 12, 2024
fe862c3
Debugging
ep-93 Sep 12, 2024
2940914
Debugging
ep-93 Sep 12, 2024
c5a6a07
Debugging
ep-93 Sep 13, 2024
07da5e0
Debugging
ep-93 Sep 13, 2024
17d9849
Debugging
ep-93 Sep 13, 2024
80aa011
Debugging
ep-93 Sep 16, 2024
ae7f777
Debugging
ep-93 Sep 16, 2024
b887a5f
Debugging
ep-93 Sep 16, 2024
563d820
Slimmed down approach
ep-93 Sep 16, 2024
5ec06eb
Slimmed down approach
ep-93 Sep 16, 2024
6609535
Slimmed down approach
ep-93 Sep 16, 2024
d0bc2bf
Debugging
ep-93 Sep 16, 2024
fa4dab2
Debugging
ep-93 Sep 16, 2024
3bc3fc0
Debugging
ep-93 Sep 16, 2024
40b2d1a
Debugging
ep-93 Sep 16, 2024
9767717
Debugging
ep-93 Sep 16, 2024
ad7594b
undo
ep-93 Sep 16, 2024
0107ee9
Debugging
ep-93 Sep 16, 2024
c4deede
Debugging
ep-93 Sep 17, 2024
52b0da7
Debugging
ep-93 Sep 17, 2024
e16e9eb
Things to fix
ep-93 Sep 17, 2024
65b95fa
Things to fix
ep-93 Sep 17, 2024
7b2c979
Things to fix
ep-93 Sep 17, 2024
4cb3685
Things to fix
ep-93 Sep 19, 2024
fe2421a
Things to fix
ep-93 Sep 19, 2024
5b1f82e
Things to fix
ep-93 Sep 19, 2024
a1ba856
Things to fix
ep-93 Sep 19, 2024
8c69cea
Things to fix
ep-93 Sep 19, 2024
09d9349
Things to fix
ep-93 Sep 19, 2024
01b2a88
Things to fix
ep-93 Sep 19, 2024
06e7952
Things to fix
ep-93 Sep 19, 2024
d8196b3
Things to fix
ep-93 Sep 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions .github/workflows/format-code.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---

name: 'Format Code: ensure code formatting guidelines are met'

on:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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"
5 changes: 3 additions & 2 deletions terraform/environments/sprinkler/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
Loading