Skip to content

Commit

Permalink
Merge pull request #62 from jellllly420/feature/link-to-profile
Browse files Browse the repository at this point in the history
Add link to user profile
  • Loading branch information
zkoppert authored Jan 10, 2024
2 parents eb8d861 + d283a9a commit 5190a27
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/linters/.jscpd.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"threshold": 25,
"threshold": 50,
"ignore": [
"test*"
],
"absolute": true
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Below are the allowed configuration options:
| `START_DATE` | False | Beginning of time | The date from which you want to start gathering contributor information. ie. Aug 1st, 2023 would be `2023-08-01` If `start_date` and `end_date` are specified then the action will determine if the contributor is new. A new contributor is one that has contributed in the date range specified but not before the start date. **Performance Note:** Using start and end dates will reduce speed of the action by approximately 63X. ie without dates if the action takes 1.7 seconds, it will take 1 minute and 47 seconds.|
| `END_DATE` | False | Current Date | The date at which you want to stop gathering contributor information. Must be later than the `START_DATE`. ie. Aug 2nd, 2023 would be `2023-08-02` If `start_date` and `end_date` are specified then the action will determine if the contributor is new. A new contributor is one that has contributed in the date range specified but not before the start date. |
| `SPONSOR_INFO` | False | False | If you want to include sponsor information in the output. This will include the sponsor count and the sponsor URL. This will impact action performance. ie. SPONSOR_INFO = "False" or SPONSOR_INFO = "True" |
| `LINK_TO_PROFILE` | False | True | If you want to link usernames to their GitHub profiles in the output. ie. LINK_TO_PROFILE = "True" or LINK_TO_PROFILE = "False" |

### Example workflows

Expand Down Expand Up @@ -117,7 +118,7 @@ jobs:

| Username | Contribution Count | New Contributor | Commits |
| --- | --- | --- | --- |
| zkoppert | 143 | False | [super-linter/super-linter](https://github.com/super-linter/super-linter/commits?author=zkoppert&since=2021-01-01&until=2023-10-10) |
| @zkoppert | 143 | False | [super-linter/super-linter](https://github.com/super-linter/super-linter/commits?author=zkoppert&since=2021-01-01&until=2023-10-10) |

## Example Markdown output with no dates supplied

Expand All @@ -131,7 +132,7 @@ jobs:

| Username | Contribution Count | New Contributor | Sponsor URL | Commits |
| --- | --- | --- | --- | --- |
| zkoppert | 1913 | False | [Sponsor Link](https://github.com/sponsors/zkoppert) | [super-linter/super-linter](https://github.com/super-linter/super-linter/commits?author=zkoppert&since=2021-09-01&until=2023-09-30) |
| @zkoppert | 1913 | False | [Sponsor Link](https://github.com/sponsors/zkoppert) | [super-linter/super-linter](https://github.com/super-linter/super-linter/commits?author=zkoppert&since=2021-09-01&until=2023-09-30) |

## Local usage without Docker

Expand Down
2 changes: 2 additions & 0 deletions contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def main():
start_date,
end_date,
sponsor_info,
link_to_profile,
) = env.get_env_vars()

# Auth to GitHub.com
Expand Down Expand Up @@ -64,6 +65,7 @@ def main():
organization,
repository_list,
sponsor_info,
link_to_profile,
)
# write_to_json(contributors)

Expand Down
13 changes: 12 additions & 1 deletion env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def get_env_vars() -> (
tuple[str | None, list[str], str, str, str | None, str | None, str | Any]
tuple[str | None, list[str], str, str, str | None, str | None, str | Any, str | Any]
):
"""
Get the environment variables for use in the action.
Expand All @@ -27,6 +27,7 @@ def get_env_vars() -> (
str: the start date to get contributor information from
str: the end date to get contributor information to.
str: whether to get sponsor information on the contributor
str: whether to link username to Github profile in markdown output
"""
# Load from .env file if it exists
Expand Down Expand Up @@ -67,6 +68,15 @@ def get_env_vars() -> (
"SPONSOR_INFO environment variable not a boolean. ie. True or False or blank"
)

link_to_profile = os.getenv("LINK_TO_PROFILE")
# make sure that link_to_profile is a boolean
if link_to_profile:
link_to_profile = link_to_profile.lower().strip()
if link_to_profile not in ["true", "false", ""]:
raise ValueError(
"LINK_TO_PROFILE environment variable not a boolean. ie. True or False or blank"
)

# Separate repositories_str into a list based on the comma separator
repositories_list = []
if repositories_str:
Expand All @@ -82,4 +92,5 @@ def get_env_vars() -> (
start_date,
end_date,
sponsor_info,
link_to_profile,
)
21 changes: 18 additions & 3 deletions markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def write_to_markdown(
organization,
repository,
sponsor_info,
link_to_profile,
):
"""
This function writes a list of collaborators to a markdown file in table format.
Expand All @@ -24,14 +25,21 @@ def write_to_markdown(
organization (str): The organization for which the contributors are being listed.
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
Returns:
None
"""
# Put together the contributor table
table, total_contributions = get_contributor_table(
collaborators, start_date, end_date, organization, repository, sponsor_info
collaborators,
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
)

# Put together the summary table including # of new contributions, # of new contributors, % new contributors, % returning contributors
Expand Down Expand Up @@ -127,7 +135,13 @@ def get_summary_table(collaborators, start_date, end_date, total_contributions):


def get_contributor_table(
collaborators, start_date, end_date, organization, repository, sponsor_info
collaborators,
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
):
"""
This function returns a string containing a markdown table of the contributors and the total contribution count.
Expand All @@ -140,6 +154,7 @@ def get_contributor_table(
organization (str): The organization for which the contributors are being listed.
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
Returns:
table (str): A string containing a markdown table of the contributors and the total contribution count.
Expand Down Expand Up @@ -177,7 +192,7 @@ def get_contributor_table(
commit_urls += url + ", "
new_contributor = collaborator.new_contributor

row = f"| {username} | {contribution_count} |"
row = f"| {'' if link_to_profile == 'false' else '@'}{username} | {contribution_count} |"
if "New Contributor" in columns:
row += f" {new_contributor} |"
if "Sponsor URL" in columns:
Expand Down
5 changes: 4 additions & 1 deletion test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_get_env_vars(self, mock_getenv):
"2022-01-01",
"2022-12-31",
"False",
"True",
]

(
Expand All @@ -34,6 +35,7 @@ def test_get_env_vars(self, mock_getenv):
start_date,
end_date,
sponsor_info,
link_to_profile,
) = env.get_env_vars()

self.assertEqual(organization, "org")
Expand All @@ -43,14 +45,15 @@ def test_get_env_vars(self, mock_getenv):
self.assertEqual(start_date, "2022-01-01")
self.assertEqual(end_date, "2022-12-31")
self.assertEqual(sponsor_info, "false")
self.assertEqual(link_to_profile, "true")

@patch("os.getenv")
def test_get_env_vars_missing_values(self, mock_getenv):
"""
Test the get_env_vars function when none of the environment variables are set.
Expect a ValueError to be raised.
"""
mock_getenv.side_effect = [None, None, None, None, None, None, None]
mock_getenv.side_effect = [None, None, None, None, None, None, None, None]

with self.assertRaises(ValueError):
env.get_env_vars()
Expand Down
64 changes: 60 additions & 4 deletions test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_write_to_markdown(self, mock_file):
None,
"org/repo",
"false",
"true",
)

mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
Expand All @@ -60,8 +61,8 @@ def test_write_to_markdown(self, mock_file):
mock_file().write.assert_any_call(
"| Username | Contribution Count | New Contributor | Commits |\n"
"| --- | --- | --- | --- |\n"
"| user1 | 100 | False | commit url |\n"
"| user2 | 200 | True | commit url2 |\n"
"| @user1 | 100 | False | commit url |\n"
"| @user2 | 200 | True | commit url2 |\n"
)

@patch("builtins.open", new_callable=mock_open)
Expand Down Expand Up @@ -100,6 +101,7 @@ def test_write_to_markdown_with_sponsors(self, mock_file):
None,
"org/repo",
"true",
"true",
)

mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
Expand All @@ -113,8 +115,62 @@ def test_write_to_markdown_with_sponsors(self, mock_file):
mock_file().write.assert_any_call(
"| Username | Contribution Count | New Contributor | Sponsor URL | Commits |\n"
"| --- | --- | --- | --- | --- |\n"
"| user1 | 100 | False | [Sponsor Link](sponsor_url_1) | commit url |\n"
"| user2 | 200 | True | not sponsorable | commit url2 |\n"
"| @user1 | 100 | False | [Sponsor Link](sponsor_url_1) | commit url |\n"
"| @user2 | 200 | True | not sponsorable | commit url2 |\n"
)

@patch("builtins.open", new_callable=mock_open)
def test_write_to_markdown_without_link_to_profile(self, mock_file):
"""
Test the write_to_markdown function with link to profile turned off.
"""
person1 = contributor_stats.ContributorStats(
"user1",
False,
"url",
100,
"commit url",
"sponsor_url_1",
)
person2 = contributor_stats.ContributorStats(
"user2",
False,
"url2",
200,
"commit url2",
"sponsor_url_2",
)
# Set person2 as a new contributor since this cannot be set on initiatization of the object
person2.new_contributor = True
collaborators = [
person1,
person2,
]

write_to_markdown(
collaborators,
"filename",
"2023-01-01",
"2023-01-02",
None,
"org/repo",
"false",
"false",
)

mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
mock_file().write.assert_any_call("# Contributors\n\n")
mock_file().write.assert_any_call(
"- Date range for contributor list: 2023-01-01 to 2023-01-02\n"
)
mock_file().write.assert_any_call(
"| Total Contributors | Total Contributions | % New Contributors |\n| --- | --- | --- |\n| 2 | 300 | 50.0% |\n\n"
)
mock_file().write.assert_any_call(
"| Username | Contribution Count | New Contributor | Commits |\n"
"| --- | --- | --- | --- |\n"
"| user1 | 100 | False | commit url |\n"
"| user2 | 200 | True | commit url2 |\n"
)


Expand Down

0 comments on commit 5190a27

Please sign in to comment.