lysandre/github-release
13
1from dataclasses import dataclass2 3import gradio as gr4from graphql_calls import get_tag_commit_date, get_commits5 6 7@dataclass8class Contributions:9 additions: int10 deletions: int11 descriptions: list[str]12 13 14def get_release_notes(15 token: str,16 repo: str,17 tag: str,18 branch: str,19 contributor_treshhold: int,20 ignore_dependabot: bool,21 ignore_direct: bool,22):23 try:24 date = get_tag_commit_date(token, repo, tag)25 commits = get_commits(token, repo, branch, date)26 except ValueError as e:27 return str(e)28 29 result = ""30 contributors = {}31 32 for commit in commits[::-1]:33 if "Hugging Face" not in commit.user.organizations:34 if commit.user.name not in contributors:35 contributors[commit.user.name] = Contributions(36 additions=commit.additions,37 deletions=commit.deletions,38 descriptions=[commit.message],39 )40 else:41 contributors[commit.user.name].additions += commit.additions42 contributors[commit.user.name].deletions += commit.deletions43 contributors[commit.user.name].descriptions += [commit.message]44 45 if "(#" in commit.message:46 if ignore_dependabot and commit.user.name == 'dependabot[bot]':47 continue48 49 split = commit.message.split("(#")50 message = split[0]51 number = split[1].strip(")")52 53 result += f"* {message} by @{commit.user.name} in #{number}\n"54 elif not ignore_direct:55 result += f"* {commit.message} by @{commit.user.name} (direct commit on {branch})\n"56 57 significant_contributors = {58 k: v for k, v in contributors.items() if (v.additions + v.deletions) > contributor_treshhold and k != '<NOT FOUND>'59 }60 61 if len(significant_contributors):62 result += (63 "\n## Significant community contributions\n"64 "\nThe following contributors have made significant "65 "changes to the library over the last release:\n\n"66 )67 68 for significant_contributor, contributions in significant_contributors.items():69 result += f"* @{significant_contributor}\n"70 for description in contributions.descriptions:71 result += f" * {description}\n"72 73 print(result)74 return result75 76article = """77## How to use the interface?78 79⚠️ Most errors are due to:80- A wrong `tag` -> the tag must exist on the repository!81- A wrong `branch` -> The branch must exist on the repository!82- A wrong `token` -> Obtaining a token is detailed below.83 84### token85 86This is a personal access token generated by GitHub. You can obtain one with the following guide:87https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.88 89### Repository90 91The repository for which to create the release notes. Should be in the format `organization/repository`.92 93### Tag94 95The tag from which all commits were made. The app will fetch the date at which this tag was created, and will96return all commits of the branch defined below that have a timestamp following that date.97 98### Branch99 100The branch on which all commits lie. Usually `master` or `main`.101 102### Threshold103 104This threshold allows highlighting specific community contributors according to the size of their contributions.105It currently adds all their additions/deletions across all PRs included in this release. It is then compared106to the defined threshold: if above, that user will get a special note mentioning what they have worked on!107"""108 109demo = gr.Interface(110 title='Automatic release notes 🤗',111 article=article,112 description="**See instructions below the form.**",113 fn=get_release_notes,114 inputs=[115 gr.components.Textbox(lines=1, placeholder="Your GitHub token"),116 gr.components.Textbox(117 lines=1, placeholder="Repository", value="huggingface/transformers"118 ),119 gr.components.Textbox(lines=1, placeholder="The tag from which to get commit"),120 gr.components.Textbox(121 lines=1,122 placeholder="The linear branch on which the new version tag will be added",123 value="main",124 ),125 gr.components.Slider(126 minimum=0,127 maximum=2000,128 value=500,129 label="Threshold for significant contributors",130 ),131 gr.components.Checkbox(label="Ignore dependabot commits"),132 gr.components.Checkbox(label="Ignore direct commits"),133 ],134 outputs="text",135 examples=[136 ['ghp_XXX', 'huggingface/datasets', '2.1.0', 'master', 60, True, True],137 ['ghp_XXX', 'huggingface/accelerate', 'v0.7.1', 'main', 500, True, True],138 ['ghp_XXX', 'huggingface/transformers', 'v4.18.0', 'main', 500, True, True],139 ['ghp_XXX', 'huggingface/diffusers', 'v0.17.0', 'main', 500, True, True],140 ['ghp_XXX', 'gradio-app/gradio', 'v2.9.0', 'main', 500, True, True],141 ],142)143 144 145if __name__ == "__main__":146 demo.launch(show_error=True)147 