1010class 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. \n Error: { stderr } \n Output: { 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
0 commit comments