@@ -15,7 +15,26 @@ const files = [
15
15
'.devcontainer/docker-compose.yml' ,
16
16
] ;
17
17
18
- export async function getPreviousTargetOid ( payload : RegistryPackagePublishedEvent ) {
18
+ export async function shouldUpdateFiles ( octokit : Octokit , oid : string ) {
19
+ const { data : file } = await octokit . rest . repos . getContent ( {
20
+ ...REPOS . electron ,
21
+ path : files [ 0 ] ,
22
+ } ) ;
23
+
24
+ if ( ! ( 'content' in file ) ) {
25
+ throw new Error ( `Incorrectly received array when fetching content for ${ files [ 0 ] } ` ) ;
26
+ }
27
+
28
+ const fileContent = Buffer . from ( file . content , 'base64' ) . toString ( 'utf-8' ) ;
29
+ const match = fileContent . match ( oid ) ;
30
+ if ( match ?. [ 0 ] === oid ) {
31
+ return false ;
32
+ }
33
+
34
+ return true ;
35
+ }
36
+
37
+ export async function getPreviousOid ( payload : RegistryPackagePublishedEvent ) {
19
38
const { registry_package, organization } = payload ;
20
39
const { target_oid, name } = registry_package . package_version ;
21
40
@@ -42,15 +61,14 @@ export async function getPreviousTargetOid(payload: RegistryPackagePublishedEven
42
61
43
62
export async function updateFilesWithNewOid (
44
63
octokit : Octokit ,
45
- filePaths : string [ ] ,
46
- previousTargetOid : string ,
64
+ previousOid : string ,
47
65
targetOid : string ,
48
66
branchName : string ,
49
67
) {
50
68
const d = debug ( `roller/github:updateFilesWithNewOid` ) ;
51
69
let updatedAny = false ;
52
70
53
- for ( const filePath of filePaths ) {
71
+ for ( const filePath of files ) {
54
72
try {
55
73
const { data : file } = await octokit . rest . repos . getContent ( {
56
74
...REPOS . electron ,
@@ -62,20 +80,14 @@ export async function updateFilesWithNewOid(
62
80
}
63
81
64
82
const fileContent = Buffer . from ( file . content , 'base64' ) . toString ( 'utf-8' ) ;
65
- const match = fileContent . match ( previousTargetOid ) ;
83
+ const match = fileContent . match ( previousOid ) ;
66
84
if ( ! match ) {
67
85
d ( `No match found for ${ filePath } ` ) ;
68
86
continue ;
69
87
}
70
88
71
- const currentSha = match [ 0 ] ;
72
- if ( currentSha === targetOid ) {
73
- d ( `No update needed for ${ filePath } ` ) ;
74
- continue ;
75
- }
76
-
77
- d ( `Updating ${ filePath } from ${ currentSha } to ${ targetOid } ` ) ;
78
- const newContent = fileContent . replace ( currentSha , targetOid ) ;
89
+ d ( `Updating ${ filePath } from ${ match [ 0 ] } to ${ targetOid } ` ) ;
90
+ const newContent = fileContent . replace ( match [ 0 ] , targetOid ) ;
79
91
await octokit . rest . repos . createOrUpdateFileContents ( {
80
92
...REPOS . electron ,
81
93
path : filePath ,
@@ -125,46 +137,75 @@ export async function handleBuildImagesCheck(payload: RegistryPackagePublishedEv
125
137
const d = debug ( `roller/github:handleBuildImagesCheck` ) ;
126
138
const octokit = await getOctokit ( ) ;
127
139
128
- const { target_oid } = payload . registry_package . package_version ;
129
- const previousTargetOid = await getPreviousTargetOid ( payload ) ;
130
- console . log ( 'Previous target OID:' , previousTargetOid ) ;
140
+ const { target_oid : targetOid } = payload . registry_package . package_version ;
141
+ const previousOid = await getPreviousOid ( payload ) ;
131
142
132
- if ( ! previousTargetOid ) {
143
+ if ( ! previousOid ) {
133
144
d ( 'No previous target OID found, cannot proceed with updates' ) ;
134
145
return ;
135
146
}
136
147
137
148
const branchName = `roller/build-images/${ MAIN_BRANCH } ` ;
138
- const { ref, sha } = await prepareGitBranch ( octokit , branchName , MAIN_BRANCH ) ;
139
-
140
- d ( `Preparing to update files with new OID: ${ target_oid } ` ) ;
141
- const updatedFiles = await updateFilesWithNewOid (
142
- octokit ,
143
- files ,
144
- previousTargetOid ,
145
- target_oid ,
146
- branchName ,
147
- ) ;
148
-
149
- if ( ! updatedFiles ) {
150
- d ( 'No files were updated, skipping PR creation' ) ;
151
- return ;
152
- }
153
149
154
- d ( `Creating ref=${ ref } at sha=${ sha } ` ) ;
155
- await octokit . rest . git . createRef ( { ...REPOS . electron , ref, sha } ) ;
156
-
157
- d ( `Raising a PR for ${ branchName } to ${ MAIN_BRANCH } ` ) ;
158
- const pr = await octokit . rest . pulls . create ( {
150
+ // Look for a pre-existing PR that targets this branch to see if we can update that.
151
+ const { data : prs } = await octokit . rest . pulls . list ( {
159
152
...REPOS . electron ,
160
- base : MAIN_BRANCH ,
161
153
head : `${ REPOS . electron . owner } :${ branchName } ` ,
162
- title : `build: update build-images to ${ target_oid . slice ( 0 , 7 ) } ` ,
163
- body : `This PR updates the build-images references from ${ previousTargetOid . slice (
164
- 0 ,
165
- 7 ,
166
- ) } to ${ target_oid . slice ( 0 , 7 ) } .`,
154
+ state : 'open' ,
167
155
} ) ;
168
156
169
- d ( `New PR: ${ pr . data . html_url } ` ) ;
157
+ if ( prs . length > 0 ) {
158
+ const pr = prs [ 0 ] ;
159
+ const oid = targetOid . slice ( 0 , 7 ) ;
160
+
161
+ d ( `Found existing PR: #${ pr . number } opened by ${ pr . user . login } - updating` ) ;
162
+
163
+ d ( `Preparing to update files with new OID: ${ targetOid } ` ) ;
164
+ const updatedFiles = await updateFilesWithNewOid ( octokit , previousOid , targetOid , branchName ) ;
165
+
166
+ if ( ! updatedFiles ) {
167
+ d ( 'No files were updated, skipping PR update' ) ;
168
+ return ;
169
+ }
170
+
171
+ await octokit . rest . pulls . update ( {
172
+ ...REPOS . electron ,
173
+ pull_number : pr . number ,
174
+ title : `chore: bump build image tag to ${ oid } ` ,
175
+ body : `This PR updates the build-images references from ${ previousOid . slice (
176
+ 0 ,
177
+ 7 ,
178
+ ) } to ${ oid } .`,
179
+ } ) ;
180
+ return ;
181
+ } else {
182
+ d ( `No existing PR found for ${ branchName } - creating a new one` ) ;
183
+ const { ref, sha } = await prepareGitBranch ( octokit , branchName , MAIN_BRANCH ) ;
184
+
185
+ const shouldUpdate = await shouldUpdateFiles ( octokit , targetOid ) ;
186
+ if ( ! shouldUpdate ) {
187
+ d ( 'Build images are up to date - skipping PR creation' ) ;
188
+ return ;
189
+ }
190
+
191
+ d ( `Creating ref=${ ref } at sha=${ sha } ` ) ;
192
+ await octokit . rest . git . createRef ( { ...REPOS . electron , ref, sha } ) ;
193
+
194
+ d ( `Preparing to update files with new OID: ${ targetOid } ` ) ;
195
+ await updateFilesWithNewOid ( octokit , previousOid , targetOid , branchName ) ;
196
+
197
+ d ( `Raising a PR for ${ branchName } to ${ MAIN_BRANCH } ` ) ;
198
+ const pr = await octokit . rest . pulls . create ( {
199
+ ...REPOS . electron ,
200
+ base : MAIN_BRANCH ,
201
+ head : `${ REPOS . electron . owner } :${ branchName } ` ,
202
+ title : `build: update build-images to ${ targetOid . slice ( 0 , 7 ) } ` ,
203
+ body : `This PR updates the build-images references from ${ previousOid . slice (
204
+ 0 ,
205
+ 7 ,
206
+ ) } to ${ targetOid . slice ( 0 , 7 ) } .`,
207
+ } ) ;
208
+
209
+ d ( `New PR: ${ pr . data . html_url } ` ) ;
210
+ }
170
211
}
0 commit comments