Skip to content

Commit 8ddcec1

Browse files
authored
Reimplement in Python (#26)
2 parents d3f4b78 + 890e5e4 commit 8ddcec1

31 files changed

+763
-243
lines changed

.devcontainer/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.10
2+
3+
ENV PYTHONDONTWRITEBYTECODE=1 \
4+
PYTHONUNBUFFERED=1 \
5+
USER=compiler
6+
7+
# create $USER and home directory
8+
RUN useradd --create-home --shell /bin/bash $USER && \
9+
chown -R $USER /home/$USER
10+
11+
# switch to non-root $USER
12+
USER $USER
13+
14+
# update PATH for local pip installs
15+
ENV PATH "$PATH:/home/$USER/.local/bin"
16+
17+
# update pip
18+
RUN python -m pip install --upgrade pip
19+
20+
# enter src directory
21+
WORKDIR /home/$USER/src
22+
23+
# copy and install packages
24+
COPY . .
25+
RUN pip install -r .devcontainer/requirements.txt
26+
RUN pip install -r tests/requirements.txt

.devcontainer/compose.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: compilerla/conventional-pre-commit
2+
3+
services:
4+
dev:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
entrypoint: []
9+
command: sleep infinity
10+
image: compilerla/conventional-pre-commit:dev
11+
volumes:
12+
- ../:/home/compiler/src

.devcontainer/devcontainer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "compilerla/conventional-pre-commit",
3+
"dockerComposeFile": ["./compose.yml"],
4+
"service": "dev",
5+
"runServices": ["dev"],
6+
"workspaceFolder": "/home/compiler/src",
7+
"postAttachCommand": "pip install -e .",
8+
// Set *default* container specific settings.json values on container create.
9+
"settings": {
10+
"terminal.integrated.defaultProfile.linux": "bash",
11+
"terminal.integrated.profiles.linux": {
12+
"bash": {
13+
"path": "/bin/bash"
14+
}
15+
}
16+
},
17+
// Add the IDs of extensions you want installed when the container is created.
18+
"extensions": [
19+
"eamodio.gitlens",
20+
"esbenp.prettier-vscode",
21+
"mhutchie.git-graph",
22+
"ms-python.python",
23+
"ms-python.vscode-pylance"
24+
]
25+
}

.devcontainer/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
black
2+
build
3+
flake8
4+
pre-commit
5+
twine

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length = 127

.github/workflows/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.10

.github/workflows/github-stats/index.js

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,58 @@ const octokit = require("@octokit/core");
66
const client = new octokit.Octokit({ auth: process.env.GITHUB_TOKEN });
77

88
async function searchApi(page) {
9-
console.log(`Searching page ${page}`);
10-
return client.request("GET /search/code", {
11-
q: "compilerla/conventional-pre-commit+in:file+language:yaml",
12-
per_page: 100,
13-
page: page
14-
});
9+
console.log(`Searching page ${page}`);
10+
return client.request("GET /search/code", {
11+
q: "compilerla/conventional-pre-commit+in:file+language:yaml",
12+
per_page: 100,
13+
page: page,
14+
});
1515
}
1616

1717
async function searchHookUsage() {
18-
try{
19-
let page = 1;
20-
const { _, data } = await searchApi(page++);
18+
try {
19+
let page = 1;
20+
const { _, data } = await searchApi(page++);
2121

22-
let incomplete = data.incomplete_results;
22+
let incomplete = data.incomplete_results;
2323

24-
while (incomplete) {
25-
const { h, d } = await searchApi(page++);
26-
data.items.concat(d.items);
27-
incomplete = d.incomplete_results;
28-
}
29-
30-
console.log("Finished searching");
31-
return data;
32-
}
33-
catch (error) {
34-
console.log(error);
35-
return {};
24+
while (incomplete) {
25+
const { h, d } = await searchApi(page++);
26+
data.items.concat(d.items);
27+
incomplete = d.incomplete_results;
3628
}
29+
30+
console.log("Finished searching");
31+
return data;
32+
} catch (error) {
33+
console.log(error);
34+
return {};
35+
}
3736
}
3837

3938
function updateReadme(repos, owners) {
40-
console.log("Updating README");
39+
console.log("Updating README");
4140

42-
const path = "../../../README.md";
43-
const current = fs.readFileSync(path, "utf-8");
41+
const path = "../../../README.md";
42+
const current = fs.readFileSync(path, "utf-8");
4443

45-
const pattern = /<!\-\- github_stats starts \-\->.*<!\-\- github_stats ends \-\->/s
46-
const stats = `:octocat: **${repos} public repos** across :woman_technologist: :man_technologist: **${owners} users/orgs** on GitHub use this hook!`;
47-
const content = `<!-- github_stats starts -->\n${stats}\n<!-- github_stats ends -->`;
44+
const pattern =
45+
/<!\-\- github_stats starts \-\->.*<!\-\- github_stats ends \-\->/s;
46+
const stats = `:octocat: **${repos} public repos** across :woman_technologist: :man_technologist: **${owners} users/orgs** on GitHub use this hook!`;
47+
const content = `<!-- github_stats starts -->\n${stats}\n<!-- github_stats ends -->`;
4848

49-
const updated = current.replace(pattern, content);
50-
fs.writeFileSync(path, updated, "utf-8");
49+
const updated = current.replace(pattern, content);
50+
fs.writeFileSync(path, updated, "utf-8");
5151

52-
console.log("Finished updating README");
52+
console.log("Finished updating README");
5353
}
5454

5555
searchHookUsage().then((data) => {
56-
const hookRefs = data.items.filter(d => d.name == ".pre-commit-config.yaml");
57-
const repos = new Set(hookRefs.map(d => d.repository.full_name));
58-
const owners = new Set(hookRefs.map(d => d.repository.owner.login));
56+
const hookRefs = data.items.filter(
57+
(d) => d.name == ".pre-commit-config.yaml"
58+
);
59+
const repos = new Set(hookRefs.map((d) => d.repository.full_name));
60+
const owners = new Set(hookRefs.map((d) => d.repository.owner.login));
5961

60-
updateReadme(repos.size, owners.size);
62+
updateReadme(repos.size, owners.size);
6163
});

.github/workflows/publish.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
jobs:
9+
publish:
10+
name: Publish to PyPI
11+
runs-on: ubuntu-latest
12+
if: "startsWith(github.event.release.tag_name, 'v')"
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- uses: actions/setup-python@v3
17+
with:
18+
python-version-file: .github/workflows/.python-version
19+
20+
- name: Install build dependencies
21+
run: pip install build
22+
23+
- name: Build package
24+
run: python -m build
25+
26+
- name: Publish to Test PyPI
27+
uses: pypa/gh-action-pypi-publish@release/v1.5
28+
if: "github.event.release.prerelease"
29+
with:
30+
username: __token__
31+
password: ${{ secrets.PYPI_TEST_API_TOKEN }}
32+
33+
- name: Publish to PyPI
34+
uses: pypa/gh-action-pypi-publish@release/v1.5
35+
if: "!github.event.release.prerelease"
36+
with:
37+
username: __token__
38+
password: ${{ secrets.PYPI_API_TOKEN }}

.github/workflows/tests.yml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
name: Tests
22

33
on:
4-
pull_request:
5-
branches: [ "*" ]
4+
push:
5+
branches: ["*"]
66

77
jobs:
88
tests:
9+
name: Tests
910
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
python-version: ["3.7", "3.8", "3.9", "3.10"]
14+
1015
steps:
11-
- uses: actions/checkout@v2
12-
- uses: actions/setup-python@v2
13-
- name: Install pre-commit
16+
- uses: actions/checkout@v3
17+
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v3
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
23+
- name: Install dependencies
1424
run: |
1525
python -m pip install --upgrade pip
16-
pip install pre-commit
26+
pip install -r tests/requirements.txt
27+
pip install -e .
28+
1729
- name: Run tests
18-
run: ./tests.sh
30+
run: ./tests/run.sh

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.egg-info
2+
*.py[co]
3+
.coverage
4+
dist

0 commit comments

Comments
 (0)