Skip to content

Commit

Permalink
Testin
Browse files Browse the repository at this point in the history
Show in step summary

Run custom contributors

Add uses

Test fixed commits

Add organisations

Fix f string

Fix list

Fix items

Restructure

Reference main

Fix join

Limit orgs

Updates

Limit to training

debug

Check

Add print

Raise error

Change to id

Fix

Fxi2

Add print

url

fix
  • Loading branch information
Harrison Cook authored and HCookie committed Oct 16, 2024
1 parent f01f58e commit 484ecf2
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 84 deletions.
26 changes: 16 additions & 10 deletions .github/workflows/contributors_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ on:
workflow_dispatch:
schedule:
- cron: "3 2 1 * *"

# on:
# push:
# branches: [testing]
permissions:
contents: read

Expand Down Expand Up @@ -35,13 +37,17 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
START_DATE: ${{ env.START_DATE }}
END_DATE: ${{ env.END_DATE }}
REPOSITORY: github/contributors
SPONSOR_INFO: "true"
REPOSITORY: ecmwf/anemoi-training
# SPONSOR_INFO: "true"

- name: Show Contributor
shell: bash
run: cat contributors.md >> $GITHUB_STEP_SUMMARY

- name: Create issue
uses: peter-evans/create-issue-from-file@e8ef132d6df98ed982188e460ebb3b5d4ef3a9cd
with:
title: Monthly contributor report
token: ${{ secrets.GITHUB_TOKEN }}
content-filepath: ./contributors.md
assignees: zkoppert
# - name: Create issue
# uses: peter-evans/create-issue-from-file@e8ef132d6df98ed982188e460ebb3b5d4ef3a9cd
# with:
# title: Monthly contributor report
# token: ${{ secrets.GITHUB_TOKEN }}
# content-filepath: ./contributors.md
# # assignees: zkoppert
62 changes: 62 additions & 0 deletions .github/workflows/contributors_report_testing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: Testing contributor report
# on:
# workflow_dispatch:
# schedule:
# - cron: "3 2 1 * *"
on:
push:
branches: [testing]
permissions:
contents: read

jobs:
contributor_report:
name: contributor report
runs-on: ubuntu-latest
permissions:
issues: write

steps:
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1

- name: Get dates for last month
shell: bash
run: |
# Calculate the first day of the previous month
start_date=$(date -d "last month" +%Y-%m-01)
# Calculate the last day of the previous month
end_date=$(date -d "$start_date +1 month -1 day" +%Y-%m-%d)
#Set an environment variable with the date range
echo "START_DATE=$start_date" >> "$GITHUB_ENV"
echo "END_DATE=$end_date" >> "$GITHUB_ENV"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run Contributors
run: python -m contributors

env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
START_DATE: ${{ env.START_DATE }}
END_DATE: ${{ env.END_DATE }}
REPOSITORY: ecmwf/anemoi-training
# SHOW_ORGANISATIONS: ecmwf
# SPONSOR_INFO: "true"

- name: Show Contributor
shell: bash
run: cat contributors.md >> $GITHUB_STEP_SUMMARY

# - name: Create issue
# uses: peter-evans/create-issue-from-file@e8ef132d6df98ed982188e460ebb3b5d4ef3a9cd
# with:
# title: Monthly contributor report
# token: ${{ secrets.GITHUB_TOKEN }}
# content-filepath: ./contributors.md
# # assignees: zkoppert
Empty file added contributors/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions contributors/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from contributors.contributors import main

main()
File renamed without changes.
49 changes: 9 additions & 40 deletions contributor_stats.py → contributors/contributor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
from typing import List

import requests
from dataclasses import dataclass, field


@dataclass
class ContributorStats:
"""
A class to represent a contributor_stats object correlating to a single contributors stats.
Expand All @@ -31,47 +33,14 @@ class ContributorStats:
"""

def __new__(cls, *args, **kwargs): # pylint: disable=unused-argument
"""Create a new contributor_stats object"""
return super().__new__(cls)

def __init__(
self,
username: str,
new_contributor: bool,
avatar_url: str,
contribution_count: int,
commit_url: str,
sponsor_info: str,
):
"""Initialize the contributor_stats object"""
new_contributor = False
self.username = username
self.new_contributor = new_contributor
self.avatar_url = avatar_url
self.contribution_count = contribution_count
self.commit_url = commit_url
self.sponsor_info = sponsor_info

def __repr__(self) -> str:
"""Return the representation of the contributor_stats object"""
return (
f"contributor_stats(username={self.username}, "
f"new_contributor={self.new_contributor}, "
f"avatar_url={self.avatar_url}, "
f"contribution_count={self.contribution_count}, commit_url={self.commit_url})"
f"sponsor_info={self.sponsor_info})"
)
username: str
avatar_url: str
contribution_count: int
commit_url: str
sponsor_info: str
organisations: list[str] = field(default_factory=list)

def __eq__(self, other) -> bool:
"""Check if two contributor_stats objects are equal"""
return (
self.username == other.username
and self.new_contributor == other.new_contributor
and self.avatar_url == other.avatar_url
and self.contribution_count == other.contribution_count
and self.commit_url == other.commit_url
)
new_contributor: bool = False


def is_new_contributor(username: str, returning_contributors: list) -> bool:
Expand Down
39 changes: 21 additions & 18 deletions contributors.py → contributors/contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

from typing import List

import auth
import contributor_stats
import env
import json_writer
import markdown
import github3.repos
import github3.users
import github3

from . import auth
from . import contributor_stats
from . import env
from . import json_writer
from . import markdown


def main():
Expand All @@ -26,10 +30,11 @@ def main():
end_date,
sponsor_info,
link_to_profile,
show_organisations_list,
) = env.get_env_vars()

# Auth to GitHub.com
github_connection = auth.auth_to_github(
github_connection: github3.GitHub = auth.auth_to_github(
gh_app_id, gh_app_installation_id, gh_app_private_key_bytes, token, ghe
)

Expand Down Expand Up @@ -72,6 +77,7 @@ def main():
repository_list,
sponsor_info,
link_to_profile,
show_organisations_list,
)
json_writer.write_to_json(
filename="contributors.json",
Expand All @@ -90,7 +96,7 @@ def get_all_contributors(
repository_list: List[str],
start_date: str,
end_date: str,
github_connection: object,
github_connection: github3.GitHub,
):
"""
Get all contributors from the organization or repository
Expand Down Expand Up @@ -129,7 +135,7 @@ def get_all_contributors(


def get_contributors(
repo: object,
repo: github3.repos.Repository,
start_date: str,
end_date: str,
):
Expand Down Expand Up @@ -172,12 +178,13 @@ def get_contributors(
f"https://github.com/{repo.full_name}/commits?author={user.login}"
)
contributor = contributor_stats.ContributorStats(
user.login,
False,
user.avatar_url,
user.contributions_count,
commit_url,
"",
username=user.login,
new_contributor=False,
avatar_url=user.avatar_url,
contribution_count=user.contributions_count,
commit_url=commit_url,
sponsor_info="",
organisations=list(map(lambda x: x.url, user.organizations())),
)
contributors.append(contributor)
except Exception as e:
Expand All @@ -186,7 +193,3 @@ def get_contributors(
return None

return contributors


if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions env.py → contributors/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def get_env_vars(
raise ValueError("GH_TOKEN environment variable not set")

ghe = os.getenv("GH_ENTERPRISE_URL", default="").strip()
show_organisations = os.getenv("SHOW_ORGANISATIONS", default="").strip()

start_date = validate_date_format("START_DATE")
end_date = validate_date_format("END_DATE")
Expand All @@ -140,6 +141,10 @@ def get_env_vars(
repositories_list = [
repository.strip() for repository in repositories_str.split(",")
]

show_organisations_list = []
if show_organisations:
show_organisations_list = [org.strip() for org in show_organisations.split(",")]

return (
organization,
Expand All @@ -153,4 +158,5 @@ def get_env_vars(
end_date,
sponsor_info,
link_to_profile,
show_organisations_list,
)
File renamed without changes.
45 changes: 39 additions & 6 deletions markdown.py → contributors/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"""This module contains the functions needed to write the output to markdown files."""


from collections import defaultdict

from .contributor_stats import ContributorStats


def write_to_markdown(
collaborators,
filename,
Expand All @@ -11,6 +16,7 @@ def write_to_markdown(
repository,
sponsor_info,
link_to_profile,
show_organisations_list,
):
"""
This function writes a list of collaborators to a markdown file in table format.
Expand All @@ -26,6 +32,7 @@ def write_to_markdown(
repository (str): The repository for which the contributors are being listed.
sponsor_info (str): True if the user wants the sponsor_url shown in the report
link_to_profile (str): True if the user wants the username linked to Github profile in the report
show_organisations_list (list): Organisations to show
Returns:
None
Expand All @@ -40,6 +47,7 @@ def write_to_markdown(
repository,
sponsor_info,
link_to_profile,
show_organisations_list,
)

# Put together the summary table including # of new contributions, # of new contributors, % new contributors, % returning contributors
Expand Down Expand Up @@ -84,9 +92,14 @@ def write_markdown_file(
markdown_file.write(f"- Repository: {repository}\n")
markdown_file.write("\n")
markdown_file.write(summary_table)
markdown_file.write(table)
if len(table) == 1 and 'Independent' in table:
markdown_file.write(table['Independent'])
else:
for key, t in table.items():
markdown_file.write(f"## {key}\n")
markdown_file.write(t)
markdown_file.write(
"\n _this file was generated by the [Contributors GitHub Action](https://github.com/github/contributors)_\n"
"\n _this file was generated by the [Contributors GitHub Action](https://github.com/HCookie/contributors)_\n"
)


Expand Down Expand Up @@ -134,14 +147,19 @@ def get_summary_table(collaborators, start_date, end_date, total_contributions):
return summary_table


def make_row(*args) -> str:
return "| " + "|".join(args) + "|"


def get_contributor_table(
collaborators,
collaborators: list[ContributorStats],
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
show_organisations_list,
):
"""
This function returns a string containing a markdown table of the contributors and the total contribution count.
Expand Down Expand Up @@ -174,9 +192,10 @@ def get_contributor_table(
headers = "| " + " | ".join(columns) + " |\n"
headers += "| " + " | ".join(["---"] * len(columns)) + " |\n"

table = headers
total_contributions = 0

organisation_contributors = defaultdict(list)

for collaborator in collaborators:
total_contributions += collaborator.contribution_count
username = collaborator.username
Expand Down Expand Up @@ -205,5 +224,19 @@ def get_contributor_table(
row += f" [Sponsor Link]({collaborator.sponsor_info}) |"
row += f" {commit_urls} |\n"

table += row
return table, total_contributions
added_to_org: bool = False
print(show_organisations_list, collaborator.organisations)
for org in collaborator.organisations or []:
if org in show_organisations_list:
organisation_contributors[org].append(row)
added_to_org = True

if not added_to_org:
organisation_contributors["Independent"].append(row)

tables = {
org: headers + "".join(rows) for org, rows in organisation_contributors.items()
}

# table += row
return tables, total_contributions
Empty file added tests/__init__.py
Empty file.
Loading

0 comments on commit 484ecf2

Please sign in to comment.