Skip to content

Commit 5b3f04c

Browse files
vikram@dagger.iovikram@dagger.io
authored andcommitted
Remove comment and workspace functions from workspace module
Signed-off-by: vikram@dagger.io <Vikram Vaswani>
1 parent 737b55f commit 5b3f04c

3 files changed

Lines changed: 8 additions & 106 deletions

File tree

.dagger/src/book/main.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ async def fix(
9898
ref: Annotated[str, Doc("Git ref")] | None = None,
9999
token: Annotated[Secret, Doc("GitHub API token")] | None = None,
100100
) -> Result:
101+
"""Diagnoses the test failures in the source directory and fixes them. If repository, ref and token are provided, opens a PR with the fixes and posts a comment with the changes"""
101102
if repository and ref and token:
102-
fsummary = await self.fix_ci(source, repository, ref, token)
103+
fsummary = await self.fix_github(source, repository, ref, token)
103104
fdirectory = None
104105
else:
105106
fdirectory = await self.fix_local(source)
@@ -130,14 +131,14 @@ async def fix_local(
130131
return await work.env().output("after").as_workspace().container().directory("/app")
131132

132133
@function
133-
async def fix_ci(
134+
async def fix_github(
134135
self,
135136
source: Annotated[dagger.Directory, DefaultPath("/")],
136137
repository: Annotated[str, Doc("Owner and repository name")],
137138
ref: Annotated[str, Doc("Git ref")],
138139
token: Annotated[Secret, Doc("GitHub API token")],
139140
) -> str:
140-
"""Diagnoses the test failures in the source directory and opens a PR with the fixes"""
141+
"""Diagnoses the test failures in the source repository, opens a PR with the fixes and posts a comment with the changes"""
141142
environment = (
142143
dag.env(privileged=True)
143144
.with_workspace_input("before", dag.workspace(source=source), "the workspace to use for code and tests")
@@ -172,16 +173,13 @@ async def fix_ci(
172173
.file("/tmp/a.diff")
173174
)
174175

175-
#pr_url = await dag.workspace(source=source, token=token).open_pr(repository, ref, diff_file)
176+
# open PR with changes
176177
pr_url = await dag.github_api().create_pr(repository, ref, diff_file, token)
177178

178-
diff = await diff_file.contents()
179-
180179
# post comment with changes
180+
diff = await diff_file.contents()
181181
comment_body = f"{summary}\n\nDiff:\n\n```{diff}```"
182182
comment_body += f"\n\nPR with fixes: {pr_url}"
183-
184-
#comment_url = await dag.workspace(source=source, token=token).comment(repository, ref, comment_body)
185183
comment_url = await dag.github_api().create_comment(repository, ref, comment_body, token)
186184

187185
return f"Comment posted: {comment_url}"

.dagger/workspace/src/workspace/main.py

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
class Workspace:
1111
ctr: Container
1212
source: Directory
13-
token: Secret | None = None
1413

1514
@classmethod
1615
async def create(
1716
cls,
1817
source: Annotated[Directory, Doc("The context for the workspace"), DefaultPath("/")],
19-
token: Annotated[Secret | None, Doc("GitHub API token")],
2018
):
2119
ctr = (
2220
dag
@@ -27,7 +25,7 @@ async def create(
2725
.with_mounted_cache("/root/.cache/pip", dag.cache_volume("python-pip"))
2826
.with_exec(["pip", "install", "-r", "requirements.txt"])
2927
)
30-
return cls(ctr=ctr, source=source, token=token)
28+
return cls(ctr=ctr, source=source)
3129

3230
@function
3331
async def read_file(
@@ -80,100 +78,6 @@ async def test(self) -> str:
8078
raise Exception(f"Tests failed. \nError: {stderr} \nOutput: {stdout}")
8179
return await cmd.stdout()
8280

83-
@function
84-
async def comment(
85-
self,
86-
repository: Annotated[str, Doc("The owner and repository name")],
87-
ref: Annotated[str, Doc("The ref name")],
88-
body: Annotated[str, Doc("The comment body")],
89-
) -> str:
90-
"""Adds a comment to the PR"""
91-
#repository_url = f"https://github.com/{repository}"
92-
pr_number = int(re.search(r"(\d+)", ref).group(1))
93-
plaintext = await self.token.plaintext()
94-
95-
url = f"https://api.github.com/repos/{repository}/issues/{pr_number}/comments"
96-
headers = {
97-
"Authorization": f"Bearer {plaintext}",
98-
"Accept": "application/vnd.github+json"
99-
}
100-
data = {
101-
"body": body
102-
}
103-
response = requests.post(url, headers=headers, json=data)
104-
105-
if response.status_code == 201:
106-
return f"{response.json()['html_url']}"
107-
else:
108-
raise Exception(f"Failed to post comment: {response.status_code} - {response.text}")
109-
110-
111-
@function
112-
async def open_pr(
113-
self,
114-
repository: Annotated[str, Doc("The owner and repository name")],
115-
ref: Annotated[str, Doc("The ref name")],
116-
diff_file: Annotated[File, Doc("The diff file")],
117-
) -> str:
118-
"""Creates a new PR with the changes"""
119-
plaintext = await self.token.plaintext()
120-
pr_number = int(re.search(r"(\d+)", ref).group(1))
121-
new_branch = f"patch-from-pr-{pr_number}"
122-
remote_url = f"https://${{GITHUB_TOKEN}}@github.com/{repository}.git"
123-
diff = await diff_file.contents()
124-
125-
await (
126-
dag
127-
.container()
128-
.from_("alpine/git")
129-
.with_new_file("/tmp/a.diff", f"{diff}")
130-
.with_workdir("/app")
131-
.with_env_variable("GITHUB_TOKEN", plaintext)
132-
.with_exec(["git", "init"])
133-
.with_exec(["git", "config", "user.name", "Dagger Agent"])
134-
.with_exec(["git", "config", "user.email", "vikram@dagger.io"])
135-
.with_exec(["sh", "-c", "git remote add origin " + remote_url])
136-
.with_exec(["git", "fetch", "origin", f"pull/{pr_number}/head:{new_branch}"])
137-
.with_exec(["git", "checkout", new_branch])
138-
.with_exec(["git", "apply", "/tmp/a.diff"])
139-
.with_exec(["git", "add", "."])
140-
.with_exec(["git", "commit", "-m", f"Fixes PR #{pr_number}"])
141-
.with_exec(["git", "push", "--set-upstream", "origin", new_branch])
142-
.stdout()
143-
)
144-
145-
headers = {
146-
"Authorization": f"Bearer {plaintext}",
147-
"Accept": "application/vnd.github+json"
148-
}
149-
pr_url = f"https://api.github.com/repos/{repository}/pulls/{pr_number}"
150-
pr_response = requests.get(pr_url, headers=headers)
151-
152-
if pr_response.status_code != 200:
153-
raise Exception(f"Failed to fetch original PR: {pr_response.text}")
154-
155-
pr_data = pr_response.json()
156-
base_branch = pr_data["head"]["ref"]
157-
158-
create_pr_url = f"https://api.github.com/repos/{repository}/pulls"
159-
head_user = repository.split("/")[0]
160-
head = f"{head_user}:{new_branch}"
161-
162-
payload = {
163-
"title": f"Automated follow-up to PR #{pr_number}",
164-
"body": f"This PR fixes PR #{pr_number} using `{new_branch}`.",
165-
"head": head,
166-
"base": base_branch
167-
}
168-
169-
create_response = requests.post(create_pr_url, headers=headers, json=payload)
170-
if create_response.status_code != 201:
171-
raise Exception(f"Failed to create new PR: {create_response.text}")
172-
173-
new_pr = create_response.json()
174-
return f"{new_pr['html_url']}"
175-
176-
17781
@function
17882
def container(
17983
self

.github/workflows/dagger.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
with:
2929
version: "0.18.16"
3030
verb: call
31-
args: fix-ci --repository=$GITHUB_REPOSITORY --ref=$GITHUB_REF_NAME --token=env://GITHUB_API_TOKEN
31+
args: fix --repository=$GITHUB_REPOSITORY --ref=$GITHUB_REF_NAME --token=env://GITHUB_API_TOKEN
3232
cloud-token: ${{ secrets.DAGGER_CLOUD_TOKEN }}
3333
env:
3434
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

0 commit comments

Comments
 (0)