Skip to content

Commit 0ffee7d

Browse files
committed
v1.0.5-test
1 parent d89fef8 commit 0ffee7d

File tree

3 files changed

+161
-161
lines changed

3 files changed

+161
-161
lines changed

action.yml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ runs:
7878
body="${body//$'\n'/'%0A'}"
7979
body="${body//$'\r'/'%0D'}"
8080
echo ::set-output name=body::$body
81-
82-
# body=$(cat pr-body.txt)
83-
# echo "pr_body<<EOF" >> $GITHUB_OUTPUT
84-
# body="${body//'%'/'%25'}"
85-
# body="${body//$'\n'/'%0A'}"
86-
# body="${body//$'\r'/'%0D'}"
87-
# echo "pr_body=${body}" >> $GITHUB_OUTPUT
8881
shell: bash
8982

9083
- name: Remove temporary files

scripts/client.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import requests
2+
3+
from enum import Enum
4+
from datetime import datetime
5+
from pydantic import BaseModel
6+
from typing import List, Dict, Any
7+
from urllib.parse import urlencode
8+
9+
10+
class AutodocOutputMode(Enum):
11+
# The output will be the original source code, augmented with inline function docstrings.
12+
INLINE = "inline"
13+
14+
# The output willl be an OpenAPI specification, generated from the source code,
15+
# which is assumed to describe a Restful API.
16+
OPENAPI = "openapi"
17+
18+
19+
class RepositoryResponse(BaseModel):
20+
id: int
21+
owner_id: str
22+
# Format: owner/repo (eg. RunLLM/commons)
23+
name: str
24+
created_at: datetime
25+
updated_at: datetime
26+
27+
28+
class RegisterAutodocRunResponse(BaseModel):
29+
run_id: int
30+
31+
# These are all the supported paths we can generate documentation for.
32+
file_path_to_language: Dict[str, str]
33+
34+
35+
class GenerateAutodocResponse(BaseModel):
36+
documented_content: str
37+
tokens_used: int
38+
39+
40+
class ExplainAutodocResponse(BaseModel):
41+
explanation: str
42+
tokens_used: int
43+
44+
45+
class RunLLMClient:
46+
def __init__(self, server_address: str, api_key: str):
47+
self.server_address = server_address
48+
self.api_key = api_key
49+
50+
def _check_for_error(self, resp: Any) -> None:
51+
if resp.status_code != 200:
52+
raise Exception(f"Request failed with status code {resp.status_code}. Response: {resp.text}")
53+
54+
def _get_default_headers(self) -> Dict[str, str]:
55+
return {
56+
'Content-Type': 'application/json',
57+
"x-api-key": self.api_key,
58+
}
59+
60+
def list_repositories(self) -> List[RepositoryResponse]:
61+
resp = requests.get(
62+
self.server_address + "/api/repositories",
63+
headers=self._get_default_headers(),
64+
)
65+
self._check_for_error(resp)
66+
return [
67+
RepositoryResponse(
68+
**repo_dict
69+
) for repo_dict in resp.json()
70+
]
71+
72+
def create_repository(self, name: str) -> RepositoryResponse:
73+
resp = requests.post(
74+
self.server_address + "/api/repository",
75+
headers=self._get_default_headers(),
76+
json={"name": name},
77+
)
78+
self._check_for_error(resp)
79+
return RepositoryResponse(**resp.json())
80+
81+
def create_autodoc_run(self, repo_id: int, gh_action_url: str, changed_file_paths: List[str]) -> RegisterAutodocRunResponse:
82+
resp = requests.post(
83+
self.server_address + "/api/autodoc",
84+
headers=self._get_default_headers(),
85+
json={
86+
"repo_id": repo_id,
87+
"gh_action_url": gh_action_url,
88+
"file_paths": changed_file_paths,
89+
}
90+
)
91+
self._check_for_error(resp)
92+
return RegisterAutodocRunResponse(**resp.json())
93+
94+
def generate_inline_documentation(self, run_id: int, file_path: str, file_content: str, language: str, diff: str) -> GenerateAutodocResponse:
95+
query_params = urlencode({'file_path': file_path})
96+
resp = requests.post(
97+
self.server_address + f"/api/autodoc/{run_id}?{query_params}",
98+
headers=self._get_default_headers(),
99+
json={
100+
"output_mode": AutodocOutputMode.INLINE.value,
101+
"file_content": file_content,
102+
"language": language,
103+
"changes": diff,
104+
}
105+
)
106+
self._check_for_error(resp)
107+
return GenerateAutodocResponse(**resp.json())
108+
109+
def generate_openapi_spec(self, run_id: int, file_path: str, original_openapi_spec: str, language: str) -> GenerateAutodocResponse:
110+
query_params = urlencode({'file_path': file_path})
111+
resp = requests.post(
112+
self.server_address + f"/api/autodoc/{run_id}?{query_params}",
113+
headers=self._get_default_headers(),
114+
json={
115+
"output_mode": AutodocOutputMode.OPENAPI.value,
116+
"file_content": original_openapi_spec,
117+
"language": language,
118+
}
119+
)
120+
self._check_for_error(resp)
121+
return GenerateAutodocResponse(**resp.json())
122+
123+
def generate_explanation(self, run_id: int, mode: AutodocOutputMode, diff: str) -> ExplainAutodocResponse:
124+
resp = requests.post(
125+
self.server_address + f"/api/autodoc/{run_id}/explanation",
126+
headers=self._get_default_headers(),
127+
json={
128+
"output_mode": mode.value,
129+
"changes": diff,
130+
}
131+
)
132+
self._check_for_error(resp)
133+
return ExplainAutodocResponse(**resp.json())
134+
135+
def mark_completed(self, run_id: int, pull_request_url: str) -> None:
136+
resp = requests.put(
137+
self.server_address + f"/api/autodoc/{run_id}",
138+
headers=self._get_default_headers(),
139+
json={
140+
"status": "Succeeded",
141+
"pull_request_url": pull_request_url,
142+
}
143+
)
144+
self._check_for_error(resp)
145+
146+
def mark_failed(self, run_id: int, error: str) -> None:
147+
resp = requests.put(
148+
self.server_address + f"/api/autodoc/{run_id}",
149+
headers=self._get_default_headers(),
150+
json={
151+
"status": "Failed",
152+
"error": error,
153+
}
154+
)
155+
self._check_for_error(resp)

scripts/generate_docs.py

Lines changed: 6 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import argparse
22
import os
3-
import requests
43
import subprocess
54

6-
from urllib.parse import urlencode
5+
from typing import List, Dict, Optional
76
from collections import defaultdict
8-
from datetime import datetime
9-
from enum import Enum
10-
from pydantic import BaseModel
11-
from typing import List, Dict, Optional, Any
127

8+
from scripts.client import RunLLMClient, AutodocOutputMode
139

1410
# Map of languages to their file extensions
1511
language_extensions = {
@@ -27,154 +23,6 @@ def calculate_cost(total_tokens: int) -> float:
2723
return (total_tokens / 1000) * COST_PER_1K_TOKEN
2824

2925

30-
class AutodocOutputMode(Enum):
31-
# The output will be the original source code, augmented with inline function docstrings.
32-
INLINE = "inline"
33-
34-
# The output willl be an OpenAPI specification, generated from the source code,
35-
# which is assumed to describe a Restful API.
36-
OPENAPI = "openapi"
37-
38-
39-
class RepositoryResponse(BaseModel):
40-
id: int
41-
owner_id: str
42-
# Format: owner/repo (eg. RunLLM/commons)
43-
name: str
44-
created_at: datetime
45-
updated_at: datetime
46-
47-
48-
class RegisterAutodocRunResponse(BaseModel):
49-
run_id: int
50-
51-
# These are all the supported paths we can generate documentation for.
52-
file_path_to_language: Dict[str, str]
53-
54-
55-
class GenerateAutodocResponse(BaseModel):
56-
documented_content: str
57-
tokens_used: int
58-
59-
60-
class ExplainAutodocResponse(BaseModel):
61-
explanation: str
62-
tokens_used: int
63-
64-
65-
class RunLLMClient:
66-
def __init__(self, server_address: str, api_key: str):
67-
self.server_address = server_address
68-
self.api_key = api_key
69-
70-
def _check_for_error(self, resp: Any) -> None:
71-
if resp.status_code != 200:
72-
raise Exception(f"Request failed with status code {resp.status_code}. Response: {resp.text}")
73-
74-
def _get_default_headers(self) -> Dict[str, str]:
75-
return {
76-
'Content-Type': 'application/json',
77-
"x-api-key": self.api_key,
78-
}
79-
80-
def list_repositories(self) -> List[RepositoryResponse]:
81-
resp = requests.get(
82-
self.server_address + "/api/repositories",
83-
headers=self._get_default_headers(),
84-
)
85-
self._check_for_error(resp)
86-
return [
87-
RepositoryResponse(
88-
**repo_dict
89-
) for repo_dict in resp.json()
90-
]
91-
92-
def create_repository(self, name: str) -> RepositoryResponse:
93-
resp = requests.post(
94-
self.server_address + "/api/repository",
95-
headers=self._get_default_headers(),
96-
json={"name": name},
97-
)
98-
self._check_for_error(resp)
99-
return RepositoryResponse(**resp.json())
100-
101-
def create_autodoc_run(self, repo_id: int, gh_action_url: str, changed_file_paths: List[str]) -> RegisterAutodocRunResponse:
102-
resp = requests.post(
103-
self.server_address + "/api/autodoc",
104-
headers=self._get_default_headers(),
105-
json={
106-
"repo_id": repo_id,
107-
"gh_action_url": gh_action_url,
108-
"file_paths": changed_file_paths,
109-
}
110-
)
111-
self._check_for_error(resp)
112-
return RegisterAutodocRunResponse(**resp.json())
113-
114-
def generate_inline_documentation(self, run_id: int, file_path: str, file_content: str, language: str, diff: str) -> GenerateAutodocResponse:
115-
query_params = urlencode({'file_path': file_path})
116-
resp = requests.post(
117-
self.server_address + f"/api/autodoc/{run_id}?{query_params}",
118-
headers=self._get_default_headers(),
119-
json={
120-
"output_mode": AutodocOutputMode.INLINE.value,
121-
"file_content": file_content,
122-
"language": language,
123-
"changes": diff,
124-
}
125-
)
126-
self._check_for_error(resp)
127-
return GenerateAutodocResponse(**resp.json())
128-
129-
def generate_openapi_spec(self, run_id: int, file_path: str, original_openapi_spec: str, language: str) -> GenerateAutodocResponse:
130-
query_params = urlencode({'file_path': file_path})
131-
resp = requests.post(
132-
self.server_address + f"/api/autodoc/{run_id}?{query_params}",
133-
headers=self._get_default_headers(),
134-
json={
135-
"output_mode": AutodocOutputMode.OPENAPI.value,
136-
"file_content": original_openapi_spec,
137-
"language": language,
138-
}
139-
)
140-
self._check_for_error(resp)
141-
return GenerateAutodocResponse(**resp.json())
142-
143-
def generate_explanation(self, run_id: int, mode: AutodocOutputMode, diff: str) -> ExplainAutodocResponse:
144-
resp = requests.post(
145-
self.server_address + f"/api/autodoc/{run_id}/explanation",
146-
headers=self._get_default_headers(),
147-
json={
148-
"output_mode": mode.value,
149-
"changes": diff,
150-
}
151-
)
152-
self._check_for_error(resp)
153-
return ExplainAutodocResponse(**resp.json())
154-
155-
def mark_completed(self, run_id: int, pull_request_url: str) -> None:
156-
resp = requests.put(
157-
self.server_address + f"/api/autodoc/{run_id}",
158-
headers=self._get_default_headers(),
159-
json={
160-
"status": "Succeeded",
161-
"pull_request_url": pull_request_url,
162-
}
163-
)
164-
self._check_for_error(resp)
165-
166-
def mark_failed(self, run_id: int, error: str) -> None:
167-
resp = requests.put(
168-
self.server_address + f"/api/autodoc/{run_id}",
169-
headers=self._get_default_headers(),
170-
json={
171-
"status": "Failed",
172-
"error": error,
173-
}
174-
)
175-
self._check_for_error(resp)
176-
177-
17826
def _partition_diff_by_file_name(diff_lines: List[str]) -> Dict[str, str]:
17927
"""Partition the diff into a dictionary of file paths to their individual str diffs."""
18028
diff_by_file: Dict[str, str] = defaultdict(str)
@@ -194,6 +42,7 @@ def _partition_diff_by_file_name(diff_lines: List[str]) -> Dict[str, str]:
19442

19543
return diff_by_file
19644

45+
19746
def _get_or_create_repo_id(client: RunLLMClient) -> int:
19847
"""Get the repo ID from the RunLLM API.
19948
@@ -210,6 +59,7 @@ def _get_or_create_repo_id(client: RunLLMClient) -> int:
21059
# No repo exists for this user, so we create one.
21160
return client.create_repository(repo_name).id
21261

62+
21363
def _git_diff(cached: bool = False, file_path: Optional[str] = None) -> str:
21464
"""Run git diff and return the output as a string."""
21565
cmd = ["git", "diff"]
@@ -222,12 +72,14 @@ def _git_diff(cached: bool = False, file_path: Optional[str] = None) -> str:
22272
raise Exception(f'Failed to run git diff: {result.stderr.decode("utf-8")}')
22373
return result.stdout.decode("utf-8")
22474

75+
22576
def _git_add(file_path: str) -> None:
22677
cmd = ["git", "add", file_path]
22778
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
22879
if result.returncode != 0:
22980
raise Exception(f'Failed to run git add: {result.stderr.decode("utf-8")}')
23081

82+
23183
def update_docs(
23284
client: RunLLMClient,
23385
gh_action_url: str,

0 commit comments

Comments
 (0)