Skip to content

Commit

Permalink
feat: add Makefile, linter and sa
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Aug 27, 2024
1 parent 33ce813 commit 0fc78ef
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 14 deletions.
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[**.sh]
max_line_length = 120

[**.md]
indent_size = 4

[{Makefile,**.mk,.git*}]
indent_style = tab

[{lib/*,README.md}]
indent_size = unset
2 changes: 1 addition & 1 deletion .github/CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ at [https://contributor-covenant.org/version/1/3/0/][version]

[homepage]: https://contributor-covenant.org

[version]: https://contributor-covenant.org/version/1/3/0/
[version]: https://contributor-covenant.org/version/1/3/0/
100 changes: 100 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Contributing to bashunit

## We have a Code of Conduct

Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By participating in this project you agree to abide by its terms.

## Any contributions you make will be under the MIT License

When you submit code changes, your submissions are understood to be under the same [MIT](https://github.com/TypedDevs/bashunit/blob/main/LICENSE) that covers the project. By contributing to this project, you agree that your contributions will be licensed under its MIT.

## Write bug reports with detail, background, and sample code

In your bug report, please provide the following:

* A quick summary and/or background
* Steps to reproduce
* Be specific!
* Give sample code if you can.
* What you expected would happen
* What actually happens
* Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

Please post code and output as text ([using proper markup](https://guides.github.com/features/mastering-markdown/)). Additional screenshots to help contextualize behavior are ok.

## Workflow for Pull Requests

1. Fork/clone the repository.
2. Create your branch from `main` if you plan to implement new functionality or change existing code significantly.
3. Implement your change and add tests for it.
4. Ensure the test suite passes.
5. Ensure the code complies with our coding guidelines (see below).
6. Send that pull request!

Please make sure you have [set up your username and email address](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup) for use with Git. Strings such as `silly nick name <root@localhost>` looks bad in the commit history of a project.

## Testing

Run tests from the library:
```bash
# using make
make test

# using bashunit
lib/bashunit tests
```

Run the test with a watcher for development:
this will require to have installed [fswatcher](https://github.com/emcrisostomo/fswatch)
```bash
# you have to install `watch` for your OS
make test/watch
```

## Coding Guidelines

### ShellCheck

To contribute to this repository you must have [ShellCheck](https://github.com/koalaman/shellcheck) installed on your local machine or IDE, since it is the static code analyzer that is being used in continuous integration pipelines.

Installation: https://github.com/koalaman/shellcheck#installing

#### Example of usage

```bash
# using make
make sa

# using ShellCheck itself
shellcheck ./**/**/*.sh -C
```

### editorconfig-checker

To contribute to this repository, consider installing [editorconfig-checker](https://github.com/editorconfig-checker/editorconfig-checker) to check all project files regarding the `.editorconfig` to ensure we all fulfill the standard.

Installation: https://github.com/editorconfig-checker/editorconfig-checker#installation

To run it, use the following command:
```bash
# using make
make lint

# using editorconfig-checker itself
ec -config .editorconfig
```

This command will be executed on the CI to ensure the project's quality standards.

#### We recommend

To install the pre-commit of the project with the following command:

**Please note that you will need to have ShellCheck and editorconfig-checker installed on your computer.**
See above how to install in your local.

```bash
make pre_commit/install
```

[Shell Guide](https://google.github.io/styleguide/shellguide.html#s7.2-variable-names) by Google Conventions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
92 changes: 92 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
SHELL=/bin/bash

-include .env

STATIC_ANALYSIS_CHECKER := $(shell which shellcheck 2> /dev/null)
LINTER_CHECKER := $(shell which ec 2> /dev/null)
GIT_DIR = $(shell git rev-parse --git-dir 2> /dev/null)

OS:=
ifeq ($(OS),Windows_NT)
OS +=WIN32
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
OS +=_AMD64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
OS +=_IA32
endif
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
OS+=LINUX
endif
ifeq ($(UNAME_S),Darwin)
OS+=OSX
endif
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_P),x86_64)
OS +=_AMD64
endif
ifneq ($(filter %86,$(UNAME_P)),)
OS+=_IA32
endif
ifneq ($(filter arm%,$(UNAME_P)),)
OS+=_ARM
endif
endif

help:
@echo ""
@echo "Usage: make [command]"
@echo ""
@echo "Commands:"
@echo " test Run the tests"
@echo " test/list List all tests under the tests directory"
@echo " test/watch Automatically run tests every second"
@echo " env/example Copy variables without the values from .env into .env.example"
@echo " pre_commit/install Install the pre-commit hook"
@echo " pre_commit/run Function that will be called when the pre-commit hook runs"
@echo " sa Run shellcheck static analysis tool"
@echo " lint Run editorconfig linter tool"

SRC_SCRIPTS_DIR=src
TEST_SCRIPTS_DIR=tests
EXAMPLE_TEST_SCRIPTS=./example/logic_test.sh
PRE_COMMIT_SCRIPTS_FILE=./bin/pre-commit

TEST_SCRIPTS = $(wildcard $(TEST_SCRIPTS_DIR)/*/*[tT]est.sh)

test/list:
@echo "Test scripts found:"
@echo $(TEST_SCRIPTS) | tr ' ' '\n'

test: $(TEST_SCRIPTS)
@lib/bashunit $(TEST_SCRIPTS)

test/watch: $(TEST_SCRIPTS)
@lib/bashunit $(TEST_SCRIPTS)
@fswatch -m poll_monitor -or $(SRC_SCRIPTS_DIR) $(TEST_SCRIPTS_DIR) .env Makefile | xargs -n1 lib/bashunit $(TEST_SCRIPTS)

env/example:
@echo "Copying variables without the values from .env into .env.example"
@sed 's/=.*/=/' .env > .env.example

pre_commit/install:
@echo "Installing pre-commit hook"
cp $(PRE_COMMIT_SCRIPTS_FILE) $(GIT_DIR)/hooks/

pre_commit/run: test sa lint env/example

sa:
ifndef STATIC_ANALYSIS_CHECKER
@printf "\e[1m\e[31m%s\e[0m\n" "Shellcheck not installed: Static analysis not performed!" && exit 1
else
@shellcheck ./**/*.sh -C && printf "\e[1m\e[32m%s\e[0m\n" "ShellCheck: OK!"
endif

lint:
ifndef LINTER_CHECKER
@printf "\e[1m\e[31m%s\e[0m\n" "Editorconfig not installed: Lint not performed!" && exit 1
else
@ec -config .editorconfig && printf "\e[1m\e[32m%s\e[0m\n" "editorconfig-check: OK!"
endif
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ To build the project yourself, you can do it manually or execute it `./build.sh`
## How to use it?

Optional env vars:
- `PR_TICKET_LINK_PREFIX=https://your-company.atlassian.net/browse/`
- This will be replaced in the placeholder `{{TICKET_LINK}}` in your PR template
- `PR_TICKET_LINK_PREFIX=https://your-company.atlassian.net/browse/`
- This will be replaced in the placeholder `{{TICKET_LINK}}` in your PR template
- If no value is present for `PR_TICKET_LINK_PREFIX` then the `{{TICKET_LINK}}` will be ignored
- It will use the ticket key and number to append to the link prefix
- `PR_TEMPLATE_DIR=.github/PULL_REQUEST_TEMPLATE.md`
Expand Down
13 changes: 7 additions & 6 deletions create-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ fi

# Create the PR with the specified options
if ! gh pr create --title "$PR_TITLE" \
--base "$BASE_BRANCH" \
--head "$BRANCH_NAME" \
--assignee "$ASSIGNEE" \
--label "$LABEL" \
--body "$PR_BODY"; then
error_and_exit "Failed to create the pull request. Ensure you have the correct permissions and the repository is properly configured."
--base "$BASE_BRANCH" \
--head "$BRANCH_NAME" \
--assignee "$ASSIGNEE" \
--label "$LABEL" \
--body "$PR_BODY"; then
error_and_exit "Failed to create the pull request."\
"Ensure you have the correct permissions and the repository is properly configured."
fi

echo "Pull request created successfully."
2 changes: 1 addition & 1 deletion src/check_os.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ elif [[ "$(uname)" == "Darwin" ]]; then
_OS="OSX"
elif [[ $(uname) == *"MINGW"* ]]; then
_OS="Windows"
fi
fi
2 changes: 1 addition & 1 deletion src/dev/debug.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ function debug_var() {
printf "[%s] %s: %s=%s\n" "${_COLOR_FAINT}DEBUG${_COLOR_DEFAULT}" \
"${_COLOR_GREEN}${BASH_SOURCE[1]}:${BASH_LINENO[0]}" \
"$var_name" "$var_value"
}
}
2 changes: 1 addition & 1 deletion src/generic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function validate_the_branch_has_commits() {

function validate_base_branch_exists() {
if ! git show-ref --verify --quiet "refs/heads/$BASE_BRANCH"; then
error_and_exit "Base branch '$BASE_BRANCH' does not exist. Please check the base branch name or create it."
error_and_exit "Base branch '$BASE_BRANCH' does not exist. Please check the base branch name or create it."
fi
}

2 changes: 1 addition & 1 deletion src/pr_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function format_title() {
prefix=$(echo "$input" | cut -d'/' -f1)
input="${input#*/}"

case "$prefix" in
case "$prefix" in
fix|bug|bugfix) prefix="Fix" ;;
*) prefix="" ;;
esac
Expand Down

0 comments on commit 0fc78ef

Please sign in to comment.