Skip to content

Commit b9580ec

Browse files
committed
Added revertCommit() (#355).
1 parent 9406aef commit b9580ec

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/main/java/org/gitlab4j/api/CommitsApi.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.gitlab4j.api;
22

3+
import java.util.Arrays;
34
import java.util.Date;
45
import java.util.List;
56
import java.util.Optional;
@@ -550,6 +551,33 @@ public Comment addComment(Object projectIdOrPath, String sha, String note) throw
550551
return (addComment(projectIdOrPath, sha, note, null, null, null));
551552
}
552553

554+
/**
555+
* Create a commit with single file and action.
556+
*
557+
* <pre><code>GitLab Endpoint: POST /projects/:id/repository/commits</code></pre>
558+
*
559+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
560+
* @param branch tame of the branch to commit into. To create a new branch, also provide startBranch
561+
* @param commitMessage the commit message
562+
* @param startBranch the name of the branch to start the new commit from
563+
* @param authorEmail the commit author's email address
564+
* @param authorName the commit author's name
565+
* @param action the CommitAction to commit
566+
* @return the created Commit instance
567+
* @throws GitLabApiException if any exception occurs during execution
568+
*/
569+
public Commit createCommit(Object projectIdOrPath, String branch, String commitMessage, String startBranch,
570+
String authorEmail, String authorName, CommitAction action) throws GitLabApiException {
571+
572+
// Validate the action
573+
if (action == null) {
574+
throw new GitLabApiException("action cannot be null or empty.");
575+
}
576+
577+
return (createCommit(projectIdOrPath, branch, commitMessage, startBranch,
578+
authorEmail, authorName, Arrays.asList(action)));
579+
}
580+
553581
/**
554582
* Create a commit with multiple files and actions.
555583
*
@@ -562,7 +590,7 @@ public Comment addComment(Object projectIdOrPath, String sha, String note) throw
562590
* @param authorEmail the commit author's email address
563591
* @param authorName the commit author's name
564592
* @param actions the array of CommitAction to commit as a batch
565-
* @return the create Commit instance
593+
* @return the created Commit instance
566594
* @throws GitLabApiException if any exception occurs during execution
567595
*/
568596
public Commit createCommit(Object projectIdOrPath, String branch, String commitMessage, String startBranch,
@@ -597,4 +625,23 @@ public Commit createCommit(Object projectIdOrPath, String branch, String commitM
597625
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits");
598626
return (response.readEntity(Commit.class));
599627
}
628+
629+
/**
630+
* Reverts a commit in a given branch.
631+
*
632+
* <pre><code>GitLab Endpoint: POST /projects/:id/repository/commits/:sha/revert</code></pre>
633+
*
634+
* @since GitLab 11.5
635+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
636+
* @param sha the commit SHA to revert
637+
* @param branch the target branch to revert the commit on
638+
* @return a Commit instance holding the reverted commit
639+
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
640+
*/
641+
public Commit revertCommit(Object projectIdOrPath, String sha, String branch) throws GitLabApiException {
642+
GitLabApiForm formData = new GitLabApiForm().withParam("branch", branch, true);
643+
Response response = post(Response.Status.CREATED, formData,
644+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "commits", sha, "revert");
645+
return (response.readEntity(Commit.class));
646+
}
600647
}

0 commit comments

Comments
 (0)