org.gitlab4j.api.models.MergeRequest Java Examples

The following examples show how to use org.gitlab4j.api.models.MergeRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
/**
 * Merge changes to the merge request. If the MR has any conflicts and can not be merged,
 * you'll get a 405 and the error message 'Branch cannot be merged'. If merge request is
 * already merged or closed, you'll get a 406 and the error message 'Method Not Allowed'.
 * If the sha parameter is passed and does not match the HEAD of the source, you'll get
 * a 409 and the error message 'SHA does not match HEAD of source branch'.  If you don't
 * have permissions to accept this merge request, you'll get a 401.
 * <p>
 * PUT /projects/:id/merge_requests/:merge_request_iid/merge
 *
 * @param projectId                  the ID of a project
 * @param mergeRequestId             the internal ID of the merge request
 * @param mergeCommitMessage,        custom merge commit message, optional
 * @param shouldRemoveSourceBranch,  if true removes the source branch, optional
 * @param mergeWhenPipelineSucceeds, if true the MR is merged when the pipeline, optional
 * @return the merged merge request
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest acceptMergeRequest(Integer projectId, Integer mergeRequestId,
                                       String mergeCommitMessage, Boolean shouldRemoveSourceBranch, Boolean mergeWhenPipelineSucceeds)
        throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Form formData = new GitLabApiForm()
            .withParam("merge_commit_message", mergeCommitMessage)
            .withParam("should_remove_source_branch", shouldRemoveSourceBranch)
            .withParam("merge_when_pipeline_succeeds", mergeWhenPipelineSucceeds);

    Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestId, "merge");
    return (response.readEntity(MergeRequest.class));
}
 
Example #2
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
/**
 * Updates an existing merge request. You can change branches, title, or even close the MR.
 * <p>
 * PUT /projects/:id/merge_requests/:merge_request_id
 *
 * @param projectId      the ID of a project
 * @param mergeRequestId the ID of the merge request to update
 * @param sourceBranch   the source branch
 * @param targetBranch   the target branch
 * @param title          the title for the merge request
 * @param description    the description of the merge request
 * @param assigneeId     the Assignee user ID, optional
 * @return the updated merge request
 * @throws GitLabApiException if any exception occurs
 * @deprecated as of release 4.4.3
 */
@Deprecated
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestId, String sourceBranch, String targetBranch, String title, String description,
                                       Integer assigneeId) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Form formData = new Form();
    addFormParam(formData, "source_branch", sourceBranch, false);
    addFormParam(formData, "target_branch", targetBranch, false);
    addFormParam(formData, "title", title, false);
    addFormParam(formData, "description", description, false);
    addFormParam(formData, "assignee_id", assigneeId, false);

    Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestId);
    return (response.readEntity(MergeRequest.class));
}
 
Example #3
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Get all merge requests matching the filter.
 *
 * <pre><code>GitLab Endpoint: GET /merge_requests</code></pre>
 *
 * @param filter a MergeRequestFilter instance with the filter settings
 * @param itemsPerPage the number of MergeRequest instances that will be fetched per page
 * @return all merge requests for the specified project matching the filter
 * @throws GitLabApiException if any exception occurs
 */
public Pager<MergeRequest> getMergeRequests(MergeRequestFilter filter, int itemsPerPage) throws GitLabApiException {

    MultivaluedMap<String, String> queryParams = (filter != null ? filter.getQueryParams().asMap() : null);
    if (filter != null && (filter.getProjectId() != null && filter.getProjectId().intValue() > 0) ||
            (filter.getIids() != null && filter.getIids().size() > 0)) {

        if (filter.getProjectId() == null || filter.getProjectId().intValue() == 0) {
            throw new RuntimeException("project ID cannot be null or 0");
        }

        return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, queryParams, "projects", filter.getProjectId(), "merge_requests"));
    } else {
        return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, queryParams, "merge_requests"));
    }
}
 
Example #4
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Get all merge requests matching the filter.
 *
 * <pre><code>GitLab Endpoint: GET /merge_requests</code></pre>
 *
 * @param filter a MergeRequestFilter instance with the filter settings
 * @param page the page to get
 * @param perPage the number of MergeRequest instances per page
 * @return all merge requests for the specified project matching the filter
 * @throws GitLabApiException if any exception occurs
 */
public List<MergeRequest> getMergeRequests(MergeRequestFilter filter, int page, int perPage) throws GitLabApiException {

    MultivaluedMap<String, String> queryParams = (filter != null ?
        filter.getQueryParams(page, perPage).asMap() : getPageQueryParams(page, perPage));
    Response response;
    if (filter != null && (filter.getProjectId() != null && filter.getProjectId().intValue() > 0) ||
            (filter.getIids() != null && filter.getIids().size() > 0)) {

        if (filter.getProjectId() == null || filter.getProjectId().intValue() == 0) {
            throw new RuntimeException("project ID cannot be null or 0");
        }

        response = get(Response.Status.OK, queryParams, "projects", filter.getProjectId(), "merge_requests");
    } else {
        response = get(Response.Status.OK, queryParams, "merge_requests");
    }

    return (response.readEntity(new GenericType<List<MergeRequest>>() {}));
}
 
Example #5
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a merge request and optionally assigns a reviewer to it.
 * <p>
 * POST /projects/:id/merge_requests
 *
 * @param projectId    the ID of a project, required
 * @param sourceBranch the source branch, required
 * @param targetBranch the target branch, required
 * @param title        the title for the merge request, required
 * @param description  the description of the merge request
 * @param assigneeId   the Assignee user ID, optional
 * @return the created MergeRequest instance
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest createMergeRequest(Integer projectId, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId)
        throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    Form formData = new Form();
    addFormParam(formData, "source_branch", sourceBranch, true);
    addFormParam(formData, "target_branch", targetBranch, true);
    addFormParam(formData, "title", title, true);
    addFormParam(formData, "description", description, false);
    addFormParam(formData, "assignee_id", assigneeId, false);

    Response response = post(Response.Status.CREATED, formData, "projects", projectId, "merge_requests");
    return (response.readEntity(MergeRequest.class));
}
 
Example #6
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 6 votes vote down vote up
/**
 * Updates an existing merge request. You can change branches, title, or even close the MR.
 * <p>
 * PUT /projects/:id/merge_requests/:merge_request_id
 *
 * @param projectId      the ID of a project
 * @param mergeRequestId the internal ID of the merge request to update
 * @param targetBranch   the target branch, optional
 * @param title          the title for the merge request
 * @param assigneeId     the Assignee user ID, optional
 * @param description    the description of the merge request, optional
 * @param stateEvent     new state for the merge request, optional
 * @param labels         comma separated list of labels, optional
 * @param milestoneId    the ID of a milestone, optional
 * @return the updated merge request
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestId, String targetBranch,
                                       String title, Integer assigneeId, String description, StateEvent stateEvent, String labels,
                                       Integer milestoneId) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Form formData = new GitLabApiForm()
            .withParam("target_branch", targetBranch)
            .withParam("title", title)
            .withParam("assignee_id", assigneeId)
            .withParam("description", description)
            .withParam("state_event", stateEvent)
            .withParam("labels", labels)
            .withParam("milestone_id", milestoneId);

    Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestId);
    return (response.readEntity(MergeRequest.class));
}
 
Example #7
Source File: TestSearchApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testProjectlMergeRequestsSearch() throws GitLabApiException {
    List<?> results = (List<?>) gitLabApi.getSearchApi().projectSearch(
            testProject, ProjectSearchScope.MERGE_REQUESTS, TEST_PROJECT_NAME);
    assertNotNull(results);
    if (results.size() > 0) {
        assertTrue(results.get(0).getClass() == MergeRequest.class);
    }
}
 
Example #8
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * Cancel merge when pipeline succeeds. If you don't have permissions to accept this merge request,
 * you'll get a 401. If the merge request is already merged or closed, you get 405 and
 * error message 'Method Not Allowed'. In case the merge request is not set to be merged when the
 * pipeline succeeds, you'll also get a 406 error.
 * <p>
 * PUT /projects/:id/merge_requests/:merge_request_iid/cancel_merge_when_pipeline_succeeds
 *
 * @param projectId      the ID of a project
 * @param mergeRequestId the internal ID of the merge request
 * @return the updated merge request
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest cancelMergeRequest(Integer projectId, Integer mergeRequestId) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Response response = put(Response.Status.OK, null, "projects", projectId, "merge_requests", mergeRequestId, "cancel_merge_when_pipeline_succeeds");
    return (response.readEntity(MergeRequest.class));
}
 
Example #9
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * Approve a merge request.
 * <p>
 * Note: This API endpoint is only available on 8.9 EE and above.
 * <p>
 * POST /projects/:id/merge_requests/:merge_request_iid/approve
 *
 * @param projectId      the project ID of the merge request
 * @param mergeRequestId the internal ID of the merge request
 * @param sha            the HEAD of the merge request, optional
 * @return a MergeRequest instance with approval information included
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest approveMergeRequest(Integer projectId, Integer mergeRequestId, String sha) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Form formData = new GitLabApiForm().withParam("sha", sha);
    Response response = post(Response.Status.OK, formData, "projects", projectId, "merge_requests", mergeRequestId, "approve");
    return (response.readEntity(MergeRequest.class));
}
 
Example #10
Source File: BranchDiscoveryTrait.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
    if (head instanceof BranchSCMHead && request instanceof GitLabSCMSourceRequest) {
        for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
            // only match if the merge request is an origin merge request
            if (m.getSourceProjectId().equals(m.getTargetProjectId())
                && m.getSourceBranch().equalsIgnoreCase(head.getName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example #11
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * Get the merge request with approval information.
 * <p>
 * Note: This API endpoint is only available on 8.9 EE and above.
 * <p>
 * GET /projects/:id/merge_requests/:merge_request_iid/approvals
 *
 * @param projectId      the project ID of the merge request
 * @param mergeRequestId the internal ID of the merge request
 * @return a MergeRequest instance with approval information included
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest getMergeRequestApprovals(Integer projectId, Integer mergeRequestId) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Response response = get(Response.Status.OK, null, "projects", projectId, "merge_requests", mergeRequestId, "approvals");
    return (response.readEntity(MergeRequest.class));
}
 
Example #12
Source File: MergeRequestApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * Unapprove a merge request.
 * <p>
 * Note: This API endpoint is only available on 8.9 EE and above.
 * <p>
 * POST /projects/:id/merge_requests/:merge_request_iid/unapprove
 *
 * @param projectId      the project ID of the merge request
 * @param mergeRequestId the internal ID of the merge request
 * @return a MergeRequest instance with approval information included
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest unapproveMergeRequest(Integer projectId, Integer mergeRequestId) throws GitLabApiException {

    if (projectId == null) {
        throw new GitLabApiException("projectId cannot be null");
    }

    if (mergeRequestId == null) {
        throw new GitLabApiException("mergeRequestId cannot be null");
    }

    Response response = post(Response.Status.OK, (Form) null, "projects", projectId, "merge_requests", mergeRequestId, "unapprove");
    return (response.readEntity(MergeRequest.class));
}
 
Example #13
Source File: MileStonesApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
public List<MergeRequest> getMergeRequest(Integer projectId, Integer milestoneId) throws GitLabApiException {
    if (projectId == null) {
        throw new RuntimeException("projectId cannot be null");
    }
    Response response = get(Response.Status.OK, getDefaultPerPageParam(), "projects", projectId, "milestones", milestoneId, "merge_requests");
    return (response.readEntity(new GenericType<List<MergeRequest>>() {
    }));
}
 
Example #14
Source File: SearchApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
/**
 * Search within the specified group.  If a user is not a member of a group and the group is private,
 * a request on that group will result to a 404 status code.
 *
 * <pre><code>GitLab Endpoint: POST /groups/:groupId/search?scope=:scope&amp;search=:search-query</code></pre>
 *
 * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
 * @param scope search the expression within the specified scope. Currently these scopes are supported:
 *              projects, issues, merge_requests, milestones, users
 * @param search the search query
 * @param itemsPerPage the number of items that will be fetched per page
 * @return a Pager containing the object type specified by the scope
 * @throws GitLabApiException if any exception occurs
 * @since GitLab 10.5
 */
public Pager<?> groupSearch(Object groupIdOrPath, GroupSearchScope scope, String search, int itemsPerPage) throws GitLabApiException {

    GitLabApiForm formData = new GitLabApiForm()
            .withParam("scope", scope, true)
            .withParam("search", search, true);

    switch (scope) {
        case PROJECTS:
            return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(),
                    "groups", getGroupIdOrPath(groupIdOrPath), "search"));

        case ISSUES:
            return (new Pager<Issue>(this, Issue.class, itemsPerPage, formData.asMap(),
                    "groups", getGroupIdOrPath(groupIdOrPath), "search"));

        case MERGE_REQUESTS:
            return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, formData.asMap(),
                    "groups", getGroupIdOrPath(groupIdOrPath), "search"));

        case MILESTONES:
            return (new Pager<Milestone>(this, Milestone.class, itemsPerPage, formData.asMap(),
                    "groups", getGroupIdOrPath(groupIdOrPath), "search"));

        case USERS:
            return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(),
                    "groups", getGroupIdOrPath(groupIdOrPath), "search"));

        default:
            throw new GitLabApiException("Invalid GroupSearchScope [" + scope + "]");
    }
}
 
Example #15
Source File: TestUnitMergeRequestApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    initMocks(this);
    mockedResponse = new MockResponse(MergeRequest.class, "merge-request.json", null);
    when(mockGitLabApi.getApiClient()).thenReturn(mockedGitLabApiClient);

    when(mockedGitLabApiClient.validateSecretToken(any())).thenReturn(true);
    when(mockedGitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>any()))
            .thenReturn(mockedResponse);
}
 
Example #16
Source File: TestNotesApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testMergeRequestNotes() throws GitLabApiException {

    Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
    assertNotNull(project);

    for (MergeRequest mr : gitLabApi.getMergeRequestApi().getMergeRequests(project.getId())) {
        List<Note> notes = gitLabApi.getNotesApi().getMergeRequestNotes(project.getId(), mr.getIid());
        assertNotNull(notes);
    }
}
 
Example #17
Source File: TestNotesApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testMergeRequestNotesPager() throws GitLabApiException {

    Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
    assertNotNull(project);

    for (MergeRequest mr : gitLabApi.getMergeRequestApi().getMergeRequests(project.getId())) {
        Pager<Note> pager = gitLabApi.getNotesApi().getMergeRequestNotes(project.getId(), mr.getIid(), SortOrder.DESC, Note.OrderBy.CREATED_AT, 10);
        assertNotNull(pager);
    }
}
 
Example #18
Source File: TestSearchApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testGlobalMergeRequestsSearch() throws GitLabApiException {
    List<?> results = (List<?>) gitLabApi.getSearchApi().globalSearch(SearchScope.MERGE_REQUESTS, TEST_PROJECT_NAME);
    assertNotNull(results);
    if (results.size() > 0) {
        assertTrue(results.get(0).getClass() == MergeRequest.class);
    }
}
 
Example #19
Source File: TestSearchApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testGrouplMergeRequestsSearch() throws GitLabApiException {
    List<?> results = (List<?>) gitLabApi.getSearchApi().groupSearch(testGroup, GroupSearchScope.MERGE_REQUESTS, TEST_GROUP_PROJECT_NAME);
    assertNotNull(results);
    if (results.size() > 0) {
        assertTrue(results.get(0).getClass() == MergeRequest.class);
    }
}
 
Example #20
Source File: BranchDiscoveryTrait.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
    if (head instanceof BranchSCMHead && request instanceof GitLabSCMSourceRequest) {
        for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
            if (m.getSourceProjectId().equals(m.getTargetProjectId())
                && !m.getSourceBranch().equalsIgnoreCase(head.getName())) {
                return true;
            }
        }
    }
    return false;
}
 
Example #21
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
/**
    * Updates an existing merge request. You can change branches, title, or even close the MR.
    *
    * <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
    *
    * <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid</code></pre>
    *
    * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
    * @param mergeRequestIid the internal ID of the merge request to update
    * @param targetBranch the target branch, optional
    * @param title the title for the merge request
    * @param assigneeId the Assignee user ID, optional
    * @param description the description of the merge request, optional
    * @param stateEvent new state for the merge request, optional
    * @param labels comma separated list of labels, optional
    * @param milestoneId the ID of a milestone, optional
    * @param removeSourceBranch Flag indicating if a merge request should remove the source
    *                           branch when merging, optional
    * @param squash Squash commits into a single commit when merging, optional
    * @param discussionLocked Flag indicating if the merge request's discussion is locked, optional
    * @param allowCollaboration Allow commits from members who can merge to the target branch,
    *                           optional
    * @return the updated merge request
    * @throws GitLabApiException if any exception occurs
    */
   public MergeRequest updateMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
           String targetBranch, String title, Integer assigneeId, String description,
           StateEvent stateEvent, String labels, Integer milestoneId, Boolean removeSourceBranch,
           Boolean squash, Boolean discussionLocked, Boolean allowCollaboration)
           throws GitLabApiException {

String[] labelsArray = null;
if (labels != null) {
    labelsArray = labels.split(",", -1);
}

MergeRequestParams params = new MergeRequestParams()
        .withTargetBranch(targetBranch)
        .withTitle(title)
        .withAssigneeId(assigneeId)
        .withDescription(description)
        .withStateEvent(stateEvent)
        .withLabels(labelsArray)
        .withMilestoneId(milestoneId)
        .withRemoveSourceBranch(removeSourceBranch)
        .withDiscussionLocked(discussionLocked)
        .withAllowCollaboration(allowCollaboration)
        .withSquash(squash);

       return (updateMergeRequest(projectIdOrPath, mergeRequestIid, params));
   }
 
Example #22
Source File: TestGitLabApiBeans.java    From gitlab4j-api with MIT License 4 votes vote down vote up
@Test
public void testMergeRequestApprovals() throws Exception {
    MergeRequest mergeRequestApprovals = unmarshalResource(MergeRequest.class, "approvals.json");
    assertTrue(compareJson(mergeRequestApprovals, "approvals.json"));
}
 
Example #23
Source File: SearchApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
 * Search globally across the GitLab instance.
 *
 * <pre><code>GitLab Endpoint: POST /search?scope=:scope&amp;search=:search-query</code></pre>
 *
 * @param scope search the expression within the specified scope. Currently these scopes are supported:
 *              projects, issues, merge_requests, milestones, snippet_titles, snippet_blobs, users
 * @param search the search query
 * @param itemsPerPage the number of items that will be fetched per page
 * @return a Pager containing the object type specified by the scope
 * @throws GitLabApiException if any exception occurs
 * @since GitLab 10.5
 */
public Pager<?> globalSearch(SearchScope scope, String search, int itemsPerPage) throws GitLabApiException {

    GitLabApiForm formData = new GitLabApiForm()
            .withParam("scope", scope, true)
            .withParam("search", search, true);

    switch (scope) {
        case BLOBS:
            return (new Pager<SearchBlob>(this, SearchBlob.class, itemsPerPage, formData.asMap(), "search"));

        case COMMITS:
            return (new Pager<Commit>(this, Commit.class, itemsPerPage, formData.asMap(), "search"));

        case PROJECTS:
            return (new Pager<Project>(this, Project.class, itemsPerPage, formData.asMap(), "search"));

        case ISSUES:
            return (new Pager<Issue>(this, Issue.class, itemsPerPage, formData.asMap(), "search"));

        case MERGE_REQUESTS:
            return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, formData.asMap(), "search"));

        case MILESTONES:
            return (new Pager<Milestone>(this, Milestone.class, itemsPerPage, formData.asMap(), "search"));

        case SNIPPET_TITLES:
            return (new Pager<Snippet>(this, Snippet.class, itemsPerPage, formData.asMap(), "search"));

        case SNIPPET_BLOBS:
            return (new Pager<Snippet>(this, Snippet.class, itemsPerPage, formData.asMap(), "search"));

        case USERS:
            return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(), "search"));

        case WIKI_BLOBS:
            return (new Pager<SearchBlob>(this, SearchBlob.class, itemsPerPage, formData.asMap(), "search"));

        default:
            throw new GitLabApiException("Invalid SearchScope [" + scope + "]");
    }
}
 
Example #24
Source File: TestGitLabApiBeans.java    From gitlab4j-api with MIT License 4 votes vote down vote up
@Test
public void testMergeRequest() throws Exception {
    MergeRequest mergeRequest = unmarshalResource(MergeRequest.class, "merge-request.json");
    assertTrue(compareJson(mergeRequest, "merge-request.json"));
}
 
Example #25
Source File: SearchApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
 * Search within the specified project.  If a user is not a member of a project and the project is private,
 * a request on that project will result to a 404 status code.
 *
 * <pre><code>GitLab Endpoint: POST /project/:projectId/search?scope=:scope&amp;search=:search-query&amp;ref=ref</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
 * @param scope search the expression within the specified scope. Currently these scopes are supported:
 *               issues, merge_requests, milestones, notes, wiki_blobs, commits, blobs, users
 * @param search the search query
 * @param ref the name of a repository branch or tag to search on. The project’s default branch is used by 
 *             default. This is only applicable for scopes: commits, blobs, and wiki_blobs.
 * @param itemsPerPage the number of items that will be fetched per page
 * @return a Pager containing the object type specified by the scope
 * @throws GitLabApiException if any exception occurs
 * @since GitLab 10.5
 */
public Pager<?> projectSearch(Object projectIdOrPath, ProjectSearchScope scope, String search, String ref, int itemsPerPage) throws GitLabApiException {

    GitLabApiForm formData = new GitLabApiForm()
            .withParam("scope", scope, true)
            .withParam("search", search, true)
            .withParam("ref", ref, false);
    
    if (ref != null) {
        if (!scope.equals(ProjectSearchScope.BLOBS) && !scope.equals(ProjectSearchScope.WIKI_BLOBS) && !scope.equals(ProjectSearchScope.COMMITS)) {
            throw new GitLabApiException("Ref parameter is only applicable for scopes: commits, blobs, and wiki_blobs");
        }
    }

    switch (scope) {
        case BLOBS:
            return (new Pager<SearchBlob>(this, SearchBlob.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case COMMITS:
            return (new Pager<Commit>(this, Commit.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case ISSUES:
            return (new Pager<Issue>(this, Issue.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case MERGE_REQUESTS:
            return (new Pager<MergeRequest>(this, MergeRequest.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case MILESTONES:
            return (new Pager<Milestone>(this, Milestone.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case NOTES:
            return (new Pager<Note>(this, Note.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case WIKI_BLOBS:
            return (new Pager<SearchBlob>(this, SearchBlob.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        case USERS:
            return (new Pager<User>(this, User.class, itemsPerPage, formData.asMap(),
                    "projects", getProjectIdOrPath(projectIdOrPath), "search"));

        default:
            throw new GitLabApiException("Invalid ProjectSearchScope [" + scope + "]");
    }
}
 
Example #26
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
 * Creates a merge request.
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param sourceBranch the source branch, required
 * @param targetBranch the target branch, required
 * @param title the title for the merge request, required
 * @param description the description of the merge request
 * @param assigneeId the Assignee user ID, optional
 * @param targetProjectId the ID of a target project, optional
 * @param labels labels for MR, optional
 * @param milestoneId the ID of a milestone, optional
 * @param removeSourceBranch Flag indicating if a merge request should remove the source branch when merging, optional
 * @param squash Squash commits into a single commit when merging, optional
 * @return the created MergeRequest instance
 * @throws GitLabApiException if any exception occurs
 * @since GitLab Starter 8.17, GitLab CE 11.0.
 */
public MergeRequest createMergeRequest(Object projectIdOrPath, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId,
        Integer targetProjectId, String[] labels, Integer milestoneId, Boolean removeSourceBranch, Boolean squash) throws GitLabApiException {

    MergeRequestParams params = new MergeRequestParams()
        .withSourceBranch(sourceBranch)
        .withTargetBranch(targetBranch)
        .withTitle(title)
        .withDescription(description)
        .withAssigneeId(assigneeId)
        .withTargetProjectId(targetProjectId)
        .withLabels(labels)
        .withMilestoneId(milestoneId)
        .withRemoveSourceBranch(removeSourceBranch)
        .withSquash(squash);

    return(createMergeRequest(projectIdOrPath, params));
}
 
Example #27
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
    * Creates a merge request.
    *
    * <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
    *
    * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
    * @param sourceBranch the source branch, required
    * @param targetBranch the target branch, required
    * @param title the title for the merge request, required
    * @param description the description of the merge request
    * @param assigneeId the Assignee user ID, optional
    * @param targetProjectId the ID of a target project, optional
    * @param labels labels for MR, optional
    * @param milestoneId the ID of a milestone, optional
    * @param removeSourceBranch Flag indicating if a merge request should remove the source branch when merging, optional
    * @return the created MergeRequest instance
    * @throws GitLabApiException if any exception occurs
    */
   public MergeRequest createMergeRequest(Object projectIdOrPath, String sourceBranch, String targetBranch, String title, String description, Integer assigneeId,
               Integer targetProjectId, String[] labels, Integer milestoneId, Boolean removeSourceBranch) throws GitLabApiException {

MergeRequestParams params = new MergeRequestParams()
    .withSourceBranch(sourceBranch)
    .withTargetBranch(targetBranch)
    .withTitle(title)
    .withDescription(description)
    .withAssigneeId(assigneeId)
    .withTargetProjectId(targetProjectId)
    .withLabels(labels)
    .withMilestoneId(milestoneId)
    .withRemoveSourceBranch(removeSourceBranch);

return(createMergeRequest(projectIdOrPath, params));
   }
 
Example #28
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
/**
 * Merge changes to the merge request. If the MR has any conflicts and can not be merged,
 * you'll get a 405 and the error message 'Branch cannot be merged'. If merge request is
 * already merged or closed, you'll get a 406 and the error message 'Method Not Allowed'.
 * If the sha parameter is passed and does not match the HEAD of the source, you'll get
 * a 409 and the error message 'SHA does not match HEAD of source branch'.  If you don't
 * have permissions to accept this merge request, you'll get a 401.
 *
 * <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.  Additionally,
 * mergeWhenPipelineSucceeds sets the merge_when_build_succeeds flag for GitLab API V3.</p>
 *
 * <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid/merge</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param mergeRequestIid the internal ID of the merge request
 * @param mergeCommitMessage, custom merge commit message, optional
 * @param shouldRemoveSourceBranch, if true removes the source branch, optional
 * @param mergeWhenPipelineSucceeds, if true the MR is merged when the pipeline, optional
 * @param sha if present, then this SHA must match the HEAD of the source branch, otherwise the merge will fail, optional
 * @return the merged merge request
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest acceptMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
        String mergeCommitMessage, Boolean shouldRemoveSourceBranch, Boolean mergeWhenPipelineSucceeds, String sha)
        throws GitLabApiException {

    if (mergeRequestIid == null) {
        throw new RuntimeException("mergeRequestIid cannot be null");
    }

    Form formData = new GitLabApiForm()
            .withParam("merge_commit_message", mergeCommitMessage)
            .withParam("should_remove_source_branch", shouldRemoveSourceBranch)
            .withParam((isApiVersion(ApiVersion.V3) ?
                    "merge_when_build_succeeds" : "merge_when_pipeline_succeeds"),
                    mergeWhenPipelineSucceeds)
            .withParam("sha", sha);

    Response response = put(Response.Status.OK, formData.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "merge");
    return (response.readEntity(MergeRequest.class));
}
 
Example #29
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get information about a single merge request as an Optional instance.
 *
 * <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/merge_requests/:merge_request_id</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param mergeRequestIid the internal ID of the merge request
 * @param renderHtml if true response includes rendered HTML for title and description, can be null
 * @param includeDivergedCommitCount if true response includes the commits behind the target branch, can be null
 * @param includeRebaseInProgress if true response includes whether a rebase operation is in progress, can be null
 * @return the specified MergeRequest as an Optional instance instance
 */
public Optional<MergeRequest> getOptionalMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
                                                      Boolean renderHtml, Boolean includeDivergedCommitCount , Boolean includeRebaseInProgress) {
    try {
        return (Optional.ofNullable(getMergeRequest(projectIdOrPath, mergeRequestIid, renderHtml, includeDivergedCommitCount, includeRebaseInProgress)));
    } catch (GitLabApiException glae) {
        return (GitLabApi.createOptionalFromException(glae));
    }
}
 
Example #30
Source File: MergeRequestApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Merge changes to the merge request. If the MR has any conflicts and can not be merged,
 * you'll get a 405 and the error message 'Branch cannot be merged'. If merge request is
 * already merged or closed, you'll get a 406 and the error message 'Method Not Allowed'.
 * If the sha parameter is passed and does not match the HEAD of the source, you'll get
 * a 409 and the error message 'SHA does not match HEAD of source branch'.  If you don't
 * have permissions to accept this merge request, you'll get a 401.
 *
 * <p>NOTE: GitLab API V4 uses IID (internal ID), V3 uses ID to identify the merge request.</p>
 *
 * <pre><code>GitLab Endpoint: PUT /projects/:id/merge_requests/:merge_request_iid/merge</code></pre>
 *
 * @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
 * @param mergeRequestIid the internal ID of the merge request
 * @param params the MergeRequest instance holding the parameters for accepting the merge request
 * @return the merged merge request
 * @throws GitLabApiException if any exception occurs
 */
public MergeRequest acceptMergeRequest(Object projectIdOrPath, Integer mergeRequestIid,
        AcceptMergeRequestParams params) throws GitLabApiException {
    Response response = put(Response.Status.OK, params.getForm().asMap(), "projects",
    	getProjectIdOrPath(projectIdOrPath), "merge_requests", mergeRequestIid, "merge");
    return (response.readEntity(MergeRequest.class));
}