Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
Set up core files for repo, describe purpose of repo, configure pre-commit and set up GitHub workflows.
  • Loading branch information
tp832944 committed Dec 5, 2024
0 parents commit 9dac3ba
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cspell/custom_misc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GCHQ
4 changes: 4 additions & 0 deletions .cspell/library_terms.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
autoupdate
coreax
jumanjihouse
vars
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Force all files to be indexed with LF line endings except those listed below
* text
107 changes: 107 additions & 0 deletions .github/workflows/pre_commit_autoupdate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
name: Autoupdate pre-commit config

on:
schedule:
- cron: "0 0 * * 0"

jobs:
pre_commit_autoupdate:
env:
config_path: .pre-commit-config.yaml
update_message_path: /tmp/message.txt
update_branch: chore/pre-commit-autoupdate
runs-on: ubuntu-latest
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.WRITE_CONTENTS_PR_APP }}
private-key: ${{ secrets.WRITE_CONTENTS_PR_KEY }}
- uses: actions/checkout@v4
with:
ref: main
- name: Create or checkout update branch
id: create_branch
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
export pr_number=$( gh pr view ${{ env.update_branch }} --json number --jq '.number' )
export pr_state=$( gh pr view ${{ env.update_branch }} --json state --jq '.state' )
echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
echo "pr_state=$pr_state" >> "$GITHUB_OUTPUT"
if git fetch origin ${{ env.update_branch }}; then
# If branch wasn't deleted after merge, do so here
if [ "$pr_state" = "MERGED" ]; then
git push -d origin ${{ env.update_branch }}
git checkout -b ${{ env.update_branch }}
git push origin ${{ env.update_branch }}
fi
git checkout ${{ env.update_branch }}
else
git checkout -b ${{ env.update_branch }}
git push origin ${{ env.update_branch }}
fi
# Pre-commit setup and autoupdate steps
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.13
cache: pip
- name: Upgrade Pip
run: python -m pip install --upgrade pip
- name: Install pre-commit
run: pip install pre-commit
- name: Store hash of baseline pre-commit config for comparison
id: old_file
run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT
- name: Overwrite config on branch with version from main
run: git checkout main ${{ env.config_path }}
- name: Store hash of main pre-commit config for comparison
id: main_file
run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT
- name: Run pre-commit autoupdate on main pre-commit config
id: autoupdate
run: |
pre-commit autoupdate > ${{ env.update_message_path }}
sed -i "/updating/!d" ${{ env.update_message_path }}
- name: Store hash of new pre-commit config for comparison
id: new_file
run: echo "hash=$( sha256sum $config_path )" >> $GITHUB_OUTPUT
# Commit authoring and pull-request creation/updating
- name: Commit (with signature) pre-commit config
id: commit
if: steps.old_file.outputs.hash != steps.new_file.outputs.hash
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run: |
export message="chore(deps): autoupdate pre-commit hooks"
export sha=$( git rev-parse ${{ env.update_branch }}:${{ env.config_path }} )
export content=$( base64 -i ${{ env.config_path }} )
gh api --method PUT /repos/:owner/:repo/contents/${{ env.config_path }} \
--field message="$message" \
--field content="$content" \
--field branch=${{ env.update_branch }} \
--field sha="$sha"
- name: Create or update pre-commit-autoupdate pull-request
if: steps.commit.conclusion == 'success' || ( steps.main_file.outputs.hash != steps.new_file.outputs.hash )
env:
GH_TOKEN: ${{ steps.generate-token.outputs.token }}
run : |
export title="chore(deps): autoupdate pre-commit hooks"
export body=$( cat ${{ env.update_message_path }} )
export pr_number=${{ steps.create_branch.outputs.pr_number }}
export pr_state=${{ steps.create_branch.outputs.pr_state }}
# If the PR is closed, can it be reopened, or is the PR already open?
if ( [ "$pr_state" = "CLOSED" ] && gh pr reopen $pr_number ) || [ "$pr_state" = "OPEN" ]; then
gh api --method PATCH /repos/:owner/:repo/pulls/$pr_number \
--field title="$title" \
--field body="$body"
else
# If a PR doesn't already exist, and no previous PR can be reopened, create a new PR.
gh pr create -t "$title" -b "$body" -l dependencies -B main -H ${{ env.update_branch }}
fi
27 changes: 27 additions & 0 deletions .github/workflows/pre_commit_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Pre-commit Checks

on:
push:
branches:
- main
pull_request:
branches:
- "**"

jobs:
pre_commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.13
cache: pip
- name: Upgrade Pip
run: python -m pip install --upgrade pip
- name: Install pre-commit
run: pip install pre-commit
- name: Run Pre-commit checks
run: SKIP=no-commit-to-branch pre-commit run -a --show-diff-on-failure
Empty file added .gitignore
Empty file.
64 changes: 64 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
default_install_hook_types:
- pre-commit
- pre-merge-commit
- commit-msg
default_stages:
- pre-commit
- pre-merge-commit
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
# Reject commits that add large files (coverage.xml, for example)
# Consider adjusting kB limit
- id: check-added-large-files
args:
- --enforce-all
- --maxkb=5000
# Check for files that would conflict in case-insensitive filesystems
- id: check-case-conflict
# Check for files that contain merge conflict strings
- id: check-merge-conflict
# Check syntax of data files
- id: check-json
- id: check-toml
- id: check-yaml
# Files must end in a single newline
- id: end-of-file-fixer
# Remove whitespace at the end of lines
- id: trailing-whitespace
# Prevent commit to main/master
- id: no-commit-to-branch
# Sort spell check custom dictionary
- id: file-contents-sorter
files: ^.cspell/.+.txt$
args:
- --ignore-case
- --unique
- repo: local
hooks:
# Check files are valid UTF-8
- id: require-utf8
name: Check file encoding
description: Ensure file is valid UTF-8
entry: python pre_commit_hooks/require_utf8.py
language: python
# Keep requirements-docs.txt in sync with uv.lock
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.5
hooks:
# No tabs, only spaces
- id: forbid-tabs
- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v8.16.0
hooks:
# Run a spellcheck (words pulled from cspell.config.yaml)
- id: cspell
stages:
- pre-commit
- pre-merge-commit
- commit-msg
exclude_types:
- gitignore
exclude: .cspell/.*
10 changes: 10 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Contributor guidelines

We strongly encourage contributions to Coreax. Please follow the guidelines
[there](https://github.com/gchq/coreax/blob/main/CONTRIBUTING.md). Issues should be
reported to the main Coreax [issues board](https://github.com/gchq/coreax/issues).

## Pre-commit

This repo runs pre-commit checks. Please run `pre-commit install` before making any
commits.
Loading

0 comments on commit 9dac3ba

Please sign in to comment.