How To Manage A Pull Request In AWS CodeCommit Using AWS CLI
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to manage an AWS CodeCommit repository using AWS CLI.
https://cloudaffaire.com/how-to-manage-an-aws-codecommit-repository-using-aws-cli/
In this blog post, we will discuss how to manage a pull request in AWS CodeCommit using AWS CLI.
What Is A Pull Request In AWS CodeCommit:
A pull request is a primary way you and other repository users can review, comment on, and merge code changes from one branch to another. You can create a copy of the source code in some branch (sometimes refers to fork) to your personal branch, do the required changes and then create a pull request to the source branch to merge your changes in your branch into the source branch.
How To Manage A Pull Request In AWS CodeCommit Using AWS CLI:
Requirements:
AWS CLI v2 installed and configured.
https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
Git installed and configured.
https://www.atlassian.com/git/tutorials/install-git
Two IAM users having programmatic access and AWSCodeCommitFullAccess policy attached.
Step 1: Create some demo directories and configure AWS CLI profiles for the two IAM users.
1 2 3 4 5 6 7 8 9 10 11 12 |
#################################################### ## How To Manage A Pull Request In AWS CodeCommit ## #################################################### ## I am using a Linux shell to execute AWS CLI commands ## create some files for this demo mkdir -p codecmitdemo && cd codecmitdemo mkdir user1 user2 ## configure aws cli for user1 and user2 aws configure --profile user1 aws configure --profile user2 |
Step 2: Create a repository with one file using the ‘user1’ profile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## ------------- ## user1 profile ## ------------- ## create a codecommit repository and upload a file aws codecommit create-repository \ --repository-name "my_aws_repo" \ --repository-description "CloudAffaire test repository" \ --tags "Key=release,Value=v1" \ --profile user1 ## upload a file in the codecommit repository echo "aws" > user1/myfile1.txt && AWS_CODECMIT_COMMIT_ID1=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "master" \ --file-content fileb://user1/myfile1.txt \ --file-path user1/myfile1.txt \ --name "User1" \ --email "user1@cloudaffaire.com" \ --commit-message "user1 added myfile1.txt" \ --query 'commitId' \ --output text \ --profile user1) |
This will serve as our source code which in the next step user2 will fork and create a pull request.
Step 3: Create a branch from the master using the ‘user2’ profile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## ------------- ## user2 profile ## ------------- ## create a branch for user2 aws codecommit create-branch \ --repository-name "my_aws_repo" \ --branch-name "update" \ --commit-id "$AWS_CODECMIT_COMMIT_ID1" \ --profile user2 ## upload a file in user1_branch branch echo "gcp" > user2/myfile2.txt && AWS_CODECMIT_COMMIT_ID2=$(aws codecommit put-file \ --repository-name "my_aws_repo" \ --branch-name "update" \ --file-content fileb://user2/myfile2.txt \ --file-path user2/myfile2.txt \ --name "User2" \ --email "user2@cloudaffaire.com" \ --commit-message "user2 added myfile2.txt" \ --parent-commit-id "$AWS_CODECMIT_COMMIT_ID1" \ --query 'commitId' \ --output text \ --profile user2) |
Step 4: Create a pull request as ‘user2’ profile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
## --------------------- ## create a pull request ## --------------------- ## create a pull request by user2 PULL_REQ_ID=$(aws codecommit create-pull-request \ --title "mypullrequest" \ --description "myfile2.txt added" \ --targets "repositoryName=my_aws_repo,sourceReference=update,destinationReference=master" \ --profile user2 \ --query 'pullRequest.pullRequestId' \ --output text) ## list all pull requests in your codecommit repository aws codecommit list-pull-requests \ --pull-request-status "OPEN" \ --repository-name "my_aws_repo" \ --profile user2 ## get pull request details aws codecommit get-pull-request \ --pull-request-id $PULL_REQ_ID \ --profile user2 |
Step 5: Create a pull request approval rule for the ‘user1’ profile as an approver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
## --------------------------------- ## create pull request approval rule ## --------------------------------- ## create a pull request approver rule policy (approver will be user1) AWS_CODECOMMIT_APPROVER=$(aws sts get-caller-identity \ --query 'Arn' \ --output text \ --profile user1) && cat < { "Version":"2018-11-08", "Statements":[ { "Type":"Approvers", "NumberOfApprovalsNeeded":1, "ApprovalPoolMembers":[ "$AWS_CODECOMMIT_APPROVER" ] } ] } EOF ## create a pull request approval rule by user2 aws codecommit create-pull-request-approval-rule \ --pull-request-id $PULL_REQ_ID \ --approval-rule-name "my_approval_rule" \ --approval-rule-content file://myapproverpolicy.json \ --profile user2 ## get pull request details aws codecommit describe-pull-request-events \ --pull-request-id $PULL_REQ_ID \ --profile user2 ## check if associated approval rules satisfied by user2 PULL_REQ_REV_ID=$(aws codecommit get-pull-request \ --pull-request-id $PULL_REQ_ID \ --query 'pullRequest.revisionId' \ --output text \ --profile user2) && aws codecommit evaluate-pull-request-approval-rules \ --pull-request-id $PULL_REQ_ID \ --revision-id $PULL_REQ_REV_ID \ --profile user2 |
Step 6: Add a comment to the pull request.
1 2 3 4 5 6 7 8 9 10 11 12 |
## ------------- ## add a comment ## ------------- ## create a new comment on the pull request by user1 aws codecommit post-comment-for-pull-request \ --pull-request-id $PULL_REQ_ID \ --repository-name "my_aws_repo" \ --before-commit-id $AWS_CODECMIT_COMMIT_ID1 \ --after-commit-id $AWS_CODECMIT_COMMIT_ID2 \ --content "reviewing" \ --profile user1 |
Step 7: Approve the pull request by the ‘user1’ profile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## ------------------------ ## approve the pull request ## ------------------------ ## approve the pull request by user1 aws codecommit update-pull-request-approval-state \ --pull-request-id $PULL_REQ_ID \ --revision-id $PULL_REQ_REV_ID \ --approval-state "APPROVE" \ --profile user1 ## leave an approval comment by user1 PULL_REQ_CMNT_ID=$(aws codecommit get-comments-for-pull-request \ --repository-name "my_aws_repo" \ --pull-request-id $PULL_REQ_ID \ --before-commit-id $AWS_CODECMIT_COMMIT_ID1 \ --after-commit-id $AWS_CODECMIT_COMMIT_ID2 \ --query 'commentsForPullRequestData[0].comments[0].commentId' \ --output text \ --profile user1) && aws codecommit post-comment-reply \ --in-reply-to $PULL_REQ_CMNT_ID \ --content "looks good, approved" \ --profile user1 ## get all the comments for a pull request by user2 aws codecommit get-comments-for-pull-request \ --repository-name "my_aws_repo" \ --pull-request-id $PULL_REQ_ID \ --before-commit-id $AWS_CODECMIT_COMMIT_ID1 \ --after-commit-id $AWS_CODECMIT_COMMIT_ID2 \ --profile user2 |
Step 8: Get pull request approval status as ‘user2’ profile.
1 2 3 4 5 6 7 8 9 |
## -------------------------------- ## get pull request approval status ## -------------------------------- ## get pull request approval status as user2 aws codecommit get-pull-request-approval-states \ --pull-request-id $PULL_REQ_ID \ --revision-id $PULL_REQ_REV_ID \ --profile user2 |
Step 9: Create a pull merge request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## --------------------------- ## create a pull merge request ## --------------------------- ## create a pull merge request as user2 aws codecommit merge-pull-request-by-fast-forward \ --pull-request-id $PULL_REQ_ID \ --source-commit-id $AWS_CODECMIT_COMMIT_ID2 \ --repository-name "my_aws_repo" \ --profile user2 ## get changes post pull request approval aws codecommit get-differences \ --repository-name "my_aws_repo" \ --before-commit-specifier "$AWS_CODECMIT_COMMIT_ID1" \ --after-commit-specifier "$AWS_CODECMIT_COMMIT_ID2" |
Step 10: Cleanup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ------- ## cleanup ## ------- ## delete the update branch aws codecommit delete-branch \ --repository-name "my_aws_repo" \ --branch-name "update" \ --profile user2 ## delete your codecommit repository aws codecommit delete-repository \ --repository-name "my_aws_repo" ## delete demo directory cd .. && rm -rf codecmitdemo |
Hope you have enjoyed this blog post. To get more details on AWS CodeCommit, please refer below AWS documentation
https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html
https://docs.aws.amazon.com/cli/latest/reference/codecommit/index.html
CodeCommit is a Git-based version control system and you need to have a basic understanding of Git in order to work with CodeCommit. You can follow the below link to get a basic understanding of Git.
https://cloudaffaire.com/category/devops/git/