bluenevus commited on
Commit
f7fc9c2
·
verified ·
1 Parent(s): 721980e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -26
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import dash
2
- from dash import dcc, html, Input, Output, State
3
  import dash_bootstrap_components as dbc
4
  from datetime import datetime, timedelta
5
  import google.generativeai as genai
@@ -19,8 +19,9 @@ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
19
  HF_GEMINI_API_KEY = os.environ.get('HF_GEMINI_API_KEY')
20
  HF_GITHUB_TOKEN = os.environ.get('HF_GITHUB_TOKEN')
21
 
22
- # Global variable to store generated files
23
  generated_file = None
 
24
 
25
  def generate_release_notes(git_provider, repo_url, start_date, end_date, folder_location):
26
  global generated_file
@@ -94,51 +95,61 @@ def generate_release_notes(git_provider, repo_url, start_date, end_date, folder_
94
  generated_file = io.BytesIO(markdown_content.encode())
95
  generated_file.seek(0)
96
 
97
- # Update SUMMARY.md
98
- summary_url = "https://github.com/MicroHealthLLC/maiko-assistant/blob/main/documentation%2Freleases%2FSUMMARY.md"
99
- update_summary(summary_url, file_name)
100
-
101
  return release_notes, file_name
102
 
103
  except Exception as e:
104
  return f"An error occurred: {str(e)}", None
105
 
106
- def update_summary(summary_url, new_file_name):
 
107
  try:
108
  g = Github(HF_GITHUB_TOKEN)
109
- repo = g.get_repo("MicroHealthLLC/maiko-assistant")
110
- summary_content = repo.get_contents("documentation/releases/SUMMARY.md").decoded_content.decode()
 
 
 
111
 
112
  # Add new file to the top of the Releases section
113
  new_entry = f"{datetime.now().strftime('%b %d, %Y')}\n"
114
- updated_content = summary_content.replace("Releases\n", f"Releases\n{new_entry}")
115
 
116
  # Create a new branch for the PR
117
  base_branch = repo.default_branch
118
- new_branch = f"update-summary-{datetime.now().strftime('%Y%m%d%H%M%S')}"
119
  ref = repo.get_git_ref(f"heads/{base_branch}")
120
  repo.create_git_ref(ref=f"refs/heads/{new_branch}", sha=ref.object.sha)
121
 
122
  # Update SUMMARY.md in the new branch
123
  repo.update_file(
124
- "documentation/releases/SUMMARY.md",
125
  f"Update SUMMARY.md with new release notes {new_file_name}",
126
- updated_content,
127
- repo.get_contents("documentation/releases/SUMMARY.md").sha,
 
 
 
 
 
 
 
 
 
128
  branch=new_branch
129
  )
130
 
131
  # Create a pull request
132
  pr = repo.create_pull(
133
- title=f"Update SUMMARY.md with new release notes {new_file_name}",
134
- body="Automatically generated PR to update SUMMARY.md with new release notes.",
135
  head=new_branch,
136
  base=base_branch
137
  )
138
 
139
- return f"Pull request created: {pr.html_url}"
 
140
  except Exception as e:
141
- return f"Error updating SUMMARY.md: {str(e)}"
142
 
143
  # App layout
144
  app.layout = dbc.Container([
@@ -192,7 +203,11 @@ app.layout = dbc.Container([
192
  dbc.Card([
193
  dbc.CardBody([
194
  html.H4("Generated Release Notes"),
195
- html.Pre(id="output-notes", style={"white-space": "pre-wrap"})
 
 
 
 
196
  ])
197
  ]),
198
  dcc.Download(id="download-markdown")
@@ -232,18 +247,30 @@ def download_markdown(n_clicks):
232
  return dcc.send_bytes(generated_file.getvalue(), f"release_notes_{datetime.now().strftime('%Y%m%d%H%M%S')}.md")
233
 
234
  @app.callback(
235
- Output("pr-button", "children"),
 
236
  Input("pr-button", "n_clicks"),
237
- [State("folder-location", "value")],
 
238
  prevent_initial_call=True
239
  )
240
- def create_pr(n_clicks, folder_location):
241
  if n_clicks is None:
242
- return dash.no_update
 
 
 
 
 
 
 
 
 
243
 
244
- # Implement PR creation logic here
245
- # For now, we'll just return a placeholder message
246
- return "PR Created"
 
247
 
248
  if __name__ == '__main__':
249
  print("Starting the Dash application...")
 
1
  import dash
2
+ from dash import dcc, html, Input, Output, State, callback
3
  import dash_bootstrap_components as dbc
4
  from datetime import datetime, timedelta
5
  import google.generativeai as genai
 
19
  HF_GEMINI_API_KEY = os.environ.get('HF_GEMINI_API_KEY')
20
  HF_GITHUB_TOKEN = os.environ.get('HF_GITHUB_TOKEN')
21
 
22
+ # Global variables to store generated files and PR URL
23
  generated_file = None
24
+ pr_url = None
25
 
26
  def generate_release_notes(git_provider, repo_url, start_date, end_date, folder_location):
27
  global generated_file
 
95
  generated_file = io.BytesIO(markdown_content.encode())
96
  generated_file.seek(0)
97
 
 
 
 
 
98
  return release_notes, file_name
99
 
100
  except Exception as e:
101
  return f"An error occurred: {str(e)}", None
102
 
103
+ def update_summary_and_create_pr(repo_url, folder_location, new_file_name, markdown_content):
104
+ global pr_url
105
  try:
106
  g = Github(HF_GITHUB_TOKEN)
107
+ repo = g.get_repo(repo_url)
108
+
109
+ # Get the current content of SUMMARY.md
110
+ summary_path = f"{folder_location}/SUMMARY.md"
111
+ summary_content = repo.get_contents(summary_path).decoded_content.decode()
112
 
113
  # Add new file to the top of the Releases section
114
  new_entry = f"{datetime.now().strftime('%b %d, %Y')}\n"
115
+ updated_summary = summary_content.replace("Releases\n", f"Releases\n{new_entry}")
116
 
117
  # Create a new branch for the PR
118
  base_branch = repo.default_branch
119
+ new_branch = f"update-release-notes-{datetime.now().strftime('%Y%m%d%H%M%S')}"
120
  ref = repo.get_git_ref(f"heads/{base_branch}")
121
  repo.create_git_ref(ref=f"refs/heads/{new_branch}", sha=ref.object.sha)
122
 
123
  # Update SUMMARY.md in the new branch
124
  repo.update_file(
125
+ summary_path,
126
  f"Update SUMMARY.md with new release notes {new_file_name}",
127
+ updated_summary,
128
+ repo.get_contents(summary_path).sha,
129
+ branch=new_branch
130
+ )
131
+
132
+ # Create the new release notes file
133
+ new_file_path = f"{folder_location}/{new_file_name}"
134
+ repo.create_file(
135
+ new_file_path,
136
+ f"Add release notes {new_file_name}",
137
+ markdown_content,
138
  branch=new_branch
139
  )
140
 
141
  # Create a pull request
142
  pr = repo.create_pull(
143
+ title=f"Add release notes {new_file_name} and update SUMMARY.md",
144
+ body="Automatically generated PR to add new release notes and update SUMMARY.md.",
145
  head=new_branch,
146
  base=base_branch
147
  )
148
 
149
+ pr_url = pr.html_url
150
+ return f"Pull request created: {pr_url}"
151
  except Exception as e:
152
+ return f"Error creating PR: {str(e)}"
153
 
154
  # App layout
155
  app.layout = dbc.Container([
 
203
  dbc.Card([
204
  dbc.CardBody([
205
  html.H4("Generated Release Notes"),
206
+ dcc.Loading(
207
+ id="loading-output",
208
+ type="circle",
209
+ children=[html.Pre(id="output-notes", style={"white-space": "pre-wrap"})]
210
+ )
211
  ])
212
  ]),
213
  dcc.Download(id="download-markdown")
 
247
  return dcc.send_bytes(generated_file.getvalue(), f"release_notes_{datetime.now().strftime('%Y%m%d%H%M%S')}.md")
248
 
249
  @app.callback(
250
+ [Output("pr-button", "children"),
251
+ Output("pr-button", "disabled")],
252
  Input("pr-button", "n_clicks"),
253
+ [State("repo-url", "value"),
254
+ State("folder-location", "value")],
255
  prevent_initial_call=True
256
  )
257
+ def create_pr(n_clicks, repo_url, folder_location):
258
  if n_clicks is None:
259
+ return dash.no_update, dash.no_update
260
+
261
+ global generated_file
262
+ if generated_file is None:
263
+ return "Error: No file generated", True
264
+
265
+ file_name = f"{datetime.now().strftime('%m-%d-%Y')}.md"
266
+ markdown_content = generated_file.getvalue().decode()
267
+
268
+ result = update_summary_and_create_pr(repo_url, folder_location, file_name, markdown_content)
269
 
270
+ if pr_url:
271
+ return f"PR Created: {pr_url}", True
272
+ else:
273
+ return result, False
274
 
275
  if __name__ == '__main__':
276
  print("Starting the Dash application...")