33 Commits

Author SHA1 Message Date
c1b28d7f43 Merge branch 'master' of https://git.mamahaha.work/sangge/waka-readme 2024-01-22 00:43:51 +08:00
512d27739c temp change 2024-01-22 00:43:24 +08:00
ad3a3b38e6 update dockerfile 2024-01-20 00:43:13 +08:00
1d9eeae447 Merge pull request 'dev-gitea' (#3) from dev-gitea into master
Reviewed-on: #3
2024-01-18 23:04:08 +08:00
c0345cb5d9 Merge branch 'dev-gitea' of https://git.mamahaha.work/sangge/waka-readme into dev-gitea 2024-01-18 23:03:17 +08:00
632b13121d update some readme 2024-01-18 22:59:49 +08:00
e9ab3581aa Merge pull request 'remove requirements.txt' (#2) from dev-gitea into master
Reviewed-on: #2
2024-01-18 22:44:26 +08:00
8b0ab3bd76 Merge branch 'master' into dev-gitea 2024-01-18 22:43:30 +08:00
7a6d95d436 remove requirements.txt 2024-01-18 22:42:21 +08:00
fd26197e70 Merge pull request 'use pyproject.toml to control deps' (#1) from dev-gitea into master
Reviewed-on: #1
2024-01-18 22:40:43 +08:00
99b6b3f887 remove dev file 2024-01-18 22:35:01 +08:00
c2f0b9de48 use pyproject.toml to control deps 2024-01-18 22:29:09 +08:00
95c7f84451 use slim image 2024-01-16 00:19:01 +08:00
d1558ad6db update readme 2024-01-15 23:46:39 +08:00
76d3c7249a update gitea url 2024-01-15 21:17:44 +08:00
450ba61d22 Merge branch 'master' of https://git.mamahaha.work/sangge/waka-readme 2024-01-15 21:06:50 +08:00
3d3239a2e3 use 3.11 2024-01-15 21:06:17 +08:00
830c76c07a update requirement 2024-01-15 21:01:33 +08:00
6125862010 mute useless ci 2024-01-15 20:53:58 +08:00
b88da2af7d update requirements
Some checks failed
WakaReadme CI / UnitTests (push) Failing after 13s
2024-01-15 20:52:49 +08:00
b55e75a74d update container
Some checks failed
WakaReadme CI / UnitTests (push) Failing after 13s
2024-01-15 20:38:23 +08:00
3bc9d82ee4 feat: update action 2024-01-15 20:36:55 +08:00
980f869999 finish dev version
Some checks failed
WakaReadme CI / UnitTests (push) Failing after 26s
2024-01-15 20:02:19 +08:00
108a90abce test: update
Some checks failed
WakaReadme CI / UnitTests (push) Failing after 19s
2024-01-15 02:38:12 +08:00
Jovial Joe Jayarson
5dc349128a Merge pull request #152 from yozachar/workshop
chore: monthly updates for Dec'23
2023-12-25 13:06:38 +05:30
Jovial Joe Jayarson
d2e7ba7208 chore: monthly updates for Dec'23 2023-12-24 22:17:15 +05:30
Jovial Joe Jayarson
2dd14d3fd8 Merge pull request #149 from yozachar/workshop
chore: monthly updates for Nov'23
2023-11-09 08:44:27 +05:30
Jovial Joe Jayarson
e644272268 chore: monthly updates for Nov'23 2023-11-08 11:48:30 +05:30
Jovial Joe Jayarson
2aab71918a Merge pull request #148 from yozachar/workshop
chore: monthly updates for Oct'23; bump version
2023-10-23 09:48:07 +05:30
Jovial Joe Jayarson
3f2f9d748e chore: monthly updates for Oct'23; bump version 2023-10-21 08:12:29 +05:30
Andrew Hong
54c3312012 feat: adds flag IGNORE_LANGUAGES (#146)
- feat: adds flag `IGNORE_LANGUAGES`  (#146)

---------

Co-authored-by: Jovial Joe Jayarson <jovial7joe@hotmail.com>
2023-10-21 07:58:43 +05:30
Jovial Joe Jayarson
dd576f44c6 Merge pull request #144 from joe733/workshop
chore: monthly updates for Sep'23; bump version
2023-09-19 16:23:00 +05:30
Jovial Joe Jayarson
b2363ac606 chore: monthly updates for Sep'23; bump version
- fix: 0 discovered test in container
- uses containerfile for testing
- update dependencies, bump version
2023-09-17 15:55:22 +05:30
11 changed files with 201 additions and 144 deletions

View File

@@ -17,6 +17,7 @@ INPUT_SHOW_TIME=
INPUT_SHOW_TOTAL=
INPUT_SHOW_MASKED_TIME=
INPUT_STOP_AT_OTHER=
INPUT_IGNORED_LANGUAGES=
# commit
INPUT_COMMIT_MESSAGE=
INPUT_TARGET_BRANCH=

View File

@@ -2,9 +2,9 @@ name: WakaReadme CI
on:
push:
branches: [master]
branches: [main]
pull_request:
branches: [master]
branches: [main]
workflow_dispatch:
jobs:

4
.gitignore vendored
View File

@@ -107,6 +107,7 @@ poetry.lock
# However, this project does not rely on pdm for production.
pdm.lock
.pdm-python
.pdm-build
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
@@ -162,8 +163,9 @@ cython_debug/
# VSCode
.vscode/
# asdf
# asdf/rtx
.tool-versions
.rtx.toml
# ruff
.ruff_cache

View File

@@ -1,6 +1,6 @@
# Contributing
![python_ver](https://img.shields.io/badge/Python-%3E%3D3.11-blue.svg)
![python_ver](https://img.shields.io/badge/Python-%3E%3D3.12-blue.svg)
> First off, thank you! Please follow along.
@@ -21,6 +21,7 @@ INPUT_TIME_RANGE=last_7_days
INPUT_SHOW_MASKED_TIME=false
INPUT_LANG_COUNT=0
INPUT_STOP_AT_OTHER=true
INPUT_IGNORED_LANGUAGES=
```
**NEVER commit this `.env` file!**
@@ -32,10 +33,8 @@ INPUT_STOP_AT_OTHER=true
> Replace `podman` with `docker` everywhere, if you're using the latter.
```sh
# Build
$ podman-compose -p waka-readme -f ./docker-compose.yml up -d
# Logs
$ podman logs WakaReadmeDev
# Build and watch logs
$ podman-compose -p waka-readme -f ./docker-compose.yml up
# Cleanup
$ podman-compose -p waka-readme -f ./docker-compose.yml down
```
@@ -48,17 +47,18 @@ $ podman-compose -p waka-readme -f ./docker-compose.yml down
1. Inside the cloned folder run the following commands to install dependencies
```console
```sh
$ python -m venv .venv
$ . ./.venv/bin/activate
$ python -m pip install .
# ... install decencies ...
```
to activate virtual environment & install dependencies.
2. To test or execute the program in development, run:
```console
```sh
(.venv)$ python -m unittest discover # run tests
(.venv)$ python -m main --dev # execute program in dev mode
```

View File

@@ -1,27 +1,10 @@
<p align="center">
<img
src="https://socialify.git.ci/athul/waka-readme/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&name=1&owner=1&pulls=1&stargazers=1&theme=Auto"
alt="waka-readme"
width="640"
height="320"
/>
</p>
# Dev Metrics in Readme
# Dev Metrics in Readme [![Unit Tests](https://github.com/athul/waka-readme/actions/workflows/testing.yml/badge.svg?branch=master)](https://github.com/athul/waka-readme/actions/workflows/testing.yml)
fork from [waka-readme](https://github/athul/waka-readme)
edit for Gitea use.
[WakaTime](https://wakatime.com) coding metrics on your profile readme.
<!-- prettier-ignore-start -->
<picture>
<source srcset="https://github.com/athul/waka-readme/assets/38415384/60a6bcd0-01f8-421a-8730-7e872d216e09"
media="(prefers-color-scheme: dark)" />
<img src="https://github.com/athul/waka-readme/assets/38415384/29541cbc-0e39-47c4-93c2-514032b47276"
alt="new_secrets_actions" />
</picture>
<!-- prettier-ignore-end -->
:speech_balloon: **Forum** | [GitHub discussions][gh_discuss]
## New to WakaTime?
> Nope? Skip to [#Prep work](#prep-work).
@@ -39,7 +22,7 @@ Alternatively, you can fetch data from WakaTime compatible services such as [Wak
## Prep Work
A GitHub repository and a `README.md` file is required. We'll be making use of readme in the [profile repository][profile_readme].
A Gitea repository and a `README.md` file is required. We'll be making use of readme in the [profile repository][profile_readme].
- Save the `README.md` file after copy-pasting the following special comments. Your dev-metics will show up in between.
@@ -51,31 +34,9 @@ A GitHub repository and a `README.md` file is required. We'll be making use of r
`<!--START_SECTION: -->` and `<!--END_SECTION: -->` are placeholders and must be retained as is. Whereas "`waka`" can be replaced by any alphanumeric string. See [#Tweaks](#tweaks) section for more.
- Navigate to your repo's `Settings`:
- Go to `Secrets` (at `https://github.com/USERNAME/USERNAME/settings/secrets/actions/new` by replacing the `USERNAME` with your own username) and add a new secret "_Named_" `WAKATIME_API_KEY` with your API key as it's "_Secret_".
- Go to `Secrets` (at `https://your_gitea_url/username/.profile/settings/actions/secrets` by replacing the `USERNAME` with your own username) and add a new secret "_Named_" `WAKATIME_API_KEY` with your API key as it's "_Secret_".
<!-- prettier-ignore-start -->
<picture>
<source srcset="https://github.com/athul/waka-readme/assets/38415384/04dee9dc-65a1-43f9-9df4-7545646b9a72"
media="(prefers-color-scheme: dark)" />
<img src="https://github.com/athul/waka-readme/assets/38415384/9110cc3e-66cc-46ed-89d4-36644aa258e1"
alt="new_secrets_actions" />
</picture>
<!-- prettier-ignore-end -->
> If you're not using [profile repository][profile_readme], add another secret "_Named_" `GH_TOKEN` and in place of "_Secret_" insert your [GitHub token][gh_access_token].
- Go to `Workflow permissions` under `Actions` (at `https://github.com/USERNAME/USERNAME/settings/actions` by replacing the `USERNAME` with your own username) and set `Read and write permissions`.
<!-- prettier-ignore-start -->
<picture>
<source srcset="https://github.com/athul/waka-readme/assets/38415384/a1b86a0b-4065-4ff1-847b-b52e681bf247"
media="(prefers-color-scheme: dark)" />
<img src="https://github.com/athul/waka-readme/assets/38415384/de9cb7d0-fd40-43cf-8fed-7c6f57207788"
alt="new_secrets_actions" />
</picture>
<!-- prettier-ignore-end -->
- Create a new workflow file named `waka-readme.yml` inside `.github/workflows/` folder of your repository.
- Create a new workflow file named `waka-readme.yml` inside `.gitea/workflows/` folder of your profile repository.
- Clear all existing contents, add following lines and save the file.
```yml
@@ -93,7 +54,7 @@ A GitHub repository and a `README.md` file is required. We'll be making use of r
name: WakaReadme DevMetrics
runs-on: ubuntu-latest
steps:
- uses: athul/waka-readme@master
- uses: https://git.mamahaha.work/sangge/waka-readme@master # this action name
with:
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
```
@@ -113,18 +74,19 @@ There are many flags that you can modify as you see fit.
### Content Tweaks
| Environment flag | Options (`Default`, `Other`, ...) | Description |
| ------------------ | ----------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| `SHOW_TITLE` | `false`, `true` | Add title to waka-readme stats blob |
| `SECTION_NAME` | `waka`, any alphanumeric string | The generator will look for section name to fill up the readme. |
| `BLOCKS` | `░▒▓█`, `⣀⣄⣤⣦⣶⣷⣿`, `-#`, `=>`, you can be creative | Ascii art used to build stats graph |
| `CODE_LANG` | `txt`, `python` `ruby` `json` , you can use other languages also | Language syntax based highlighted text |
| `TIME_RANGE` | `last_7_days`, `last_30_days`, `last_6_months`, `last_year`, `all_time` | String representing a dispensation from which stats are aggregated |
| `LANG_COUNT` | `5`, any plausible number | Number of languages to be displayed |
| `SHOW_TIME` | `true`, `false` | Displays the amount of time spent for each language |
| `SHOW_TOTAL` | `false`, `true` | Show total coding time |
| `SHOW_MASKED_TIME` | `false`, `true` | Adds total coding time including unclassified languages (overrides: `SHOW_TOTAL`) |
| `STOP_AT_OTHER` | `false`, `true` | Stop when language marked as `Other` is retrieved (overrides: `LANG_COUNT`) |
| Environment flag | Options (`Default`, `Other`, ...) | Description |
| ------------------- | ----------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| `SHOW_TITLE` | `false`, `true` | Add title to waka-readme stats blob |
| `SECTION_NAME` | `waka`, any alphanumeric string | The generator will look for section name to fill up the readme. |
| `BLOCKS` | `░▒▓█`, `⣀⣄⣤⣦⣶⣷⣿`, `-#`, `=>`, you can be creative | Ascii art used to build stats graph |
| `CODE_LANG` | `txt`, `python` `ruby` `json` , you can use other languages also | Language syntax based highlighted text |
| `TIME_RANGE` | `last_7_days`, `last_30_days`, `last_6_months`, `last_year`, `all_time` | String representing a dispensation from which stats are aggregated |
| `LANG_COUNT` | `5`, any plausible number | Number of languages to be displayed |
| `SHOW_TIME` | `true`, `false` | Displays the amount of time spent for each language |
| `SHOW_TOTAL` | `false`, `true` | Show total coding time |
| `SHOW_MASKED_TIME` | `false`, `true` | Adds total coding time including unclassified languages (overrides: `SHOW_TOTAL`) |
| `STOP_AT_OTHER` | `false`, `true` | Stop when language marked as `Other` is retrieved (overrides: `LANG_COUNT`) |
| `IGNORED_LANGUAGES` | <code> </code>, `Binary YAML JSON TOML` | Hide languages from your stats |
### Commit Tweaks
@@ -159,13 +121,14 @@ jobs:
name: WakaReadme DevMetrics
runs-on: ubuntu-latest
steps:
- uses: athul/waka-readme@master
# this action name
- uses: https://git.mamahaha.work/sangge/waka-readme@master # do NOT replace with anything else
with:
GH_TOKEN: ${{ secrets.GH_TOKEN }} # optional if on profile readme
WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }} # required
### meta
API_BASE_URL: https://wakatime.com/api # optional
REPOSITORY: YOUR_GITHUB_USERNAME/YOUR_REPOSITORY_NAME # optional
REPOSITORY: YOUR_GITEA_USERNAME/YOUR_REPOSITORY_NAME # optional
### content
SHOW_TITLE: true # optional
SECTION_NAME: waka # optional
@@ -177,6 +140,7 @@ jobs:
SHOW_TOTAL: true # optional
SHOW_MASKED_TIME: false # optional
STOP_AT_OTHER: true # optional
IGNORED_LANGUAGES: YAML JSON TOML # optional
### commit
COMMIT_MESSAGE: Updated waka-readme graph with new metrics # optional
TARGET_BRANCH: master # optional
@@ -213,6 +177,7 @@ Other 47 hrs 58 mins >------------------------ 03.05 %
- If you are using `GH_TOKEN`, make sure set the [fine grained token](https://github.com/settings/tokens?type=beta) scope to repository contents with `read-and-write` access. See [#141 (comment)](https://github.com/athul/waka-readme/issues/141#issuecomment-1679831949).
- `WAKATIME_API_KEY` is a **required** secret. All other environment variables are optional.
- The above example does NOT show proper default values, refer [#Tweaks](#tweaks) for the same.
- `IGNORED_LANGUAGES` is suggested for [.NET](https://dotnet.microsoft.com) users, as WakaTime assumes you're working with `Binary`, while debugging.
## Why only the language stats (and not other data) from the API?
@@ -224,5 +189,3 @@ I am a fan of minimal designs and the profile readme is a great way to show off
[waka_plugins]: https://wakatime.com/plugins
[waka_help]: https://wakatime.com/help/editors
[profile_readme]: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/customizing-your-profile/managing-your-profile-readme
[gh_access_token]: https://docs.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token
[gh_discuss]: https://github.com/athul/waka-readme/discussions

View File

@@ -3,7 +3,7 @@ author: "Athul Cyriac Ajay"
description: "WakaTime coding activity graph in your profile readme"
inputs:
GH_TOKEN:
GITEA_TOKEN:
description: "GitHub access token with Repo scope"
default: ${{ github.token }}
required: true
@@ -62,6 +62,10 @@ inputs:
description: "Stop data retrieval when language marked 'Other' is reached"
default: "false"
required: false
IGNORED_LANGUAGES:
description: "Ignore space separated, listed languages"
default: ""
required: false
# commit tweaks
COMMIT_MESSAGE:

View File

@@ -5,7 +5,6 @@ services:
- .env.template
build:
context: .
dockerfile: dockerfile
dockerfile: containerfile
image: waka-readme:testing
container_name: WakaReadmeTesting
command: python -m unittest discover

51
containerfile Normal file
View File

@@ -0,0 +1,51 @@
FROM docker.io/python:3-slim
ENV INPUT_GITEA_TOKEN \
INPUT_WAKATIME_API_KEY \
# meta
INPUT_API_BASE_URL \
INPUT_REPOSITORY \
# content
INPUT_SHOW_TITLE \
INPUT_SECTION_NAME \
INPUT_BLOCKS \
INPUT_CODE_LANG \
INPUT_TIME_RANGE \
INPUT_LANG_COUNT \
INPUT_SHOW_TIME \
INPUT_SHOW_TOTAL \
INPUT_SHOW_MASKED_TIME \
INPUT_STOP_AT_OTHER \
INPUT_IGNORED_LANGUAGES \
# commit
INPUT_COMMIT_MESSAGE \
INPUT_TARGET_BRANCH \
INPUT_TARGET_PATH \
INPUT_COMMITTER_NAME \
INPUT_COMMITTER_EMAIL \
INPUT_AUTHOR_NAME \
INPUT_AUTHOR_EMAIL
ENV PATH="${PATH}:/root/.local/bin" \
# python
PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
PYTHONDONTWRITEBYTECODE=1 \
# pip
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DEFAULT_TIMEOUT=100
# copy project files
COPY --chown=root:root pyproject.toml main.py /app/
# install dependencies
RUN python -m pip install /app/ -i https://pypi.tuna.tsinghua.edu.cn/simple
# copy tests
COPY --chown=root:root tests /app/tests/
# run tests
CMD python -m unittest discover /app/

View File

@@ -16,6 +16,7 @@ ENV INPUT_GH_TOKEN \
INPUT_SHOW_TOTAL \
INPUT_SHOW_MASKED_TIME \
INPUT_STOP_AT_OTHER \
INPUT_IGNORED_LANGUAGES \
# commit
INPUT_COMMIT_MESSAGE \
INPUT_TARGET_BRANCH \
@@ -41,7 +42,7 @@ ENV PATH="${PATH}:/root/.local/bin" \
COPY --chown=root:root pyproject.toml main.py /app/
# install dependencies
RUN python -m pip install /app/
RUN python -m pip install /app/ -i https://pypi.tuna.tsinghua.edu.cn/simple
# execute program
CMD python /app/main.py

145
main.py
View File

@@ -28,7 +28,7 @@ Contents := Title + Byline + Body
"""
# standard
from base64 import b64encode
from base64 import b64encode, b64decode
from dataclasses import dataclass
from datetime import datetime
from functools import partial
@@ -42,13 +42,14 @@ from typing import Any
# external
from faker import Faker
from github import ContentFile, Github, GithubException, InputGitAuthor, Repository
# from github import ContentFile, Github, GithubException, InputGitAuthor, Repository
import gitea
from requests import get as rq_get
from requests.exceptions import RequestException
################### setup ###################
print()
# hush existing loggers
for lgr_name in logger.root.manager.loggerDict:
@@ -70,8 +71,12 @@ try:
# comment this out to disable colored logging
from loguru import logger
logger.debug("loguru loaded")
# load from .env before class def gets parsed
load_dotenv()
logger.debug("dotenv loaded")
waka_key: str | None = os.getenv("INPUT_WAKATIME_API_KEY")
except ImportError as im_err:
logger.warning(im_err)
@@ -127,10 +132,11 @@ class WakaInput:
# mapped environment variables
# # required
gh_token: str | None = os.getenv("INPUT_GH_TOKEN")
gitea_token: str | None = os.getenv("INPUT_GITEA_TOKEN")
gitea_url: str | None = os.getenv("GITHUB_SERVER_URL", "https://gitea.com")
waka_key: str | None = os.getenv("INPUT_WAKATIME_API_KEY")
api_base_url: str | None = os.getenv("INPUT_API_BASE_URL", "https://wakatime.com/api")
repository: str | None = os.getenv("INPUT_REPOSITORY")
repository: str | None = os.getenv("INPUT_REPOSITORY", ".profile")
# # depends
commit_message: str = os.getenv(
"INPUT_COMMIT_MESSAGE", "Updated WakaReadme graph with new metrics"
@@ -149,6 +155,7 @@ class WakaInput:
show_masked_time: str | bool = os.getenv("INPUT_SHOW_MASKED_TIME") or False
language_count: str | int = os.getenv("INPUT_LANG_COUNT") or 5
stop_at_other: str | bool = os.getenv("INPUT_STOP_AT_OTHER") or False
ignored_languages: str = os.getenv("INPUT_IGNORED_LANGUAGES", "")
# # optional meta
target_branch: str = os.getenv("INPUT_TARGET_BRANCH", "NOT_SET")
target_path: str = os.getenv("INPUT_TARGET_PATH", "NOT_SET")
@@ -160,7 +167,12 @@ class WakaInput:
def validate_input(self):
"""Validate Input Env Variables."""
logger.debug("Validating input variables")
if not self.gh_token or not self.waka_key or not self.api_base_url or not self.repository:
if (
not self.gitea_token
or not self.waka_key
or not self.api_base_url
or not self.repository
):
logger.error("Invalid inputs")
logger.info("Refer https://github.com/athul/waka-readme")
return False
@@ -270,7 +282,7 @@ def make_graph(block_style: str, percent: float, gr_len: int, lg_nm: str = "", /
return graph_bar
def prep_content(stats: dict[str, Any], language_count: int = 5, stop_at_other: bool = False, /):
def prep_content(stats: dict[str, Any], /):
"""WakaReadme Prepare Markdown.
Prepared markdown content from the fetched statistics.
@@ -306,17 +318,19 @@ def prep_content(stats: dict[str, Any], language_count: int = 5, stop_at_other:
max((str(lng["name"]) for lng in lang_info), key=len)
# and then do not for get to set `pad_len` to say 13 :)
)
if language_count == 0 and not stop_at_other:
language_count, stop_at_other = int(wk_i.language_count), bool(wk_i.stop_at_other)
if language_count == 0 and not wk_i.stop_at_other:
logger.debug(
"Set INPUT_LANG_COUNT to -1 to retrieve all language"
+ " or specify a positive number (ie. above 0)"
)
return contents.rstrip("\n")
ignored_languages = set[str](igl.lower() for igl in wk_i.ignored_languages.strip().split())
for idx, lang in enumerate(lang_info):
lang_name = str(lang["name"])
# >>> add languages to filter here <<<
# if lang_name in {...}: continue
if ignored_languages and lang_name.lower() in ignored_languages:
continue
lang_time = str(lang["text"]) if wk_i.show_time else ""
lang_ratio = float(lang["percent"])
lang_bar = make_graph(wk_i.block_style, lang_ratio, wk_i.graph_length, lang_name)
@@ -393,9 +407,7 @@ def churn(old_readme: str, /):
sys.exit(1)
# preparing contents
try:
generated_content = prep_content(
waka_stats, int(wk_i.language_count), bool(wk_i.stop_at_other)
)
generated_content = prep_content(waka_stats)
except (AttributeError, KeyError, ValueError) as err:
logger.error(f"Unable to read API data | {err}\n")
sys.exit(1)
@@ -406,54 +418,63 @@ def churn(old_readme: str, /):
repl=f"{wk_i.start_comment}\n\n```{wk_i.code_lang}\n{generated_content}\n```\n\n{wk_i.end_comment}",
string=old_readme,
)
logger.debug(new_readme)
if len(sys.argv) == 2 and sys.argv[1] == "--dev":
logger.debug("Detected run in `dev` mode.")
# to avoid accidentally writing back to Github
# when developing or testing waka-readme
return None
return None if new_readme == old_readme else new_readme
def qualify_target(gh_repo: Repository.Repository):
# def qualify_target(gitea_repo: Repository.Repository):
def qualify_target(gitea_repo: gitea.Repository, gitea_connect: gitea.Gitea):
"""Qualify target repository defaults."""
@dataclass
class TargetRepository:
this: ContentFile.ContentFile
# this: ContentFile.ContentFile
this: gitea.Content
path: str
commit_message: str
sha: str
branch: str
committer: InputGitAuthor | None
author: InputGitAuthor | None
# committer: InputGitAuthor | None
# author: InputGitAuthor | None
committer: None
author: None
gh_branch = gh_repo.default_branch
if wk_i.target_branch != "NOT_SET":
gh_branch = gh_repo.get_branch(wk_i.target_branch)
gitea_branch = gitea_repo.get_branches()[0].name
# if wk_i.target_branch != "NOT_SET":
# gitea_branch = gitea_repo.get_branch(wk_i.target_branch)
target = gh_repo.get_readme()
if wk_i.target_path != "NOT_SET":
target = gh_repo.get_contents(
path=wk_i.target_path,
ref=gh_branch if isinstance(gh_branch, str) else gh_branch.commit.sha,
)
readme_content = gitea.Content(gitea_connect)
readme_content.path = "README.md"
readme_content.type = gitea.Content.FILE
target = gitea_repo.get_file_content(readme_content) # base64 encoded
# target = gitea_repo.get_readme()
# if wk_i.target_path != "NOT_SET":
# target = gitea_repo.get_contents(
# path=wk_i.target_path,
# ref=gitea_branch if isinstance(gitea_branch, str) else gitea_branch.commit.sha,
# )
if isinstance(target, list):
raise RuntimeError("Cannot handle multiple files.")
committer, author = None, None
if wk_i.committer_name != "NOT_SET" and wk_i.committer_email != "NOT_SET":
committer = InputGitAuthor(name=wk_i.committer_name, email=wk_i.committer_email)
if wk_i.author_name != "NOT_SET" and wk_i.author_email != "NOT_SET":
author = InputGitAuthor(name=wk_i.author_name, email=wk_i.author_email)
# if wk_i.committer_name != "NOT_SET" and wk_i.committer_email != "NOT_SET":
# committer = InputGitAuthor(name=wk_i.committer_name, email=wk_i.committer_email)
# if wk_i.author_name != "NOT_SET" and wk_i.author_email != "NOT_SET":
# author = InputGitAuthor(name=wk_i.author_name, email=wk_i.author_email)
sha = ""
return TargetRepository(
this=target,
path=target.path,
path="README.md",
commit_message=wk_i.commit_message,
sha=target.sha,
branch=gh_branch if isinstance(gh_branch, str) else gh_branch.name,
sha=sha,
branch=gitea_branch if isinstance(gitea_branch, str) else gitea_branch.name,
committer=committer,
author=author,
)
@@ -461,34 +482,48 @@ def qualify_target(gh_repo: Repository.Repository):
def genesis():
"""Run Program."""
logger.debug("Connecting to GitHub")
gh_connect = Github(wk_i.gh_token)
logger.debug("Connecting to Gitea")
gitea_connect = gitea.Gitea(wk_i.gitea_url, wk_i.gitea_token)
logger.debug("logged into gitea\n")
# since a validator is being used earlier, casting
# `wk_i.ENV_VARIABLE` to a string here, is okay
gh_repo = gh_connect.get_repo(str(wk_i.repository))
target = qualify_target(gh_repo)
# gitea_repo = gitea_connect.get_repo(str(wk_i.repository))
owner = "sangge"
repo_name = ".profile"
gitea_repo = gitea.Repository.request(gitea_connect, owner, repo_name)
if len(sys.argv) == 2 and sys.argv[1] == "--dev":
logger.debug("Detected run in `dev` mode.,fetched repo \n")
target = qualify_target(gitea_repo, gitea_connect)
logger.debug("Decoding readme contents\n")
readme_contents = str(target.this.decoded_content, encoding="utf-8")
readme_contents = str(b64decode(target.this), encoding="utf-8")
if not (new_content := churn(readme_contents)):
logger.info("WakaReadme was not updated")
return
logger.debug("WakaReadme stats has changed")
update_metric = partial(
gh_repo.update_file,
path=target.path,
message=target.commit_message,
content=new_content,
sha=target.sha,
branch=target.branch,
)
if target.committer:
update_metric = partial(update_metric, committer=target.committer)
if target.author:
update_metric = partial(update_metric, author=target.author)
update_metric()
# update_metric = partial(
# gitea_repo.update_file,
# path=target.path,
# message=target.commit_message,
# content=new_content,
# sha=target.sha,
# branch=target.branch,
# )
# if target.committer:
# update_metric = partial(update_metric, committer=target.committer)
# if target.author:
# update_metric = partial(update_metric, author=target.author)
# update_metric()
b64_new_content = b64encode(bytes(new_content, "utf-8"))
repo_content = gitea_repo.get_git_content()
readmes = [c for c in repo_content if c.name == "README.md"]
str_new_content = b64_new_content.decode("utf-8")
gitea_repo.change_file(readmes[0].name, readmes[0].sha, str_new_content)
logger.info("Stats updated successfully")
return
################### driver ###################
@@ -517,7 +552,7 @@ if __name__ == "__main__":
except RuntimeError as err:
logger.error(f"{type(err).__name__}: {err}\n")
sys.exit(1)
except (GithubException, RequestException) as rq_exp:
logger.critical(f"{rq_exp}\n")
sys.exit(1)
# except (GithubException, RequestException) as rq_exp:
# logger.critical(f"{rq_exp}\n")
# sys.exit(1)
print("\nThanks for using WakaReadme!\n")

View File

@@ -4,7 +4,7 @@
[project]
name = "waka-readme"
version = "0.2.4"
version = "0.3.0"
description = "Wakatime Weekly Metrics on your Profile Readme."
authors = [{ name = "Athul Cyriac Ajay", email = "athul8720@gmail.com" }]
license = { text = "MIT" }
@@ -15,8 +15,8 @@ classifiers = [
"Programming Language :: Python",
"Typing :: Typed",
]
requires-python = ">=3.11"
dependencies = ["faker>=19.3.0", "pygithub>=1.59.1", "requests>=2.31.0"]
requires-python = ">=3.12"
dependencies = ["faker>=21.0.0", "pygithub>=2.1.1", "requests>=2.31.0", "py-gitea>=0.2.6"]
[project.urls]
Homepage = "https://github.com/athul/waka-readme"
@@ -29,13 +29,14 @@ Changelog = "https://github.com/athul/waka-readme/commits/master"
#############################
[project.optional-dependencies]
dev = ["loguru>=0.7.0", "python-dotenv>=1.0.0"]
tooling = [
"bandit>=1.7.5",
"black>=23.7.0",
"ruff>=0.0.284",
"pyright>=1.1.323",
]
extra = ["loguru>=0.7.2", "python-dotenv>=1.0.0"]
#############################
# Development Dependencies #
#############################
[tool.pdm.dev-dependencies]
tooling = ["bandit>=1.7.6", "black>=23.12.1", "ruff>=0.1.9", "pyright>=1.1.342"]
####################
# Configurations #
@@ -46,11 +47,11 @@ exclude_dirs = [".github", "tests", ".venv", ".vscode"]
[tool.black]
line-length = 100
target-version = ["py311"]
target-version = ["py312"]
[tool.pyright]
exclude = ["**/__pycache__", ".venv/"]
pythonVersion = "3.11"
pythonVersion = "3.12"
pythonPlatform = "All"
typeCheckingMode = "strict"
@@ -71,7 +72,7 @@ select = [
"D",
]
line-length = 100
target-version = "py311"
target-version = "py312"
extend-exclude = ["**/__pycache__"]
[tool.ruff.isort]