Skip to content

Commit 4613057

Browse files
author
Brian Obot
committed
Add Pre-commit configuration and Github Action workflow
1 parent 65295ba commit 4613057

File tree

3 files changed

+126
-14
lines changed

3 files changed

+126
-14
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Quality Check & PR to Master
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout Code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v5
18+
with:
19+
python-version: '3.11' # You can change this to your preferred version
20+
cache: 'pip'
21+
22+
- name: Install Dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
26+
pip install pytest # Ensures pytest is available even if not in requirements.txt
27+
28+
# - name: Create .env file
29+
# run: |
30+
31+
# - name: Run Tests
32+
# run: |
33+
# mkdir logs
34+
# pytest -s
35+
36+
create_pull_request:
37+
runs-on: ubuntu-latest
38+
needs: test # This creates the dependency link
39+
if: github.actor == 'brianobot'
40+
permissions:
41+
contents: write
42+
pull-requests: write
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.MY_PERSONAL_TOKEN }}
45+
steps:
46+
- name: Checkout Code
47+
uses: actions/checkout@v4
48+
49+
- name: Create Pull Request
50+
run: |
51+
# Check if a PR already exists from dev to master
52+
PR_EXISTS=$(gh pr list --head dev --base master --state open --json number -q '.[0].number')
53+
54+
if [ -z "$PR_EXISTS" ]; then
55+
echo "No open PR found. Creating a new one..."
56+
gh pr create \
57+
--head dev \
58+
--base master \
59+
--title "Auto-PR: Dev to Master" \
60+
--body "Automated PR triggered by push from ${{ github.actor }}" \
61+
--assignee "${{ github.actor }}"
62+
else
63+
echo "PR already exists: #$PR_EXISTS. Skipping creation."
64+
fi

.pre-commit-config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.5.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- id: check-added-large-files
9+
10+
# 1. Sort Imports (Isort)
11+
- repo: https://github.com/pycqa/isort
12+
rev: 5.12.0
13+
hooks:
14+
- id: isort
15+
args: ["--profile", "black"]
16+
17+
# 2. Format Code (Black)
18+
- repo: https://github.com/psf/black
19+
rev: 23.11.0
20+
hooks:
21+
- id: black
22+
23+
# 3. Linting (Ruff - extremely fast linter)
24+
- repo: https://github.com/astral-sh/ruff-pre-commit
25+
rev: v0.1.6
26+
hooks:
27+
- id: ruff
28+
args: [ --fix ]
29+
30+
# 4. Type Checking (Mypy)
31+
- repo: https://github.com/pre-commit/mirrors-mypy
32+
rev: v1.7.1
33+
hooks:
34+
- id: mypy
35+
additional_dependencies:
36+
- pydantic
37+
- types-requests # If you use requests
38+
- types-setuptools # This usually replaces what pkg-resources needed
39+
- types-PyYAML # If you use YAML configs

_2024/day_1.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,56 @@ use std::fs;
44

55
#[allow(dead_code)]
66
fn calculate_total_distance_diff(locations_a: Vec<u32>, locations_b: Vec<u32>) -> u32 {
7+
/*
8+
Takes 2 list of integers and calculates the difference between each ordered element from each list
9+
and return the total difference from the calculation.
10+
*/
711
let mut location_a = locations_a.clone();
812
let mut location_b = locations_b.clone();
9-
13+
1014
// Sort both list first
1115
location_a.sort();
1216
location_b.sort();
13-
17+
1418
let mut total = 0;
1519
for (a, b) in location_a.iter().zip(location_b.iter()) {
1620
total += cmp::max(a, b) - cmp::min(a, b);
1721
}
18-
22+
1923
total
2024
}
21-
22-
25+
26+
2327
fn calculate_total_similarity_score(locations_a: Vec<i32>, locations_b: Vec<i32>) -> i32 {
28+
/*
29+
Takes 2 list of integers and calculates the total similarity score for each value in the first list
30+
the similarity score is the product of the item in the first list and the number of occurrence of that value
31+
in the second list.
32+
*/
2433
let mut total = 0;
25-
34+
2635
for location_id in locations_a {
2736
let similarity_score = locations_b.iter().filter(|item| **item == location_id).count() as i32 * location_id;
2837
total += similarity_score;
2938
}
30-
31-
total
39+
40+
total
3241
}
33-
42+
3443

3544
fn main() {
3645
let mut list_a = Vec::new();
3746
let mut list_b = Vec::new();
38-
47+
3948
let content = fs::read_to_string("_2024/data/day_1_input.txt").expect("Failed to Read Input File");
40-
49+
4150
for line in content.lines() {
4251
let numbers = line.split_whitespace().map(|item| item.parse::<i32>().expect("Fail to convert to integer")).collect::<Vec<_>>();
43-
52+
4453
list_a.push(numbers[0]);
4554
list_b.push(numbers[1]);
4655
}
47-
56+
4857
let result = calculate_total_similarity_score(list_a, list_b);
4958
println!("Result = {result}");
50-
}
59+
}

0 commit comments

Comments
 (0)