11import { GitHubPrMonitor } from './githubPrMonitor' ;
22import { DEFAULT_HEADERS } from './headers' ;
33import { ghResponseToInstance , parseGitHubDetailedPullRequest } from './types' ;
4+ import { invoke } from '$lib/backend/ipc' ;
45import { showToast } from '$lib/notifications/toasts' ;
56import { sleep } from '$lib/utils/sleep' ;
67import posthog from 'posthog-js' ;
7- import { get , writable } from 'svelte/store' ;
8- import type { Persisted } from '$lib/persisted/persisted ' ;
8+ import { writable } from 'svelte/store' ;
9+ import type { GitHostPrService } from '$lib/gitHost/interface/gitHostPrService ' ;
910import type { RepoInfo } from '$lib/url/gitUrl' ;
10- import type { GitHostPrService } from '../interface/gitHostPrService' ;
1111import type {
1212 CreatePullRequestArgs ,
1313 DetailedPullRequest ,
@@ -16,16 +16,12 @@ import type {
1616} from '../interface/types' ;
1717import type { Octokit } from '@octokit/rest' ;
1818
19- const DEFAULT_PULL_REQUEST_TEMPLATE_PATH = '.github/PULL_REQUEST_TEMPLATE.md' ;
20-
2119export class GitHubPrService implements GitHostPrService {
2220 loading = writable ( false ) ;
2321
2422 constructor (
2523 private octokit : Octokit ,
26- private repo : RepoInfo ,
27- private usePullRequestTemplate ?: Persisted < boolean > ,
28- private pullRequestTemplatePath ?: Persisted < string >
24+ private repo : RepoInfo
2925 ) { }
3026
3127 async createPr ( {
@@ -36,33 +32,28 @@ export class GitHubPrService implements GitHostPrService {
3632 upstreamName
3733 } : CreatePullRequestArgs ) : Promise < PullRequest > {
3834 this . loading . set ( true ) ;
39- const request = async ( pullRequestTemplate : string | undefined = '' ) => {
35+ const request = async ( ) => {
4036 const resp = await this . octokit . rest . pulls . create ( {
4137 owner : this . repo . owner ,
4238 repo : this . repo . name ,
4339 head : upstreamName ,
4440 base : baseBranchName ,
4541 title,
46- body : body ? body : pullRequestTemplate ,
42+ body,
4743 draft
4844 } ) ;
45+
4946 return ghResponseToInstance ( resp . data ) ;
5047 } ;
5148
5249 let attempts = 0 ;
5350 let lastError : any ;
5451 let pr : PullRequest | undefined ;
55- let pullRequestTemplate : string | undefined ;
56- const usePrTemplate = this . usePullRequestTemplate ? get ( this . usePullRequestTemplate ) : null ;
57-
58- if ( ! body && usePrTemplate ) {
59- pullRequestTemplate = await this . fetchPrTemplate ( ) ;
60- }
6152
6253 // Use retries since request can fail right after branch push.
6354 while ( attempts < 4 ) {
6455 try {
65- pr = await request ( pullRequestTemplate ) ;
56+ pr = await request ( ) ;
6657 posthog . capture ( 'PR Successful' ) ;
6758 return pr ;
6859 } catch ( err : any ) {
@@ -76,32 +67,6 @@ export class GitHubPrService implements GitHostPrService {
7667 throw lastError ;
7768 }
7869
79- async fetchPrTemplate ( ) {
80- const path = this . pullRequestTemplatePath
81- ? get ( this . pullRequestTemplatePath )
82- : DEFAULT_PULL_REQUEST_TEMPLATE_PATH ;
83-
84- try {
85- const response = await this . octokit . rest . repos . getContent ( {
86- owner : this . repo . owner ,
87- repo : this . repo . name ,
88- path
89- } ) ;
90- const b64Content = ( response . data as any ) ?. content ;
91- if ( b64Content ) {
92- return decodeURIComponent ( escape ( atob ( b64Content ) ) ) ;
93- }
94- } catch ( err ) {
95- console . error ( `Error fetching pull request template at path: ${ path } ` , err ) ;
96-
97- showToast ( {
98- title : 'Failed to fetch pull request template' ,
99- message : `Template not found at path: \`${ path } \`.` ,
100- style : 'neutral'
101- } ) ;
102- }
103- }
104-
10570 async get ( prNumber : number ) : Promise < DetailedPullRequest > {
10671 const resp = await this . octokit . pulls . get ( {
10772 headers : DEFAULT_HEADERS ,
@@ -124,4 +89,36 @@ export class GitHubPrService implements GitHostPrService {
12489 prMonitor ( prNumber : number ) : GitHubPrMonitor {
12590 return new GitHubPrMonitor ( this , prNumber ) ;
12691 }
92+
93+ async pullRequestTemplateContent ( path : string , projectId : string ) {
94+ try {
95+ const fileContents : string | undefined = await invoke ( 'get_pr_template_contents' , {
96+ relativePath : path ,
97+ projectId
98+ } ) ;
99+ return fileContents ;
100+ } catch ( err ) {
101+ console . error ( `Error reading pull request template at path: ${ path } ` , err ) ;
102+
103+ showToast ( {
104+ title : 'Failed to read pull request template' ,
105+ message : `Could not read: \`${ path } \`.` ,
106+ style : 'neutral'
107+ } ) ;
108+ }
109+ }
110+
111+ async availablePullRequestTemplates ( path : string ) : Promise < string [ ] | undefined > {
112+ // TODO: Find a workaround to avoid this dynamic import
113+ // https://github.com/sveltejs/kit/issues/905
114+ const { join } = await import ( '@tauri-apps/api/path' ) ;
115+ const targetPath = await join ( path , '.github' ) ;
116+
117+ const availableTemplates : string [ ] | undefined = await invoke (
118+ 'available_pull_request_templates' ,
119+ { rootPath : targetPath }
120+ ) ;
121+
122+ return availableTemplates ;
123+ }
127124}
0 commit comments