Skip to content

Commit 0545373

Browse files
brmurirenepsmith
andauthored
AWS CodeCommit samples (AWS SDK for JavaScript v3) (#1934)
* update s3 tests * add S3 copy object example * add S3 copy object example * add S3 copy object example * add S3 copy object example * Add AWS CodeCommit examples (Node.js) * Add AWS CodeCommit examples (Node.js) * fixing broken snippets tags * fixing broken snippets tags Co-authored-by: Irene Smith <[email protected]>
1 parent 03468f2 commit 0545373

30 files changed

+827
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# AWS CodeCommit JavaScript SDK v3 code examples
2+
AWS CodeCommit is a fully-managed source control service that makes it easy for companies to host secure and highly scalable private Git repositories.
3+
## Code examples
4+
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) AWS CodeBuild examples:
5+
6+
- [Create a branch](src/createBranch.js)
7+
- [Commit changes to repo](src/createCommit.js)
8+
- [Create a pull request](src/createPullRequest.js)
9+
- [Create a repository](src/createRepository.js)
10+
- [Delete a branch](src/deleteBranch.js)
11+
- [Delete a repository](src/deleteRepository.js)
12+
- [Describe pull request events](src/describePullRequestEvents.js)
13+
- [Get merge options](src/getMergeOptions.js)
14+
- [Get informtion about a pull request](src/getPullRequest.js)
15+
- [Get informatino about a repository](src/getRepository.js)
16+
- [List your repositories](src/listRepositories.js)
17+
- [Merge branches](src/mergeBranches.js)
18+
19+
20+
**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
21+
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).
22+
23+
## Getting started
24+
25+
1. Clone the [AWS SDK Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment. See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for instructions.
26+
27+
2. Install the dependencies listed in the package.json.
28+
29+
```
30+
npm install node -g
31+
cd javascriptv3/example_code/codecommit
32+
npm install
33+
```
34+
3. In your text editor, update user variables specified in the ```Inputs``` section of the sample file.
35+
36+
4. Run sample code:
37+
```
38+
cd src
39+
node [example name].js
40+
```
41+
42+
## Unit tests
43+
For more information see, the [README](../README.rst).
44+
45+
## Resources
46+
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3)
47+
- [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/s3-examples.html)
48+
- [AWS SDK for JavaScript v3 API Reference Guide](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-codecommit/index.html)
49+
- [AWS CodeCommit user guide](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
createBranch.js creates a branch in a repository and points the branch to a commit..
9+
10+
Inputs (replace in code):
11+
- BRANCH_NAME
12+
- COMMIT_ID
13+
- REPOSITORY_NAME
14+
15+
Running the code:
16+
node createRepository.js
17+
*/
18+
// snippet-start:[codeCommit.JavaScript.createRepositoryV3]
19+
// Get service clients module and commands using ES6 syntax.
20+
import { CreateBranchCommand } from "@aws-sdk/client-codecommit";
21+
import { codeCommitClient } from "./libs/codeCommitClient.js";
22+
23+
// Set the parameters.
24+
25+
export const params = {
26+
branchName: 'BRANCH_NAME', /* required */
27+
commitId: 'COMMIT_ID', /* required. The ID of the commit to point the new branch to. */
28+
repositoryName: 'REPOSITORY_NAME' /* required */
29+
};
30+
31+
// Create the AWS CodeDeploy branch.
32+
export const run = async () => {
33+
try {
34+
const data = await codeCommitClient.send(new CreateBranchCommand(params));
35+
console.log("Success", data);
36+
return data; // For unit tests.
37+
} catch (err) {
38+
console.log("Error", err);
39+
}
40+
};
41+
run();
42+
// snippet-end:[codeCommit.JavaScript.createRepositoryV3]
43+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
putFile.js adds or updates a file in a branch in an AWS CodeCommit repository,
9+
and generates a commit for the addition in the specified branch.
10+
11+
Inputs (replace in code):
12+
- BRANCH
13+
- REPOSITORY_NAME
14+
- PATH_AND_FILENAME_WITHIN_REPO
15+
- SOURCE_PATH_AND_FILENAME (optional)
16+
17+
Running the code:
18+
node putFile.js
19+
*/
20+
// snippet-start:[codeCommit.JavaScript.putFileV3]
21+
// Get service clients module and commands using ES6 syntax.
22+
import {
23+
CreateCommitCommand,
24+
GetBranchCommand,
25+
} from "@aws-sdk/client-codecommit";
26+
import { codeCommitClient } from "./libs/codeCommitClient.js";
27+
28+
const BRANCH = "BRANCH";
29+
const REPO = "REPO_NAME";
30+
31+
export const getBranchParams = {
32+
branchName: BRANCH,
33+
repositoryName: REPO,
34+
};
35+
36+
// Add or update the file.
37+
export const run = async () => {
38+
try {
39+
const data = await codeCommitClient.send(
40+
new GetBranchCommand(getBranchParams)
41+
);
42+
return data; // For unit tests.
43+
const COMMIT_ID = data.branch.commitId;
44+
const createCommitParams = {
45+
branchName: BRANCH /* required */,
46+
repositoryName: REPO /* required */,
47+
parentCommitId: COMMIT_ID /* required */,
48+
putFiles: [
49+
{
50+
/* Required. The full path to the file in the repository,
51+
including the name of the file. For example, 'js/index.js' */
52+
filePath: "PATH_AND_FILENAME_WITHIN_REPO",
53+
/* Content to be committed to the file. Required if sourceFile is not specified.*/
54+
fileContent: Buffer.from("STRING"),
55+
/* The name and full path of the file that contains the
56+
changes you want to make as part of the commit. Required if fileContent
57+
is not specified.*/
58+
sourceFile: {
59+
/* Required. The full path to the file, including the name of the file. */
60+
filePath: "SOURCE_PATH_AND_FILENAME",
61+
},
62+
},
63+
/* more items */
64+
],
65+
};
66+
try {
67+
const data = await codeCommitClient.send(
68+
new CreateCommitCommand(createCommitParams)
69+
);
70+
console.log("Success", data.branch.commitId);
71+
return data; // For unit tests.
72+
} catch (err) {
73+
console.log("Error", err);
74+
}
75+
} catch (err) {
76+
console.log("Error", err);
77+
}
78+
};
79+
run();
80+
// snippet-end:[codeCommit.JavaScript.putFileV3]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
createProject.js Creates a pull request in the specified repository using AWS CodeCommit.
9+
10+
Inputs (replace in code):
11+
- REPOSITORY_NAME
12+
- SOURCE_REFERENCE
13+
- DESTINATION_REFERENCE
14+
15+
16+
Running the code:
17+
node createPullRequest.js
18+
*/
19+
// snippet-start:[codeCommit.JavaScript.createPRV3]
20+
// Get service clients module and commands using ES6 syntax.
21+
import { CreatePullRequestCommand } from "@aws-sdk/client-codecommit";
22+
import { codeCommitClient } from "./libs/codeCommitClient.js";
23+
24+
// Set the parameters.
25+
26+
export const params = {
27+
repositoryName: 'STRING_VALUE', /* required */
28+
sourceReference: 'STRING_VALUE', /* required. The branch of the repository that contains the changes for the pull request. */
29+
destinationReference: 'STRING_VALUE' /* The branch of the repository where the pull request changes are merged. */
30+
};
31+
32+
// Create the AWS CodeDeploy repository.
33+
export const run = async () => {
34+
try {
35+
const data = await codeCommitClient.send(new CreatePullRequestCommand(params));
36+
console.log("Success", data);
37+
return data; // For unit tests.
38+
} catch (err) {
39+
console.log("Error", err);
40+
}
41+
};
42+
run();
43+
// snippet-end:[codeCommit.JavaScript.createPRV3]
44+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
createRepository.js demonstrates how to create an AWS CodeCommit repository.
9+
10+
Inputs (replace in code):
11+
- REPOSITORY_NAME
12+
13+
Running the code:
14+
node createRepository.js
15+
*/
16+
// snippet-start:[codeCommit.JavaScript.createRepoV3]
17+
// Get service clients module and commands using ES6 syntax.
18+
import { CreateRepositoryCommand } from "@aws-sdk/client-codecommit";
19+
import { codeCommitClient } from "./libs/codeCommitClient.js";
20+
21+
// Set the parameters.
22+
23+
export const params = {
24+
repositoryName: "REPOSITORY_NAME"
25+
};
26+
27+
// Create the AWS CodeDeploy repository.
28+
export const run = async () => {
29+
try {
30+
const data = await codeCommitClient.send(new CreateRepositoryCommand(params));
31+
console.log("Success", data);
32+
return data; // For unit tests.
33+
} catch (err) {
34+
console.log("Error", err);
35+
}
36+
};
37+
run();
38+
// snippet-end:[codeCommit.JavaScript.createRepoV3]
39+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
deleteBranch.js deletes a branch from a repository, unless that branch is the default branch for the repository.
9+
10+
Inputs (replace in code):
11+
- BRANCH_NAME
12+
- REPOSITORY_NAME
13+
14+
Running the code:
15+
node deleteBranch.js
16+
*/
17+
18+
// snippet-start:[codeCommit.JavaScript.deleteBranchV3]
19+
// Get service clients module and commands using ES6 syntax.
20+
import { DeleteBranchCommand } from "@aws-sdk/client-codecommit";
21+
import { codeCommitClient } from "./libs/codeCommitClient.js";
22+
23+
// Set the parameters.
24+
25+
export const params = {
26+
branchName: 'BRANCH_NAME', /* required */
27+
repositoryName: 'REPOSITORY_NAME' /* required */
28+
};
29+
30+
// Delete the branch.
31+
export const run = async () => {
32+
try {
33+
const data = await codeCommitClient.send(new DeleteBranchCommand(params));
34+
console.log("Success", data);
35+
return data; // For unit tests.
36+
} catch (err) {
37+
console.log("Error", err);
38+
}
39+
};
40+
run();
41+
// snippet-end:[codeCommit.JavaScript.deleteBranchV3]
42+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
deleteRepository.js demonstrates how to delete an AWS CodeBuild repo.
9+
10+
Inputs (replace in code):
11+
- REPOSITORY_NAME
12+
13+
Running the code:
14+
node deleteRepository.js
15+
*/
16+
// snippet-start:[codeCommit.JavaScript.deleteRepoV3]
17+
// Get service clients module and commands using ES6 syntax.
18+
import { DeleteRepositoryCommand } from "@aws-sdk/client-codecommit";
19+
import { codeCommitClient } from "./libs/codeCommitClient.js";
20+
21+
// Set the parameters.
22+
export const params = {
23+
repositoryName: "REPOSITORY_NAME"
24+
};
25+
26+
// Delete the AWS CodeDeploy repository.
27+
export const run = async () => {
28+
try {
29+
const data = await codeCommitClient.send(new DeleteRepositoryCommand(params));
30+
console.log("Success", data);
31+
return data; // For unit tests.
32+
} catch (err) {
33+
console.log("Error", err);
34+
}
35+
};
36+
run();
37+
// snippet-end:[codeCommit.JavaScript.deleteRepoV3]
38+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
SPDX-License-Identifier: Apache-2.0
3+
ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3),
4+
which is available at https://github.com/aws/aws-sdk-js-v3. For information about how AWS CodeCommit works,
5+
see https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html.
6+
7+
Purpose:
8+
describePullRequestEvents.js returns information about one or more pull request events.
9+
10+
Inputs (replace in code):
11+
- PULL_REQUEST_ID
12+
- EVENT_TYPE (optional)
13+
14+
Running the code:
15+
node describePullRequestEvents.js
16+
*/
17+
// snippet-start:[codeCommit.JavaScript.describePREventsV3]
18+
// Get service clients module and commands using ES6 syntax.
19+
import { DescribePullRequestEventsCommand } from "@aws-sdk/client-codecommit";
20+
import { codeCommitClient } from "./libs/codeCommitClient.js";
21+
22+
// Set the parameters.
23+
24+
export const params = {
25+
pullRequestId: 'PULL_REQUEST_ID', /* required */
26+
/* Optional. Options include PULL_REQUEST_CREATED | PULL_REQUEST_STATUS_CHANGED | PULL_REQUEST_SOURCE_REFERENCE_UPDATED |
27+
PULL_REQUEST_MERGE_STATE_CHANGED | PULL_REQUEST_APPROVAL_RULE_CREATED | PULL_REQUEST_APPROVAL_RULE_UPDATED |
28+
PULL_REQUEST_APPROVAL_RULE_DELETED | PULL_REQUEST_APPROVAL_RULE_OVERRIDDEN | PULL_REQUEST_APPROVAL_STATE_CHANGED */
29+
pullRequestEventType: 'EVENT_TYPE'
30+
};
31+
32+
// Describe the PR event.
33+
export const run = async () => {
34+
try {
35+
const data = await codeCommitClient.send(new DescribePullRequestEventsCommand(params));
36+
console.log("Success", data);
37+
return data; // For unit tests.
38+
} catch (err) {
39+
console.log("Error", err);
40+
}
41+
};
42+
run();
43+
// snippet-end:[codeCommit.JavaScript.describePREventsV3]
44+

0 commit comments

Comments
 (0)