Skip to content

Commit

Permalink
Updates 2023 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
R055A committed May 9, 2023
1 parent 3c027f7 commit 6c5a859
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 29 deletions.
1 change: 1 addition & 0 deletions .github/workflows/auto_update_stat_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
MORE_REPOS: ${{ secrets.MORE_REPOS }}
ONLY_INCLUDED: ${{ secrets.ONLY_INCLUDED }}
EXCLUDED_COLLAB_REPOS: ${{ secrets.EXCLUDED_COLLAB_REPOS }}
MORE_COLLAB_REPOS: ${{ secrets.MORE_COLLAB_REPOS }}

# Commits all changed files to the repository
- name: Commit to the repo
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/non_auto_generate_stat_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ jobs:
MORE_REPOS: ${{ secrets.MORE_REPOS }}
ONLY_INCLUDED: ${{ secrets.ONLY_INCLUDED }}
EXCLUDED_COLLAB_REPOS: ${{ secrets.EXCLUDED_COLLAB_REPOS }}
MORE_COLLAB_REPOS: ${{ secrets.MORE_COLLAB_REPOS }}

# Commits all changed files to the repository
- name: Commit to the repo
Expand Down
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,29 @@ Generate daily updated visualizations of user and repository statistics from the
* example:
* `4`
* ### Optional Secret *Name*: `EXCLUDED_COLLAB_REPOS`
For excluding collaborative repositories from being included in the average contribution statistics
For excluding collaborative repositories from being included in the average contribution statistics calculations
- for example, such as for when
- contributions are included in a collaborative repo, but it is not one of your projects
- someone deletes and re-adds the entire codebase a few times
- your performance is not fairly represented
- ninjas
- contributions are made to a collaborative repo, but it is not one of your projects (open-source typo fix, etc)
- someone deletes and re-adds the entire codebase a few times too many
- your or someone else's performance is not fairly represented - missing data bias
- pirates, ninjas, etc.

**Instructions**:
* enter *Value* in the following format (separated by commas):
* `[owner/repo],[owner/repo],...,[owner/repo]`
* example:
* `jstrieb/github-stats,rahul-jha98/github-stats-transparent,idiotWu/stats`
* `tera_open_source/bit_typo_fix,peer_repo/no_git_co_author_credit,dude_collab/email_not_reg_on_github,mars/attacks`
* ### Optional Secret *Name*: `MORE_COLLAB_REPOS`
For including collaborative repositories that are otherwise not included in the average contribution statistics calculations
- for example, such as when
- nobody even bothered to join the repository as a collaborator let alone contribute anything
- the repository is imported and because it is ghosted none of the collaborators are represented in the scraping

**Instructions**:
* enter *Value* in the following format (separated by commas):
* `[owner/repo],[owner/repo],...,[owner/repo]`
* example:
* `large_A+_collab_project/ghosted,larger_A++_project/slave_status,import_collabs/no_other_contributions_no_collabs`
* ### Optional Secret *Name*: `STORE_REPO_VIEWS`
Boolean for storing generated repository view statistic visualization data beyond the 14 day-limit GitHub API allows
- `true` by default
Expand Down
2 changes: 1 addition & 1 deletion src/db/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"to": "0000-00-00",
"from": "0000-00-00"
}
}
}
10 changes: 9 additions & 1 deletion src/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def __init__(self,
more_collaborators: Optional[str] = getenv("MORE_COLLABS"),
manually_added_repos: Optional[str] = getenv("MORE_REPOS"),
only_included_repos: Optional[str] = getenv("ONLY_INCLUDED"),
exclude_collab_repos: Optional[str] = getenv("EXCLUDED_COLLAB_REPOS")):
exclude_collab_repos: Optional[str] = getenv("EXCLUDED_COLLAB_REPOS"),
more_collab_repos: Optional[str] = getenv("MORE_COLLAB_REPOS")):
self.__db = GitRepoStatsDB()

self.username = username
Expand Down Expand Up @@ -148,6 +149,13 @@ def __init__(self,
{x.strip() for x in exclude_collab_repos.split(",")}
)

if more_collab_repos is None:
self.more_collab_repos = set()
else:
self.more_collab_repos = (
{x.strip() for x in more_collab_repos.split(",")}
)

def set_views(self, views: any) -> None:
self.repo_views += int(views)
environ["REPO_VIEWS"] = str(self.repo_views)
Expand Down
21 changes: 13 additions & 8 deletions src/generate_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,21 @@ async def generate_overview(self) -> None:
output = f.read()

name = await self.__stats.name
name += "'" if name[-1] == "s" else "'s"
if len(name) > MAX_NAME_LEN:
if len(name + ("'" if name[-1].lower() == "s" else "'s")) > MAX_NAME_LEN:
names = name.split(' ')
if len(names) == 1:
name = names[0]
elif len(names[0][0] + '. ' + names[-1]) > MAX_NAME_LEN:
name = names[0] + "'" if names[0][-1] == "s" else "'s"
if len(names) == 1 or len(names[0][0] + '. ' + names[-1] +
("'" if names[-1][-1].lower() == "s" else "'s")) > MAX_NAME_LEN:
if self.__stats.environment_vars.username + \
("'" if self.__stats.environment_vars.username[-1].lower() == "s" else "'s") > MAX_NAME_LEN:
name = self.__stats.environment_vars.username + \
("'" if self.__stats.environment_vars.username[-1].lower() == "s" else "'s")
else:
name = ' '.join([name[0] + '.' for name in names])
name = name.strip()[:min(len(name), MAX_NAME_LEN - 2)] + "'s"
else:
name = names[0][0] + '. ' + names[-1]
name = name if len(name) <= MAX_NAME_LEN else name[:MAX_NAME_LEN-2] + '..'
name = names[0][0] + '. ' + names[-1] + ("'" if names[-1][-1].lower() == "s" else "'s")
else:
name += "'" if name[-1].lower() == "s" else "'s"
output = sub("{{ name }}",
name,
output)
Expand Down
38 changes: 26 additions & 12 deletions src/github_repo_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def __init__(self,
self._avg_percent: Optional[str] = None
self._views: Optional[int] = None
self._collaborators: Optional[int] = None
self._collaborator_set: Optional[Set[str]] = None
self._contributors: Optional[Set[str]] = None
self._views_from_date: Optional[str] = None
self._pull_requests: Optional[int] = None
self._issues: Optional[int] = None
self._empty_repos: Optional[Set[str]] = None
self._collab_repos: Optional[Set[str]] = None

async def to_str(self) -> str:
"""
Expand Down Expand Up @@ -368,6 +370,9 @@ async def lines_changed(self) -> Tuple[int, int]:
"""
if self._users_lines_changed is not None:
return self._users_lines_changed
_, ghosted_collab_repos = await self.raw_collaborators()
slave_status_repos = self.environment_vars.more_collab_repos

contributor_set = set()
repo_total_changes_arr = []
author_contribution_percentages = []
Expand Down Expand Up @@ -404,9 +409,9 @@ async def lines_changed(self) -> Tuple[int, int]:
author_total_additions += author_additions
author_total_deletions += author_deletions

# calculate average author's contributions to each repository with more than one contributor
if repo not in self.environment_vars.exclude_collab_repos and \
(author_additions + author_deletions) > 0 and other_authors_total_changes > 0:
# calculate average author's contributions to each repository with more than one contributor (or should be)
if repo not in self.environment_vars.exclude_collab_repos and (author_additions + author_deletions) > 0 \
and (other_authors_total_changes > 0 or repo in ghosted_collab_repos | slave_status_repos):
repo_total_changes = other_authors_total_changes + author_additions + author_deletions
author_contribution_percentages.append((author_additions + author_deletions) / repo_total_changes)
repo_total_changes_arr.append(repo_total_changes)
Expand Down Expand Up @@ -484,24 +489,33 @@ async def views_from_date(self) -> str:
assert self._views_from_date is not None
return self._views_from_date

@property
async def collaborators(self) -> int:
"""
:return: count of total collaborators to user's repositories
"""
if self._collaborators is not None:
return self._collaborators
async def raw_collaborators(self) -> (Set, Set):
if self._collaborator_set is not None and self._collab_repos is not None:
return self._collaborator_set, self._collab_repos

collaborator_set = set()
self._collaborator_set = set()
self._collab_repos = set()

for repo in await self.repos:
r = await self.queries\
.query_rest(f"/repos/{repo}/collaborators")

for obj in r:
if isinstance(obj, dict):
collaborator_set.add(obj.get("login"))
self._collaborator_set.add(obj.get("login"))
self._collab_repos.add(repo)

return self._collaborator_set, self._collab_repos

@property
async def collaborators(self) -> int:
"""
:return: count of total collaborators to user's repositories
"""
if self._collaborators is not None:
return self._collaborators

collaborator_set, _ = await self.raw_collaborators()
collaborators = max(0, len(collaborator_set.union(await self.contributors)) - 1)
self._collaborators = self.environment_vars.more_collaborators + collaborators
return self._collaborators
Expand Down
4 changes: 3 additions & 1 deletion test/git_stats_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
MORE_REPOS = getenv("MORE_REPOS") # or enter: "[owner/repo],...,[owner/repo]"
ONLY_INCLUDED = getenv("ONLY_INCLUDED") # or enter: "[owner/repo],..."
EXCLUDED_COLLAB_REPOS = getenv("EXCLUDED_COLLAB_REPOS") # or enter: "[owner/repo],..."
MORE_COLLAB_REPOS = getenv("MORE_COLLAB_REPOS") # or enter: "[owner/repo],..."


async def main() -> None:
Expand Down Expand Up @@ -61,7 +62,8 @@ async def main() -> None:
more_collaborators=MORE_COLLABS,
manually_added_repos=MORE_REPOS,
only_included_repos=ONLY_INCLUDED,
exclude_collab_repos=EXCLUDED_COLLAB_REPOS),
exclude_collab_repos=EXCLUDED_COLLAB_REPOS,
more_collab_repos=MORE_COLLAB_REPOS),
session=session)
print(await stats.to_str())

Expand Down

0 comments on commit 6c5a859

Please sign in to comment.