Skip to content

Commit

Permalink
Chore: Update DevOps tooling from central repository [skip ci]
Browse files Browse the repository at this point in the history
Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ModeSevenIndustrialSolutions authored and github-actions[bot] committed May 15, 2024
1 parent 33e37a3 commit 65c7184
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .devops-exclusions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.github/workflows/linting.yaml
.github/workflows/notebooks.yaml
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[flake8]
max-line-length = 160
extend-ignore = E203, E501
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# Temporary devops repo
.devops

# Twine temporary files
package-lock.json
package.json

# Output files from co2budget.ipynb (ITR-Examples)
OECM-images
TPI-images


# Local node cache
node_modules

Expand Down
10 changes: 10 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
# Markdownlint configuration file

# Default state for all rules
default: true

# Path to configuration file to extend
extends: null

MD013: false
106 changes: 51 additions & 55 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ci:
skip:
# pre-commit.ci cannot install WGET, so tomlint must be disabled
- tomllint
# - pre-commit-update

exclude: |
(?x)^(
Expand All @@ -14,12 +13,6 @@ exclude: |
repos:

# - repo: https://gitlab.com/vojko.pribudic/pre-commit-update
# rev: v0.1.0
# hooks:
# - id: pre-commit-update
# args: [--dry-run]

- repo: local
hooks:
- id: tomllint
Expand Down Expand Up @@ -52,7 +45,6 @@ repos:
args: ["--fix=lf"]
- id: name-tests-test
args: ["--pytest-test-first"]
# Do not allow direct push to main/master branches
- id: no-commit-to-branch
# - id: pretty-format-json
- id: requirements-txt-fixer
Expand All @@ -64,7 +56,7 @@ repos:
hooks:
- id: prettier
args:
['--ignore-unknown']
['--no-error-on-unmatched-pattern', '--ignore-unknown']

# Lint: Markdown
- repo: https://github.com/igorshubovych/markdownlint-cli
Expand All @@ -73,21 +65,16 @@ repos:
- id: markdownlint
args: ["--fix"]

# - repo: https://github.com/asottile/pyupgrade
# rev: v3.15.0
# - repo: https://github.com/psf/black-pre-commit-mirror
# rev: 24.4.2
# hooks:
# - id: pyupgrade
# args: ['--py37-plus']
# - id: black
# - id: black-jupyter

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
hooks:
- id: black-jupyter
# - repo: https://github.com/tomcatling/black-nb
# rev: '0.7'
# hooks:
# - id: black-nb

- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.1
Expand All @@ -104,61 +91,70 @@ repos:
rev: v0.10.0.1
hooks:
- id: shellcheck
# Optionally only show errors and warnings
# args: ["--severity=warning"]

# If you want to avoid flake8 errors due to unused vars or imports:
# - repo: https://github.com/PyCQA/autoflake
# rev: v2.0.0
# hooks:
# - id: autoflake
# args: [
# --in-place,
# --remove-all-unused-imports,
# --remove-unused-variables,
# ]
- repo: https://github.com/pycqa/pydocstyle.git
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies: ["tomli"]

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
args: ["--profile", "black"]

- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
args: [--strict]

- repo: https://github.com/Mateusz-Grzelinski/actionlint-py
rev: v1.6.27.13
hooks:
- id: actionlint

# If like to embrace black styles even in the docs:
# - repo: https://github.com/asottile/blacken-docs
# rev: v1.13.0
# hooks:
# - id: blacken-docs
# additional_dependencies: [black]

- repo: https://github.com/pycqa/flake8
rev: "7.0.0"
hooks:
- id: flake8
entry: pflake8
additional_dependencies: [pyproject-flake8]
additional_dependencies:
- pep8-naming

# Check for misspells in documentation files:
# - repo: https://github.com/codespell-project/codespell
# rev: v2.2.2
# hooks:
# - id: codespell
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.35.1
hooks:
- id: yamllint
args: [ "-d", "{rules: {line-length: {max: 120}}, ignore-from-file: [.gitignore],}", ]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.10.0"
hooks:
- id: mypy
verbose: true
args: [--show-error-codes]
args: ["--show-error-codes", "--install-types", "--non-interactive"]
additional_dependencies: ["pytest", "types-requests"]

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
hooks:
- id: ruff
files: ^(scripts|tests|custom_components)/.+\.py$
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
files: ^(scripts|tests|custom_components)/.+\.py$

# Check for misspellings in documentation files
# - repo: https://github.com/codespell-project/codespell
# rev: v2.2.2
# hooks:
# - id: codespell

# To embrace black styles, even in docs
# - repo: https://github.com/asottile/blacken-docs
# rev: v1.13.0
# hooks:
# - id: blacken-docs
# additional_dependencies: [black]

# Automatically upgrade Python syntax for newer versions
# - repo: https://github.com/asottile/pyupgrade
# rev: v3.15.0
# hooks:
# - id: pyupgrade
# args: ['--py37-plus']
126 changes: 126 additions & 0 deletions scripts/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/usr/bin/env bash

### Script to bootstrap the OS-Climate DevOps environment ###

set -eu -o pipefail
# set -xv

### Variables ###

SOURCE_FILE="bootstrap.yaml"
WGET_URL="https://raw.githubusercontent.com/os-climate/devops-toolkit/main/.github/workflows/$SOURCE_FILE"
AUTOMATION_BRANCH="update-devops-tooling"
DEVOPS_DIR=".devops"

### Checks ###

GIT_CMD=$(which git)
if [ ! -x "$GIT_CMD" ]; then
echo "GIT command was NOT found in PATH"; exit 1
fi

WGET_CMD=$(which wget)
if [ ! -x "$WGET_CMD" ]; then
echo "WGET command was NOT found in PATH"; exit 1
fi

MKTEMP_CMD=$(which mktemp)
if [ ! -x "$MKTEMP_CMD" ]; then
echo "MKTEMP command was NOT found in PATH"; exit 1
fi

SHELL_SCRIPT=$(mktemp -t script-XXXXXXXX.sh)

### Functions ###

change_dir_error() {
echo "Could not change directory"; exit 1
}

check_for_local_branch() {
BRANCH="$1"
git show-ref --quiet refs/heads/"$BRANCH"
return $?
}

check_for_remote_branch() {
BRANCH="$1"
git ls-remote --exit-code --heads origin "$BRANCH"
return $?
}

cleanup_on_exit() {
# Remove PR branch, if it exists
echo "Cleaning up on exit: bootstrap.sh"
echo "Swapping from temporary branch to: $HEAD_BRANCH"
git checkout main > /dev/null 2>&1
if (check_for_local_branch "$AUTOMATION_BRANCH"); then
echo "Removing temporary local branch: $AUTOMATION_BRANCH"
git branch -d "$AUTOMATION_BRANCH" > /dev/null 2>&1
fi
if [ -f "$SHELL_SCRIPT" ]; then
echo "Removing temporary shell code"
rm "$SHELL_SCRIPT"
fi
if [ -d "$DEVOPS_DIR" ]; then
echo "Removed local copy of devops repository"
rm -Rf "$DEVOPS_DIR"
fi
}
trap cleanup_on_exit EXIT

### Main script entry point

# Get organisation and repository name
# git config --get remote.origin.url
# git@github.com:ModeSevenIndustrialSolutions/test-bootstrap.git
URL=$(git config --get remote.origin.url)

# Take the above and store it converted as ORG_AND_REPO
# e.g. ModeSevenIndustrialSolutions/test-bootstrap
ORG_AND_REPO=${URL/%.git}
ORG_AND_REPO=${ORG_AND_REPO//:/ }
ORG_AND_REPO=$(echo "$ORG_AND_REPO" | awk '{ print $2 }')
HEAD_BRANCH=$("$GIT_CMD" rev-parse --abbrev-ref HEAD)
REPO_DIR=$(git rev-parse --show-toplevel)
# Change to top-level of GIT repository
CURRENT_DIR=$(pwd)
if [ "$REPO_DIR" != "$CURRENT_DIR" ]; then
echo "Changing directory to: $REPO_DIR"
cd "$REPO_DIR" || change_dir_error
fi

# Get latest copy of bootstrap workflow
if [ -f "$SOURCE_FILE" ]; then
echo "Removing existing copy of: $SOURCE_FILE"
rm "$SOURCE_FILE"
fi
echo "Pulling latest DevOps bootstrap workflow from:"
echo " $WGET_URL"
"$WGET_CMD" -q "$WGET_URL"

# The section below extracts shell code from the YAML file
echo "Extracting shell code from: $SOURCE_FILE"
EXTRACT="false"
while read -r LINE; do
if [ "$LINE" = "#SHELLCODESTART" ]; then
EXTRACT="true"
SHELL_SCRIPT=$(mktemp -t script-XXXXXXXX.sh)
touch "$SHELL_SCRIPT"
chmod a+x "$SHELL_SCRIPT"
echo "Creating shell script: $SHELL_SCRIPT"
echo "#!/bin/sh" > "$SHELL_SCRIPT"
fi
if [ "$EXTRACT" = "true" ]; then
echo "$LINE" >> "$SHELL_SCRIPT"
if [ "$LINE" = "#SHELLCODEEND" ]; then
break
fi
fi
done < "$SOURCE_FILE"

echo "Running extracted shell script code"
# https://www.shellcheck.net/wiki/SC1090
# Shell code executed is temporary and cannot be checked by linting
# shellcheck disable=SC1090
. "$SHELL_SCRIPT"

0 comments on commit 65c7184

Please sign in to comment.