Skip to content

Commit be532ef

Browse files
authored
Merge pull request #1469 from scmacdon/master
Update Code Commit Java V2 examples
2 parents 2839f2a + f492bc5 commit be532ef

12 files changed

+200
-343
lines changed
Lines changed: 75 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,75 @@
1-
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
// snippet-sourcedescription:[CreateBranch.java demonstrates how to create a branch.]
3-
// snippet-service:[AWS CodeCommit]
4-
// snippet-keyword:[Java]
5-
// snippet-keyword:[AWS CodeCommit]
6-
// snippet-keyword:[Code Sample]
7-
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[2020-09-30]
9-
// snippet-sourceauthor:[AWS - scmacdon]
10-
11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
*
14-
* This file is licensed under the Apache License, Version 2.0 (the "License").
15-
* You may not use this file except in compliance with the License. A copy of
16-
* the License is located at
17-
*
18-
* http://aws.amazon.com/apache2.0/
19-
*
20-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
21-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
22-
* specific language governing permissions and limitations under the License.
23-
*
24-
*/
25-
package com.example.commit;
26-
27-
// snippet-start:[codecommit.java2.create_branch.import]
28-
import software.amazon.awssdk.regions.Region;
29-
import software.amazon.awssdk.services.codecommit.CodeCommitClient;
30-
import software.amazon.awssdk.services.codecommit.model.CodeCommitException;
31-
import software.amazon.awssdk.services.codecommit.model.CreateBranchRequest;
32-
// snippet-end:[codecommit.java2.create_branch.import]
33-
34-
public class CreateBranch {
35-
36-
public static void main(String[] args) {
37-
38-
final String USAGE = "\n" +
39-
"Usage:\n" +
40-
" CreateBranch <repoName> <branchName> <commitId> \n\n" +
41-
"Where:\n" +
42-
" repoName - the name of the repository,\n" +
43-
" branchName - the name of the branch,\n" +
44-
" commitId - The ID of the commit to point the new branch to \n";
45-
46-
if (args.length < 3) {
47-
System.out.println(USAGE);
48-
System.exit(1);
49-
}
50-
51-
/* Read the name from command args*/
52-
String repoName = args[0];
53-
String branchName = args[1];
54-
String commitId = args[2];
55-
56-
Region region = Region.US_EAST_1;
57-
CodeCommitClient codeCommitClient = CodeCommitClient.builder()
58-
.region(region)
59-
.build();
60-
61-
createSpecificBranch(codeCommitClient, repoName, branchName, commitId);
62-
}
63-
64-
// snippet-start:[codecommit.java2.create_branch.main]
65-
public static void createSpecificBranch(CodeCommitClient codeCommitClient,
66-
String repoName,
67-
String branchName,
68-
String commitId) {
69-
70-
try {
71-
CreateBranchRequest branchRequest = CreateBranchRequest.builder()
72-
.branchName(branchName)
73-
.repositoryName(repoName)
74-
.commitId(commitId)
75-
.build();
76-
77-
codeCommitClient.createBranch(branchRequest);
78-
System.out.println("Branch "+branchName + " was created");
79-
80-
} catch (CodeCommitException e) {
81-
System.err.println(e.getMessage());
82-
System.exit(1);
83-
}
84-
}
85-
// snippet-end:[codecommit.java2.create_branch.main]
86-
}
1+
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2+
// snippet-sourcedescription:[CreateBranch.java demonstrates how to create a branch.]
3+
//snippet-keyword:[AWS SDK for Java v2]
4+
//snippet-keyword:[Code Sample]
5+
// snippet-service:[AWS CodeCommit]
6+
// snippet-sourcetype:[full-example]
7+
//snippet-sourcedate:[11/03/2020]
8+
// snippet-sourceauthor:[AWS - scmacdon]
9+
10+
/*
11+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
SPDX-License-Identifier: Apache-2.0
13+
*/
14+
package com.example.commit;
15+
16+
// snippet-start:[codecommit.java2.create_branch.import]
17+
import software.amazon.awssdk.regions.Region;
18+
import software.amazon.awssdk.services.codecommit.CodeCommitClient;
19+
import software.amazon.awssdk.services.codecommit.model.CodeCommitException;
20+
import software.amazon.awssdk.services.codecommit.model.CreateBranchRequest;
21+
// snippet-end:[codecommit.java2.create_branch.import]
22+
23+
public class CreateBranch {
24+
25+
public static void main(String[] args) {
26+
27+
final String USAGE = "\n" +
28+
"Usage:\n" +
29+
" CreateBranch <repoName> <branchName> <commitId> \n\n" +
30+
"Where:\n" +
31+
" repoName - the name of the repository.\n" +
32+
" branchName - the name of the branch.\n" +
33+
" commitId - The ID of the commit to point the new branch to. \n";
34+
35+
if (args.length != 3) {
36+
System.out.println(USAGE);
37+
System.exit(1);
38+
}
39+
40+
String repoName = args[0];
41+
String branchName = args[1];
42+
String commitId = args[2];
43+
44+
Region region = Region.US_EAST_1;
45+
CodeCommitClient codeCommitClient = CodeCommitClient.builder()
46+
.region(region)
47+
.build();
48+
49+
createSpecificBranch(codeCommitClient, repoName, branchName, commitId);
50+
codeCommitClient.close();
51+
}
52+
53+
// snippet-start:[codecommit.java2.create_branch.main]
54+
public static void createSpecificBranch(CodeCommitClient codeCommitClient,
55+
String repoName,
56+
String branchName,
57+
String commitId) {
58+
59+
try {
60+
CreateBranchRequest branchRequest = CreateBranchRequest.builder()
61+
.branchName(branchName)
62+
.repositoryName(repoName)
63+
.commitId(commitId)
64+
.build();
65+
66+
codeCommitClient.createBranch(branchRequest);
67+
System.out.println("Branch "+branchName + " was created");
68+
69+
} catch (CodeCommitException e) {
70+
System.err.println(e.getMessage());
71+
System.exit(1);
72+
}
73+
}
74+
// snippet-end:[codecommit.java2.create_branch.main]
75+
}

javav2/example_code/codecommit/src/main/java/com/example/commit/CreatePullRequest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
22
// snippet-sourcedescription:[CreatePullRequest.java demonstrates how to create a pull request.]
3+
//snippet-keyword:[AWS SDK for Java v2]
4+
//snippet-keyword:[Code Sample]
35
// snippet-service:[AWS CodeCommit]
4-
// snippet-keyword:[Java]
5-
// snippet-keyword:[AWS CodeCommit]
6-
// snippet-keyword:[Code Sample]
76
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[2020-09-30]
7+
//snippet-sourcedate:[11/03/2020]
98
// snippet-sourceauthor:[AWS - scmacdon]
109

11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
*
14-
* This file is licensed under the Apache License, Version 2.0 (the "License").
15-
* You may not use this file except in compliance with the License. A copy of
16-
* the License is located at
17-
*
18-
* http://aws.amazon.com/apache2.0/
19-
*
20-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
21-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
22-
* specific language governing permissions and limitations under the License.
23-
*
24-
*/
10+
/*
11+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
SPDX-License-Identifier: Apache-2.0
13+
*/
2514
package com.example.commit;
2615

2716
// snippet-start:[codecommit.java2.create_pr.import]
@@ -43,16 +32,15 @@ public static void main(String[] args) {
4332
"Usage:\n" +
4433
" CreatePullRequest <repoName> <destinationReference> <sourceReference> \n\n" +
4534
"Where:\n" +
46-
" repoName - the name of the repository,\n" +
47-
" destinationReference - the branch of the repository where the pull request changes are merged,\n" +
48-
" sourceReference - the branch of the repository that contains the changes for the pull request.\n" ;
35+
" repoName - the name of the repository.\n" +
36+
" destinationReference - the branch of the repository where the pull request changes are merged.\n" +
37+
" sourceReference - the branch of the repository that contains the changes for the pull request.\n" ;
4938

50-
if (args.length < 3) {
39+
if (args.length != 3) {
5140
System.out.println(USAGE);
5241
System.exit(1);
5342
}
5443

55-
/* Read the name from command args*/
5644
String repoName = args[0];
5745
String destinationReference = args[1];
5846
String sourceReference = args[2];
@@ -62,8 +50,9 @@ public static void main(String[] args) {
6250
.region(region)
6351
.build();
6452

65-
String prId = createPR(codeCommitClient, repoName, destinationReference, sourceReference);
66-
System.out.println("The pull request id is "+prId);
53+
String prId = createPR(codeCommitClient, repoName, destinationReference, sourceReference);
54+
System.out.println("The pull request id is "+prId);
55+
codeCommitClient.close();
6756
}
6857

6958
// snippet-start:[codecommit.java2.create_pr.main]

javav2/example_code/codecommit/src/main/java/com/example/commit/CreateRepository.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
22
// snippet-sourcedescription:[CreateRepository.java demonstrates how to create a new repository.]
3+
//snippet-keyword:[AWS SDK for Java v2]
4+
//snippet-keyword:[Code Sample]
35
// snippet-service:[AWS CodeCommit]
4-
// snippet-keyword:[Java]
5-
// snippet-keyword:[AWS CodeCommit]
6-
// snippet-keyword:[Code Sample]
76
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[2020-09-30]
7+
//snippet-sourcedate:[11/03/2020]
98
// snippet-sourceauthor:[AWS - scmacdon]
109

11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
*
14-
* This file is licensed under the Apache License, Version 2.0 (the "License").
15-
* You may not use this file except in compliance with the License. A copy of
16-
* the License is located at
17-
*
18-
* http://aws.amazon.com/apache2.0/
19-
*
20-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
21-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
22-
* specific language governing permissions and limitations under the License.
23-
*
24-
*/
10+
/*
11+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
SPDX-License-Identifier: Apache-2.0
13+
*/
2514

2615
package com.example.commit;
2716

@@ -41,22 +30,21 @@ public static void main(String[] args) {
4130
"Usage:\n" +
4231
" CreateRepository <repoName> \n\n" +
4332
"Where:\n" +
44-
" repoName - the name of the repository \n" ;
33+
" repoName - the name of the repository. \n" ;
4534

46-
if (args.length < 1) {
35+
if (args.length != 1) {
4736
System.out.println(USAGE);
4837
System.exit(1);
4938
}
5039

51-
/* Read the name from command args*/
5240
String repoName = args[0];
53-
5441
Region region = Region.US_EAST_1;
5542
CodeCommitClient codeCommitClient = CodeCommitClient.builder()
5643
.region(region)
5744
.build();
5845

5946
createRepo(codeCommitClient, repoName);
47+
codeCommitClient.close();
6048
}
6149

6250
// snippet-start:[codecommit.java2.create_repo.main]

javav2/example_code/codecommit/src/main/java/com/example/commit/DeleteBranch.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
11
// snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
22
// snippet-sourcedescription:[DeleteBranch.java demonstrates how to delete a branch.]
3+
//snippet-keyword:[AWS SDK for Java v2]
4+
//snippet-keyword:[Code Sample]
35
// snippet-service:[AWS CodeCommit]
4-
// snippet-keyword:[Java]
5-
// snippet-keyword:[AWS CodeCommit]
6-
// snippet-keyword:[Code Sample]
76
// snippet-sourcetype:[full-example]
8-
// snippet-sourcedate:[2020-09-30]
7+
//snippet-sourcedate:[11/03/2020]
98
// snippet-sourceauthor:[AWS - scmacdon]
109

11-
/**
12-
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
*
14-
* This file is licensed under the Apache License, Version 2.0 (the "License").
15-
* You may not use this file except in compliance with the License. A copy of
16-
* the License is located at
17-
*
18-
* http://aws.amazon.com/apache2.0/
19-
*
20-
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
21-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
22-
* specific language governing permissions and limitations under the License.
23-
*
24-
*/
10+
/*
11+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
12+
SPDX-License-Identifier: Apache-2.0
13+
*/
2514

2615
package com.example.commit;
2716

@@ -40,15 +29,14 @@ public static void main(String[] args) {
4029
"Usage:\n" +
4130
" DeleteBranch <repoName> <branchName> \n\n" +
4231
"Where:\n" +
43-
" repoName - the name of the repository,\n" +
44-
" branchName - the name of the branch \n" ;
32+
" repoName - the name of the repository.\n" +
33+
" branchName - the name of the branch. \n" ;
4534

46-
if (args.length < 3) {
35+
if (args.length != 2) {
4736
System.out.println(USAGE);
4837
System.exit(1);
4938
}
5039

51-
/* Read the name from command args*/
5240
String repoName = args[0];
5341
String branchName = args[1];
5442

@@ -58,6 +46,7 @@ public static void main(String[] args) {
5846
.build();
5947

6048
deleteSpecificBranch(codeCommitClient, repoName, branchName);
49+
codeCommitClient.close();
6150
}
6251

6352
// snippet-start:[codecommit.java2.del_branch.main]

0 commit comments

Comments
 (0)