sayakpaul HF staff commited on
Commit
f30c6d8
·
1 Parent(s): 5e4e861
__pycache__/github.cpython-39.pyc ADDED
Binary file (2.42 kB). View file
 
__pycache__/graphql_calls.cpython-39.pyc ADDED
Binary file (4.39 kB). View file
 
app.py CHANGED
@@ -1,7 +1,8 @@
1
  from dataclasses import dataclass
2
-
3
  import gradio as gr
4
  from graphql_calls import get_tag_commit_date, get_commits
 
5
 
6
 
7
  @dataclass
@@ -12,8 +13,6 @@ class Contributions:
12
 
13
 
14
  def get_release_notes(
15
- token: str,
16
- repo: str,
17
  tag: str,
18
  branch: str,
19
  contributor_treshhold: int,
@@ -21,6 +20,8 @@ def get_release_notes(
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:
@@ -43,7 +44,7 @@ def get_release_notes(
43
  contributors[commit.user.name].descriptions += [commit.message]
44
 
45
  if "(#" in commit.message:
46
- if ignore_dependabot and commit.user.name == 'dependabot[bot]':
47
  continue
48
 
49
  split = commit.message.split("(#")
@@ -55,7 +56,9 @@ def get_release_notes(
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):
@@ -72,54 +75,28 @@ def get_release_notes(
72
 
73
  return result
74
 
75
- article = """
76
- ## How to use the interface?
77
-
78
- ⚠️ Most errors are due to:
79
- - A wrong `tag` -> the tag must exist on the repository!
80
- - A wrong `branch` -> The branch must exist on the repository!
81
- - A wrong `token` -> Obtaining a token is detailed below.
82
-
83
- ### token
84
-
85
- This is a personal access token generated by GitHub. You can obtain one with the following guide:
86
- https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token.
87
-
88
- ### Repository
89
-
90
- The repository for which to create the release notes. Should be in the format `organization/repository`.
91
-
92
- ### Tag
93
 
94
- The tag from which all commits were made. The app will fetch the date at which this tag was created, and will
95
- return all commits of the branch defined below that have a timestamp following that date.
96
-
97
- ### Branch
98
-
99
- The branch on which all commits lie. Usually `master` or `main`.
100
-
101
- ### Threshold
102
-
103
- This threshold allows highlighting specific community contributors according to the size of their contributions.
104
- It currently adds all their additions/deletions across all PRs included in this release. It is then compared
105
- to the defined threshold: if above, that user will get a special note mentioning what they have worked on!
106
  """
 
107
 
108
  demo = gr.Interface(
109
- title='Automatic release notes 🤗',
110
  article=article,
111
  description="**See instructions below the form.**",
112
  fn=get_release_notes,
113
  inputs=[
114
- gr.components.Textbox(lines=1, placeholder="Your GitHub token"),
115
  gr.components.Textbox(
116
- lines=1, placeholder="Repository", value="huggingface/transformers"
 
 
117
  ),
118
- gr.components.Textbox(lines=1, placeholder="The tag from which to get commit"),
119
  gr.components.Textbox(
120
  lines=1,
121
  placeholder="The linear branch on which the new version tag will be added",
122
- value="main",
123
  ),
124
  gr.components.Slider(
125
  minimum=0,
@@ -131,13 +108,6 @@ demo = gr.Interface(
131
  gr.components.Checkbox(label="Ignore direct commits"),
132
  ],
133
  outputs="text",
134
- examples=[
135
- ['ghp_XXX', 'huggingface/datasets', '2.1.0', 'master', 60, True, True],
136
- ['ghp_XXX', 'huggingface/accelerate', 'v0.7.1', 'main', 500, True, True],
137
- ['ghp_XXX', 'huggingface/transformers', 'v4.18.0', 'main', 500, True, True],
138
- ['ghp_XXX', 'huggingface/diffusers', 'v0.17.0', 'main', 500, True, True],
139
- ['ghp_XXX', 'gradio-app/gradio', 'v2.9.0', 'main', 500, True, True],
140
- ],
141
  )
142
 
143
 
 
1
  from dataclasses import dataclass
2
+ import os
3
  import gradio as gr
4
  from graphql_calls import get_tag_commit_date, get_commits
5
+ from github import fetch_github_info
6
 
7
 
8
  @dataclass
 
13
 
14
 
15
  def get_release_notes(
 
 
16
  tag: str,
17
  branch: str,
18
  contributor_treshhold: int,
 
20
  ignore_direct: bool,
21
  ):
22
  try:
23
+ token = os.getenv("GITHUB_TOKEN")
24
+ repo = "huggingface/diffusers"
25
  date = get_tag_commit_date(token, repo, tag)
26
  commits = get_commits(token, repo, branch, date)
27
  except ValueError as e:
 
44
  contributors[commit.user.name].descriptions += [commit.message]
45
 
46
  if "(#" in commit.message:
47
+ if ignore_dependabot and commit.user.name == "dependabot[bot]":
48
  continue
49
 
50
  split = commit.message.split("(#")
 
56
  result += f"* {commit.message} by @{commit.user.name} (direct commit on {branch})\n"
57
 
58
  significant_contributors = {
59
+ k: v
60
+ for k, v in contributors.items()
61
+ if (v.additions + v.deletions) > contributor_treshhold and k != "<NOT FOUND>"
62
  }
63
 
64
  if len(significant_contributors):
 
75
 
76
  return result
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ article = """
80
+ Mostly copied from [lysandre/github-release](https://huggingface.co/spaces/lysandre/github-release) but suited for
81
+ Diffusers.
 
 
 
 
 
 
 
 
 
82
  """
83
+ github_info = fetch_github_info("https://github.com/huggingface/diffusers")
84
 
85
  demo = gr.Interface(
86
+ title="Automatic release notes for 🤗 Diffusers",
87
  article=article,
88
  description="**See instructions below the form.**",
89
  fn=get_release_notes,
90
  inputs=[
 
91
  gr.components.Textbox(
92
+ lines=1,
93
+ value=github_info["second_last_tag"] if github_info else None,
94
+ placeholder="The tag from which to get commit",
95
  ),
 
96
  gr.components.Textbox(
97
  lines=1,
98
  placeholder="The linear branch on which the new version tag will be added",
99
+ value=github_info["latest_release_branch"] if github_info else None,
100
  ),
101
  gr.components.Slider(
102
  minimum=0,
 
108
  gr.components.Checkbox(label="Ignore direct commits"),
109
  ],
110
  outputs="text",
 
 
 
 
 
 
 
111
  )
112
 
113
 
github.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from functools import lru_cache
3
+ from packaging.version import parse
4
+ import os
5
+
6
+ TOKEN = os.getenv("GITHUB_TOKEN", None)
7
+ HEADERS = {"Authorization": f"token {TOKEN}"} if TOKEN else {}
8
+
9
+
10
+ @lru_cache()
11
+ def fetch_all_branches(user, repo):
12
+ branches = [] # List to store all branches
13
+ page = 1 # Start from first page
14
+ while True:
15
+ # Make a request to the GitHub API for the branches
16
+ response = requests.get(
17
+ f"https://api.github.com/repos/{user}/{repo}/branches", params={"page": page}, headers=HEADERS
18
+ )
19
+
20
+ # Check if the request was successful
21
+ if response.status_code == 200:
22
+ # Add the branches from the current page to the list
23
+ branches.extend([branch["name"] for branch in response.json()])
24
+
25
+ # Check if there is a 'next' link for pagination
26
+ if "next" in response.links:
27
+ page += 1 # Move to the next page
28
+ else:
29
+ break # Exit loop if there is no next page
30
+ else:
31
+ print("Failed to retrieve branches:", response.status_code)
32
+ break
33
+
34
+ return branches
35
+
36
+
37
+ def fetch_github_info(repo_url):
38
+ # Extract owner and repo name from the URL
39
+ repo_parts = repo_url.rstrip("/").split("/")
40
+ owner, repo = repo_parts[-2], repo_parts[-1]
41
+ print(f"{owner=}, {repo=}")
42
+
43
+ # GitHub API base URL
44
+ api_url = f"https://api.github.com/repos/{owner}/{repo}"
45
+
46
+ try:
47
+ # Fetch tags
48
+ tags_response = requests.get(f"{api_url}/tags", headers=HEADERS)
49
+ tags_response.raise_for_status()
50
+ tags = tags_response.json()
51
+
52
+ if len(tags) < 2:
53
+ raise ValueError("Not enough tags to fetch the second last tag.")
54
+
55
+ second_last_tag = tags[1]["name"]
56
+
57
+ # Fetch branches
58
+ branches = fetch_all_branches(user=owner, repo=repo)
59
+ if not branches:
60
+ raise ValueError("No branches found.")
61
+
62
+ filtered_branches = []
63
+ for branch in branches:
64
+ if branch.startswith("v") and ("-release" in branch or "-patch" in branch):
65
+ filtered_branches.append(branch)
66
+ if not filtered_branches:
67
+ raise ValueError("No release branches found.")
68
+
69
+ sorted_branches = sorted(filtered_branches, key=lambda x: parse(x.split("-")[0][1:]), reverse=True)
70
+ latest_release_branch = sorted_branches[0]
71
+
72
+ return {"second_last_tag": second_last_tag, "latest_release_branch": latest_release_branch}
73
+
74
+ except requests.exceptions.RequestException as e:
75
+ print(f"Error fetching data from GitHub: {e}")
76
+ return None
77
+ except ValueError as e:
78
+ print(e)
79
+ return None
80
+
81
+
82
+ # Example usage
83
+ if __name__ == "__main__":
84
+ repo_url = "https://github.com/huggingface/diffusers"
85
+ info = fetch_github_info(repo_url)
86
+
87
+ if info:
88
+ print("Second Last Tag:", info["second_last_tag"])
89
+ print("Latest Release Branch:", info["latest_release_branch"])
graphql_calls.py CHANGED
@@ -18,13 +18,13 @@ class Commit:
18
 
19
 
20
  def call_with_query(query, token):
21
- url = 'https://api.github.com/graphql'
22
- r = requests.post(url, json={'query': query}, headers={'Authorization': f'Bearer {token}'})
23
  return r.json()
24
 
25
 
26
  def get_tag_commit_date(token, repository, tag):
27
- owner, name = repository.split('/')
28
  query = f"""
29
  query GetTagCommit {{
30
  repository(owner: "{owner}", name: "{name}"){{
@@ -47,24 +47,24 @@ def get_tag_commit_date(token, repository, tag):
47
  response = call_with_query(query, token)
48
 
49
  try:
50
- repository = response['data']['repository']['object']
51
 
52
  if repository is None:
53
- if 'errors' in response:
54
- raise ValueError(response['errors'][0]['message'])
55
- raise ValueError('Invalid tag. Does this tag exist?')
56
 
57
- committed_date = repository['committedDate']
58
  except (KeyError, TypeError):
59
- raise ValueError('Invalid token. Does your token have the valid permissions?')
60
 
61
  return committed_date
62
 
63
 
64
  def get_commits(token, repository, branch, since):
65
- owner, name = repository.split('/')
66
 
67
- def get_page_result(next_page=''):
68
  query = f"""
69
  query GetCommits {{
70
  repository(owner: "{owner}", name: "{name}"){{
@@ -100,14 +100,14 @@ def get_commits(token, repository, branch, since):
100
  """
101
  result = call_with_query(query, token)
102
 
103
- if 'data' not in result:
104
- raise ValueError(result['errors'][0]['message'])
105
 
106
- if result['data']['repository']['object'] is None:
107
  raise ValueError("Either the tag or the branch were incorrect.")
108
 
109
- nodes = result['data']['repository']['object']['history']['nodes']
110
- cursor = result['data']['repository']['object']['history']['pageInfo']['endCursor']
111
  return nodes, cursor
112
 
113
  nodes, cursor = get_page_result()
@@ -116,22 +116,28 @@ def get_commits(token, repository, branch, since):
116
  _nodes, cursor = get_page_result(f' after:"{cursor}"')
117
  nodes.extend(_nodes)
118
 
119
-
120
  commits = []
121
  for node in nodes:
122
- if node['author']['user'] is None:
123
- commits.append(Commit(
124
- message=node['message'].split('\n')[0],
125
- user=User(name='<NOT FOUND>', organizations=[]),
126
- additions=node.get('additions'),
127
- deletions=node.get('deletions')
128
- ))
 
 
129
  else:
130
- commits.append(Commit(
131
- message=node['message'].split('\n')[0],
132
- user=User(name=node['author']['user']['login'], organizations=[n['name'] for n in node['author']['user']['organizations']['nodes']]),
133
- additions=node.get('additions'),
134
- deletions=node.get('deletions')
135
- ))
 
 
 
 
 
136
 
137
  return commits
 
18
 
19
 
20
  def call_with_query(query, token):
21
+ url = "https://api.github.com/graphql"
22
+ r = requests.post(url, json={"query": query}, headers={"Authorization": f"Bearer {token}"})
23
  return r.json()
24
 
25
 
26
  def get_tag_commit_date(token, repository, tag):
27
+ owner, name = repository.split("/")
28
  query = f"""
29
  query GetTagCommit {{
30
  repository(owner: "{owner}", name: "{name}"){{
 
47
  response = call_with_query(query, token)
48
 
49
  try:
50
+ repository = response["data"]["repository"]["object"]
51
 
52
  if repository is None:
53
+ if "errors" in response:
54
+ raise ValueError(response["errors"][0]["message"])
55
+ raise ValueError("Invalid tag. Does this tag exist?")
56
 
57
+ committed_date = repository["committedDate"]
58
  except (KeyError, TypeError):
59
+ raise ValueError("Invalid token. Does your token have the valid permissions?")
60
 
61
  return committed_date
62
 
63
 
64
  def get_commits(token, repository, branch, since):
65
+ owner, name = repository.split("/")
66
 
67
+ def get_page_result(next_page=""):
68
  query = f"""
69
  query GetCommits {{
70
  repository(owner: "{owner}", name: "{name}"){{
 
100
  """
101
  result = call_with_query(query, token)
102
 
103
+ if "data" not in result:
104
+ raise ValueError(result["errors"][0]["message"])
105
 
106
+ if result["data"]["repository"]["object"] is None:
107
  raise ValueError("Either the tag or the branch were incorrect.")
108
 
109
+ nodes = result["data"]["repository"]["object"]["history"]["nodes"]
110
+ cursor = result["data"]["repository"]["object"]["history"]["pageInfo"]["endCursor"]
111
  return nodes, cursor
112
 
113
  nodes, cursor = get_page_result()
 
116
  _nodes, cursor = get_page_result(f' after:"{cursor}"')
117
  nodes.extend(_nodes)
118
 
 
119
  commits = []
120
  for node in nodes:
121
+ if node["author"]["user"] is None:
122
+ commits.append(
123
+ Commit(
124
+ message=node["message"].split("\n")[0],
125
+ user=User(name="<NOT FOUND>", organizations=[]),
126
+ additions=node.get("additions"),
127
+ deletions=node.get("deletions"),
128
+ )
129
+ )
130
  else:
131
+ commits.append(
132
+ Commit(
133
+ message=node["message"].split("\n")[0],
134
+ user=User(
135
+ name=node["author"]["user"]["login"],
136
+ organizations=[n["name"] for n in node["author"]["user"]["organizations"]["nodes"]],
137
+ ),
138
+ additions=node.get("additions"),
139
+ deletions=node.get("deletions"),
140
+ )
141
+ )
142
 
143
  return commits