Skip to content

Commit 294b810

Browse files
Merge pull request #152 from salesforce/meaningul-pr-body-parent-child
Add a meaningful PR body for parent/child commands
2 parents 1ae1dd2 + d285f60 commit 294b810

File tree

11 files changed

+136
-26
lines changed

11 files changed

+136
-26
lines changed

.github/workflows/cd.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ jobs:
7272
with:
7373
name: dockerfile-image-update
7474
- name: Upload coverage to Code Climate
75-
uses: paambaati/codeclimate-action@v2.5.5
75+
uses: paambaati/codeclimate-action@v2.6.0
7676
env:
7777
CC_TEST_REPORTER_ID: ${{ secrets.CODE_CLIMATE_REPORTER_ID }}
7878
JACOCO_SOURCE_PATH: "${{github.workspace}}/dockerfile-image-update/src/main/java"
7979
with:
8080
# The report file must be there, otherwise Code Climate won't find it
8181
coverageCommand: echo "already done"
82-
coverageLocations:
83-
"${{github.workspace}}/dockerfile-image-update/target/site/jacoco/jacoco.xml:jacoco"
82+
coverageLocations: |
83+
${{github.workspace}}/dockerfile-image-update/target/site/jacoco/jacoco.xml:jacoco
8484
master-deploy:
8585
needs: integration-test
8686
runs-on: ubuntu-latest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.salesforce.dockerfileimageupdate.model;
2+
3+
import com.salesforce.dockerfileimageupdate.utils.Constants;
4+
5+
public class PullRequestInfo {
6+
public static final String BODY_TEMPLATE =
7+
"`%s` changed recently. This pull request ensures you're using the latest version of the image and " +
8+
"changes `%s` to the latest tag: `%s`\n" +
9+
"\n" +
10+
"New base image: `%s`";
11+
public static final String DEFAULT_TITLE = "Automatic Dockerfile Image Updater";
12+
public static final String OLD_CONSTANT_BODY = Constants.PULL_REQ_ID;
13+
private final String title;
14+
private final String image;
15+
private final String tag;
16+
17+
public PullRequestInfo(String title, String image, String tag) {
18+
if (title == null || title.trim().isEmpty()) {
19+
this.title = DEFAULT_TITLE;
20+
} else {
21+
this.title = title;
22+
}
23+
this.image = image;
24+
this.tag = tag;
25+
}
26+
27+
/**
28+
* @return title for the pull request
29+
*/
30+
public String getTitle() {
31+
return title;
32+
}
33+
34+
/**
35+
* @return formatted pull request body
36+
*/
37+
public String getBody() {
38+
if (this.image == null && this.tag == null) {
39+
return OLD_CONSTANT_BODY;
40+
}
41+
return String.format(BODY_TEMPLATE, image, image, tag, image + ":" + tag);
42+
}
43+
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/All.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.gson.JsonParseException;
1515
import com.google.gson.JsonParser;
1616
import com.salesforce.dockerfileimageupdate.SubCommand;
17+
import com.salesforce.dockerfileimageupdate.model.PullRequestInfo;
1718
import com.salesforce.dockerfileimageupdate.repository.GitHub;
1819
import com.salesforce.dockerfileimageupdate.subcommands.ExecutableWithNamespace;
1920
import com.salesforce.dockerfileimageupdate.utils.Constants;
@@ -240,7 +241,11 @@ protected void changeDockerfiles(Namespace ns,
240241
}
241242

242243
if (isContentModified) {
243-
dockerfileGitHubUtil.createPullReq(parent, branch, forkedRepo, ns.get(Constants.GIT_PR_TITLE));
244+
// Passing null for image/tag to temporarily maintain old behavior of using constant.
245+
PullRequestInfo pullRequestInfo =
246+
new PullRequestInfo(ns.get(Constants.GIT_PR_TITLE),null, null);
247+
248+
dockerfileGitHubUtil.createPullReq(parent, branch, forkedRepo, pullRequestInfo);
244249
}
245250
}
246251

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/Child.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.salesforce.dockerfileimageupdate.SubCommand;
1212
import com.salesforce.dockerfileimageupdate.model.GitForkBranch;
13+
import com.salesforce.dockerfileimageupdate.model.PullRequestInfo;
1314
import com.salesforce.dockerfileimageupdate.subcommands.ExecutableWithNamespace;
1415
import com.salesforce.dockerfileimageupdate.utils.Constants;
1516
import com.salesforce.dockerfileimageupdate.utils.DockerfileGitHubUtil;
@@ -20,7 +21,7 @@
2021

2122
import java.io.IOException;
2223

23-
@SubCommand(help="updates one specific repository with given tag",
24+
@SubCommand(help = "updates one specific repository with given tag",
2425
requiredParams = {Constants.GIT_REPO, Constants.IMG, Constants.FORCE_TAG}, optionalParams = {"s", Constants.STORE})
2526
public class Child implements ExecutableWithNamespace {
2627
private static final Logger log = LoggerFactory.getLogger(Child.class);
@@ -45,11 +46,18 @@ public void execute(final Namespace ns, final DockerfileGitHubUtil dockerfileGit
4546
}
4647

4748
GitForkBranch gitForkBranch = new GitForkBranch(img, forceTag, branch);
49+
PullRequestInfo pullRequestInfo =
50+
new PullRequestInfo(ns.get(Constants.GIT_PR_TITLE),
51+
gitForkBranch.getImageName(),
52+
gitForkBranch.getImageTag());
4853

4954
dockerfileGitHubUtil.createOrUpdateForkBranchToParentDefault(repo, fork, gitForkBranch);
5055

5156
log.info("Modifying on Github...");
5257
dockerfileGitHubUtil.modifyAllOnGithub(fork, gitForkBranch.getBranchName(), img, forceTag);
53-
dockerfileGitHubUtil.createPullReq(repo, gitForkBranch.getBranchName(), fork, ns.get(Constants.GIT_PR_TITLE));
58+
dockerfileGitHubUtil.createPullReq(repo,
59+
gitForkBranch.getBranchName(),
60+
fork,
61+
pullRequestInfo);
5462
}
5563
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/subcommands/impl/Parent.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.salesforce.dockerfileimageupdate.SubCommand;
1313
import com.salesforce.dockerfileimageupdate.model.GitForkBranch;
1414
import com.salesforce.dockerfileimageupdate.model.GitHubContentToProcess;
15+
import com.salesforce.dockerfileimageupdate.model.PullRequestInfo;
1516
import com.salesforce.dockerfileimageupdate.process.ForkableRepoValidator;
1617
import com.salesforce.dockerfileimageupdate.process.GitHubPullRequestSender;
1718
import com.salesforce.dockerfileimageupdate.subcommands.ExecutableWithNamespace;
@@ -125,8 +126,14 @@ protected void changeDockerfiles(Namespace ns,
125126
}
126127

127128
if (isContentModified) {
129+
PullRequestInfo pullRequestInfo =
130+
new PullRequestInfo(ns.get(Constants.GIT_PR_TITLE),
131+
gitForkBranch.getImageName(), gitForkBranch.getImageTag());
128132
// TODO: get the new PR number and cross post over to old ones
129-
dockerfileGitHubUtil.createPullReq(parent, gitForkBranch.getBranchName(), forkedRepo, ns.get(Constants.GIT_PR_TITLE));
133+
dockerfileGitHubUtil.createPullReq(parent,
134+
gitForkBranch.getBranchName(),
135+
forkedRepo,
136+
pullRequestInfo);
130137
// TODO: Run through PRs in fork to see if they have head branches that match the prefix and close those?
131138
}
132139
}

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.common.collect.Multimap;
1212
import com.salesforce.dockerfileimageupdate.model.FromInstruction;
1313
import com.salesforce.dockerfileimageupdate.model.GitForkBranch;
14+
import com.salesforce.dockerfileimageupdate.model.PullRequestInfo;
1415
import com.salesforce.dockerfileimageupdate.storage.GitHubJsonStore;
1516
import org.kohsuke.github.*;
1617
import org.slf4j.Logger;
@@ -212,13 +213,11 @@ public GitHubJsonStore getGitHubJsonStore(String store) {
212213

213214
public void createPullReq(GHRepository origRepo,
214215
String branch, GHRepository forkRepo,
215-
String title) throws InterruptedException, IOException {
216-
if (title == null) {
217-
title = "Automatic Dockerfile Image Updater";
218-
}
216+
PullRequestInfo pullRequestInfo) throws InterruptedException, IOException {
219217
// TODO: This may loop forever in the event of constant -1 pullRequestExitCodes...
220218
while (true) {
221-
int pullRequestExitCode = gitHubUtil.createPullReq(origRepo, branch, forkRepo, title, Constants.PULL_REQ_ID);
219+
int pullRequestExitCode = gitHubUtil.createPullReq(origRepo,
220+
branch, forkRepo, pullRequestInfo.getTitle(), pullRequestInfo.getBody());
222221
if (pullRequestExitCode == 0) {
223222
return;
224223
} else if (pullRequestExitCode == 1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.salesforce.dockerfileimageupdate.model;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static com.salesforce.dockerfileimageupdate.model.PullRequestInfo.*;
6+
import static org.testng.Assert.*;
7+
8+
public class PullRequestInfoTest {
9+
10+
public static final String IMAGE = "myimage";
11+
public static final String TAG = "tag";
12+
public static final String TITLE = "title";
13+
14+
@Test
15+
public void testGetBody() {
16+
String imageAndTag = IMAGE + ":" + TAG;
17+
PullRequestInfo pullRequestInfo = new PullRequestInfo(TITLE, IMAGE, TAG);
18+
assertEquals(pullRequestInfo.getBody(), String.format(BODY_TEMPLATE, IMAGE, IMAGE, TAG, imageAndTag));
19+
}
20+
21+
@Test
22+
public void testGetBodyIsOldConstantIfImageAndTagNull() {
23+
PullRequestInfo pullRequestInfo = new PullRequestInfo(TITLE, null, null);
24+
assertEquals(pullRequestInfo.getBody(), OLD_CONSTANT_BODY);
25+
}
26+
27+
@Test
28+
public void testGetTitle() {
29+
PullRequestInfo pullRequestInfo = new PullRequestInfo(TITLE, IMAGE, TAG);
30+
assertEquals(pullRequestInfo.getTitle(), TITLE);
31+
}
32+
33+
@Test
34+
public void testGetDefaultTitleIfNull() {
35+
PullRequestInfo pullRequestInfo = new PullRequestInfo(null, IMAGE, TAG);
36+
assertEquals(pullRequestInfo.getTitle(), DEFAULT_TITLE);
37+
}
38+
39+
@Test
40+
public void testGetDefaultTitleIfEmpty() {
41+
PullRequestInfo pullRequestInfo = new PullRequestInfo(" ", IMAGE, TAG);
42+
assertEquals(pullRequestInfo.getTitle(), DEFAULT_TITLE);
43+
}
44+
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/subcommands/impl/AllTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void testChangeDockerfiles_returnIfNotDesiredParent() throws Exception {
170170
Mockito.verify(dockerfileGitHubUtil, times(0))
171171
.modifyOnGithub(any(), anyString(), anyString(), anyString(), anyString());
172172
Mockito.verify(dockerfileGitHubUtil, times(0))
173-
.createPullReq(eq(parentRepo), anyString(), eq(forkedRepo), anyString());
173+
.createPullReq(eq(parentRepo), anyString(), eq(forkedRepo), any());
174174
}
175175

176176
@Test
@@ -192,7 +192,7 @@ public void testChangeDockerfiles_returnWhenForkedRepoNotFound() throws Exceptio
192192
anyString(), anyString());
193193
Mockito.verify(dockerfileGitHubUtil, times(0))
194194
.modifyOnGithub(any(), anyString(), anyString(), anyString(), anyString());
195-
Mockito.verify(dockerfileGitHubUtil, times(0)).createPullReq(any(), anyString(), any(), anyString());
195+
Mockito.verify(dockerfileGitHubUtil, times(0)).createPullReq(any(), anyString(), any(), any());
196196

197197
}
198198

@@ -248,7 +248,7 @@ public void testChangeDockerfiles_pullRequestCreation() throws Exception {
248248
Mockito.verify(dockerfileGitHubUtil, times(1))
249249
.modifyOnGithub(any(), eq("branch"), eq("image2"), eq("tag2"), anyString());
250250
Mockito.verify(dockerfileGitHubUtil, times(1)).createPullReq(eq(parentRepo),
251-
eq("branch"), eq(forkedRepo), anyString());
251+
eq("branch"), eq(forkedRepo), any());
252252
}
253253

254254
@Test
@@ -318,7 +318,7 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
318318

319319
// Only one PR created on the repo with changes to both Dockerfiles.
320320
Mockito.verify(dockerfileGitHubUtil, times(1)).createPullReq(eq(parentRepo),
321-
eq("branch"), eq(forkedRepo), anyString());
321+
eq("branch"), eq(forkedRepo), any());
322322
}
323323

324324
@Test
@@ -372,7 +372,7 @@ public void testNoPullRequestForMissingDockerfile() throws Exception {
372372
Mockito.verify(dockerfileGitHubUtil, times(0))
373373
.modifyOnGithub(any(), anyString(), anyString(), anyString(), anyString());
374374
Mockito.verify(dockerfileGitHubUtil, times(0)).createPullReq(eq(parentRepo),
375-
anyString(), eq(forkedRepo), anyString());
375+
anyString(), eq(forkedRepo), any());
376376
}
377377

378378
@Test
@@ -444,7 +444,7 @@ public void checkPullRequestNotMadeForArchived() throws Exception {
444444
all.changeDockerfiles(ns, pathToDockerfilesInParentRepo, null, null, forkRepo, null);
445445

446446
Mockito.verify(dockerfileGitHubUtil, Mockito.never())
447-
.createPullReq(Mockito.any(), anyString(), Mockito.any(), anyString());
447+
.createPullReq(Mockito.any(), anyString(), Mockito.any(), any());
448448
//Make sure we at least check if its archived
449449
Mockito.verify(parentRepo, Mockito.times(2)).isArchived();
450450
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/subcommands/impl/ChildTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public void checkPullRequestMade(Map<String, Object> inputMap) throws Exception
5454
doNothing().when(dockerfileGitHubUtil).modifyAllOnGithub(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
5555
GitHubJsonStore gitHubJsonStore = mock(GitHubJsonStore.class);
5656
when(dockerfileGitHubUtil.getGitHubJsonStore(anyString())).thenReturn(gitHubJsonStore);
57-
doNothing().when(dockerfileGitHubUtil).createPullReq(Mockito.any(), anyString(), Mockito.any(), anyString());
57+
doNothing().when(dockerfileGitHubUtil).createPullReq(Mockito.any(), anyString(), Mockito.any(), any());
5858

5959
child.execute(ns, dockerfileGitHubUtil);
6060

6161
Mockito.verify(dockerfileGitHubUtil, atLeastOnce())
62-
.createPullReq(Mockito.any(), anyString(), Mockito.any(), anyString());
62+
.createPullReq(Mockito.any(), anyString(), Mockito.any(), any());
6363
}
6464

6565
@Test

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/subcommands/impl/ParentTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void testChangeDockerfiles_pullRequestCreation() throws Exception {
6969
Mockito.verify(dockerfileGitHubUtil, times(1))
7070
.modifyOnGithub(eq(content), eq("image-tag"), eq("image"), eq("tag"), anyString());
7171
Mockito.verify(dockerfileGitHubUtil, times(1))
72-
.createPullReq(eq(parentRepo), eq("image-tag"), eq(forkedRepo), anyString());
72+
.createPullReq(eq(parentRepo), eq("image-tag"), eq(forkedRepo), any());
7373
}
7474

7575
@Test
@@ -120,7 +120,7 @@ public void testOnePullRequestForMultipleDockerfilesInSameRepo() throws Exceptio
120120

121121
// Only one PR created on the repo with changes to both Dockerfiles.
122122
Mockito.verify(dockerfileGitHubUtil, times(1)).createPullReq(eq(parentRepo),
123-
eq("image-tag"), eq(forkedRepo), anyString());
123+
eq("image-tag"), eq(forkedRepo), any());
124124
}
125125

126126
@Test
@@ -160,6 +160,6 @@ public void testNoPullRequestForMissingDockerfile() throws Exception {
160160
Mockito.verify(dockerfileGitHubUtil, times(0))
161161
.modifyOnGithub(any(), anyString(), anyString(), anyString(), anyString());
162162
Mockito.verify(dockerfileGitHubUtil, times(0)).createPullReq(eq(parentRepo),
163-
anyString(), eq(forkedRepo), anyString());
163+
anyString(), eq(forkedRepo), any());
164164
}
165165
}

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package com.salesforce.dockerfileimageupdate.utils;
1010

1111
import com.salesforce.dockerfileimageupdate.model.GitForkBranch;
12+
import com.salesforce.dockerfileimageupdate.model.PullRequestInfo;
1213
import org.kohsuke.github.*;
1314
import org.mockito.Mock;
1415
import org.testng.annotations.DataProvider;
@@ -22,6 +23,7 @@
2223
import java.util.Optional;
2324
import java.util.concurrent.TimeUnit;
2425

26+
import static com.salesforce.dockerfileimageupdate.model.PullRequestInfo.DEFAULT_TITLE;
2527
import static org.mockito.Matchers.*;
2628
import static org.mockito.Mockito.*;
2729
import static org.testng.Assert.*;
@@ -483,8 +485,9 @@ public void testCreatePullReq_Loop() throws Exception {
483485

484486
when(gitHubUtil.createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID))).thenReturn(-1, -1, -1, 0);
485487
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
486-
dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", new GHRepository(), "Automatic Dockerfile Image Updater");
487-
verify(gitHubUtil, times(4)).createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID));
488+
PullRequestInfo pullRequestInfo = new PullRequestInfo(null, null, null);
489+
dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", new GHRepository(), pullRequestInfo);
490+
verify(gitHubUtil, times(4)).createPullReq(any(), anyString(), any(), eq(DEFAULT_TITLE), eq(Constants.PULL_REQ_ID));
488491
}
489492

490493
@Test
@@ -503,7 +506,8 @@ public void testCreatePullReq_Delete() throws Exception {
503506
when(gitHubUtil.createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID))).thenReturn(1);
504507
doCallRealMethod().when(gitHubUtil).safeDeleteRepo(forkRepo);
505508
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
506-
dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", forkRepo, null);
509+
PullRequestInfo pullRequestInfo = new PullRequestInfo(null, null, null);
510+
dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", forkRepo, pullRequestInfo);
507511
verify(gitHubUtil, times(1)).createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID));
508512
verify(forkRepo, times(1)).delete();
509513
}

0 commit comments

Comments
 (0)