@@ -4,19 +4,19 @@ import { runAndCapture, runCommand } from '../shell/shell.ts';
4
4
export const GH = {
5
5
createPullRequest,
6
6
editPullRequest,
7
- doesBranchHavePullRequest ,
7
+ setPullRequestDraftStatus ,
8
8
listPullRequests,
9
9
getPullRequestInfoForCurrentBranch,
10
10
getPullRequestInfoForBranch,
11
11
} ;
12
12
13
13
interface BasePROptions {
14
14
baseBranch ?: string ;
15
- draftPR ?: boolean ;
16
15
}
17
16
18
17
interface CreatePROptions {
19
18
web ?: boolean ;
19
+ draftPR ?: boolean ;
20
20
}
21
21
22
22
interface ManualTitleAndBody {
@@ -37,6 +37,20 @@ type PullRequestOptions =
37
37
& ( ManualTitleAndBody | AutomaticTitleAndBody ) ;
38
38
type EditPullRequestOptions = BasePROptions & ManualTitleAndBody ;
39
39
40
+ export interface PullRequest {
41
+ title : string ;
42
+ body : string ;
43
+ number : number ;
44
+ isDraft : boolean ;
45
+ baseRefName : string ;
46
+ headRefName : string ;
47
+ commits : Array < {
48
+ messageHeadLine : string ;
49
+ messageBody : string ;
50
+ oid : string ; // 464fc5a0d27f6053647cdb5939a936b91aaa91fc
51
+ } > ;
52
+ }
53
+
40
54
async function getPullRequestInfoForCurrentBranch ( ) {
41
55
const json = await runAndCapture (
42
56
'gh' ,
@@ -79,7 +93,6 @@ async function createPullRequest(options: PullRequestOptions) {
79
93
async function editPullRequest ( options : EditPullRequestOptions ) {
80
94
const args : string [ ] = [ ] ;
81
95
options . baseBranch && args . push ( '--base' , options . baseBranch ) ;
82
- options . draftPR && args . push ( '--draft' ) ;
83
96
84
97
await runCommand (
85
98
'gh' ,
@@ -95,19 +108,14 @@ async function editPullRequest(options: EditPullRequestOptions) {
95
108
) ;
96
109
}
97
110
98
- async function doesBranchHavePullRequest ( branch : string ) : Promise < boolean > {
99
- try {
100
- // gh pr view doesn't work for multi-remote use cases, gh pr list does
101
- // This will give a false positive for prs from a different repository with the same branch name
102
- const prs = await listPullRequests ( { head : branch } ) ;
103
- return prs . length > 0 ;
104
- } catch {
105
- return false ;
106
- }
111
+ async function setPullRequestDraftStatus ( draft : boolean ) {
112
+ const args = draft ? [ '--undo' ] : [ ] ;
113
+ await runCommand ( 'gh' , 'pr' , 'ready' , ...args ) ;
107
114
}
108
115
109
116
async function getPullRequestInfoForBranch ( branch : string ) {
110
117
// gh pr view doesn't work for multi-remote use cases, gh pr list does
118
+ // This will give a false positive for prs from a different repository with the same branch name
111
119
const prs = await listPullRequests ( { head : branch } ) ;
112
120
if ( prs . length < 1 ) {
113
121
return null ;
@@ -135,20 +143,9 @@ async function listPullRequests(options: {
135
143
'list' ,
136
144
...args ,
137
145
'--json' ,
138
- 'title,body,number,commits,headRefName,baseRefName' ,
146
+ 'title,body,number,isDraft, commits,headRefName,baseRefName' ,
139
147
) ;
140
- const prs : Array < {
141
- title : string ;
142
- body : string ;
143
- number : number ;
144
- baseRefName : string ;
145
- headRefName : string ;
146
- commits : Array < {
147
- messageHeadLine : string ;
148
- messageBody : string ;
149
- oid : string ; // 464fc5a0d27f6053647cdb5939a936b91aaa91fc
150
- } > ;
151
- } > = JSON . parse ( json ) ;
148
+ const prs : Array < PullRequest > = JSON . parse ( json ) ;
152
149
153
150
if ( ! Array . isArray ( prs ) ) {
154
151
log . error ( 'Invalid response from gh cli:' , prs ) ;
0 commit comments