Added code

This commit is contained in:
2023-05-23 12:46:15 +02:00
parent 428b99ad83
commit 5788bd7a43
4 changed files with 155 additions and 2 deletions

9
Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM python:3.11.3-slim-bullseye
RUN apt update && apt add --no-cache bash git openssh python3 py3-pip
RUN pip install --no-cache-dir -q --upgrade pip
RUN pip install --no-cache-dir -q autoflake
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

View File

@@ -1,2 +1,52 @@
# autoflake_action
Automatically runs the autoflake command against any commit/ pull request created
# GitHub autoflake Action
This GitHub action automatically removes unused imports and variables from your Python code using [autoflake](https://pypi.org/project/autoflake/).
## Usage
### Parameters
The following parameters can be used in your custom action configuration.
| Parameter | Required | Default | Description |
| - | - | - | - |
| commit_message | :x: | 'Removed unused imports and variables' | Custom git commit message |
| commit_options | :x: | - | Custom git commit options |
| file_pattern | :x: | '*' | Custom file pattern for `git add` |
| checkpath | :x: | '.' | The path autoflake checks |
| no_commit | :x: | False | Avoid committing, if used in a pipeline |
| options | :x: | ' ' | Parameters to use with autoflake |
| dry | :x: | false | Dry-run the action to fail when detecting uncompliant files, instead of automatically fixing them. |
### Example
This is a simple usage example of this script:
```yaml
# This action works with pull requests and pushes
name: Continuous Integration
on:
pull_request:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: creyD/action_autoflake@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
## Issues
Please report all bugs and feature request using the [GitHub issues function](https://github.com/creyD/autoflake_action/issues/new).
## Special Thanks
- Stefan Zweifel [(GitHub)](https://github.com/stefanzweifel) for providing an action to commit and push a git change

41
action.yml Normal file
View File

@@ -0,0 +1,41 @@
name: Autoflake Action
description: Automatically runs autoflake on all your changes.
author: Conrad Großer <grosserconrad@gmail.com>
inputs:
commit_message:
description: Commit message
required: false
default: 'Removed unused imports and variables'
commit_options:
description: Commit options
required: false
file_pattern:
description: File pattern used for `git add`
required: false
default: '*'
checkpath:
description: Path autoflake checks
required: false
default: '.'
options:
description: Parameters for autoflake
required: false
default: ''
dry:
description: Should this script apply autoflake directly or just warn?
required: false
default: false
no_commit:
description: Can be used to avoid committing the changes
required: false
default: false
runs:
using: 'docker'
image: 'Dockerfile'
branding:
icon: 'delete'
color: 'green'

53
entrypoint.sh Executable file
View File

@@ -0,0 +1,53 @@
#!/bin/bash
# e is for exiting the script automatically if a command fails, u is for exiting if a variable is not set
# x would be for showing the commands before they are executed
set -eu
# FUNCTIONS
# Function for setting up git env in the docker container (copied from https://github.com/stefanzweifel/git-auto-commit-action/blob/master/entrypoint.sh)
_git_setup() {
cat <<- EOF > $HOME/.netrc
machine github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
machine api.github.com
login $GITHUB_ACTOR
password $GITHUB_TOKEN
EOF
chmod 600 $HOME/.netrc
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Action"
}
# Checks if any files are changed
_git_changed() {
[[ -n "$(git status -s)" ]]
}
echo "Running autoflake..."
autoflake $INPUT_CHECKPATH $INPUT_OPTIONS || echo "Problem running autoflake!"
if $INPUT_NO_COMMIT; then
exit 0
fi
if _git_changed;
then
if $INPUT_DRY; then
echo "Found non-compliant files!"
exit 1
else
# Calling method to configure the git environemnt
_git_setup
echo "Commiting and pushing changes..."
# Add changes to git
git add "${INPUT_FILE_PATTERN}" || echo "Problem adding your files with pattern ${INPUT_FILE_PATTERN}"
# Commit and push changes back
git commit -m "$INPUT_COMMIT_MESSAGE" --author="$GITHUB_ACTOR <$GITHUB_ACTOR@users.noreply.github.com>" ${INPUT_COMMIT_OPTIONS:+"$INPUT_COMMIT_OPTIONS"}
git push origin
echo "Changes pushed successfully."
fi
else
echo "Nothing to commit. Exiting."
fi