org.gitlab4j.api.models.RepositoryFile Java Examples

The following examples show how to use org.gitlab4j.api.models.RepositoryFile. 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: RepositoryFileApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Gets the query params based on the API version.
 *
 * @param file the RepositoryFile instance with the info for the query params
 * @param branchName the branch name
 * @param commitMessage the commit message
 * @return a Form instance with the correct query params.
 */
protected Form createForm(RepositoryFile file, String branchName, String commitMessage) {

    Form form = new Form();
    if (isApiVersion(ApiVersion.V3)) {
        addFormParam(form, "file_path", file.getFilePath(), true);
        addFormParam(form, "branch_name", branchName, true);
    } else {
        addFormParam(form, "branch", branchName, true);
    }

    addFormParam(form, "encoding", file.getEncoding(), false);

    // Cannot use addFormParam() as it does not accept an empty or whitespace only string
    String content = file.getContent();
    if (content == null) {
        throw new IllegalArgumentException("content cannot be null");
    }
    form.param("content", content);

    addFormParam(form, "commit_message", commitMessage, true);
    return (form);
}
 
Example #2
Source File: TestRepositoryFileApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void testRepositoryFileGetOptionalFile() throws GitLabApiException, IOException {

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

    Optional<RepositoryFile> fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "README.md", "master");
    assertNotNull(fileInfo.get());

    fileInfo = gitLabApi.getRepositoryFileApi().getOptionalFileInfo(project.getId(), "I-DONT-EXIST", "master");
    assertFalse(fileInfo.isPresent());

    GitLabApiException e = GitLabApi.getOptionalException(fileInfo);
    assertNotNull(e);
    assertEquals(404, e.getHttpStatus());
}
 
Example #3
Source File: TestRepositoryFileApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void testCreateFileAndDeleteFile() throws GitLabApiException {

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

    Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
    assertNotNull(branch);

    RepositoryFile file = new RepositoryFile();
    file.setFilePath(TEST_FILEPATH);
    file.setContent(TEST_CONTENT);
    RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
    assertNotNull(createdFile);

    gitLabApi.getRepositoryFileApi().deleteFile(project.getId(), TEST_FILEPATH, TEST_BRANCH_NAME, "Testing deleteFile().");
    gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
 
Example #4
Source File: TestRepositoryFileApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@Test
public void testCreateFileWithEmptyContent() throws GitLabApiException {

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

    Branch branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, "master");
    assertNotNull(branch);

    RepositoryFile file = new RepositoryFile();
    file.setFilePath(TEST_FILEPATH);
    file.setContent("");
    RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
    assertNotNull(createdFile);

    gitLabApi.getRepositoryFileApi().deleteFile(project.getId(), TEST_FILEPATH, TEST_BRANCH_NAME, "Testing deleteFile().");
    gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME);
}
 
Example #5
Source File: TestPipelineApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
@BeforeClass
public static void setup() {
    // Must setup the connection to the GitLab test server and get the test Project instance
    gitLabApi = baseTestSetup();
    testProject = getTestProject();

    Optional<RepositoryFile> fileInfo =
            gitLabApi.getRepositoryFileApi().getOptionalFileInfo(testProject, ".gitlab-ci.yml", "master");
    if (fileInfo.isPresent()) {
        gitlabCiYml = fileInfo.get();
    } else {

        try {
            RepositoryFile file = new RepositoryFile();
            file.setFilePath(".gitlab-ci.yml");
            file.setContent(TEST_GITLAB_CI_YML_CONTENT);
            createdGitlabCiYml = gitLabApi.getRepositoryFileApi().createFile(
                    testProject, file, "master", "Need for testing pipelines.");
            gitlabCiYml = createdGitlabCiYml;
            System.out.println("Created .gitlab-ci.yml file for testing purposes.");
        } catch (Exception ignore) {}
    }
}
 
Example #6
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 6 votes vote down vote up
/**
 * Get information on a file in the repository. Allows you to receive information about file in repository like name, size.
 * Only works with GitLab 11.1.0+, returns an empty object for earlier versions of GitLab.
 *
 * <pre><code>GitLab Endpoint: HEAD /projects/:id/repository/files</code></pre>
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param ref (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info
 * @throws GitLabApiException if any exception occurs
 * @since GitLab-11.1.0
 */
public RepositoryFile getFileInfo(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {

    Form form = new Form();
    addFormParam(form, "ref", (ref != null ? urlEncode(ref) : null), true);
    Response response = head(Response.Status.OK, form.asMap(),
            "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));

    RepositoryFile file = new RepositoryFile();
    file.setBlobId(response.getHeaderString("X-Gitlab-Blob-Id"));
    file.setCommitId(response.getHeaderString("X-Gitlab-Commit-Id"));

    String encoding = response.getHeaderString("X-Gitlab-Encoding");
    file.setEncoding(Constants.Encoding.forValue(encoding));

    file.setFileName(response.getHeaderString("X-Gitlab-File-Name"));
    file.setFilePath(response.getHeaderString("X-Gitlab-File-Path"));
    file.setLastCommitId(response.getHeaderString("X-Gitlab-Last-Commit-Id"));
    file.setRef(response.getHeaderString("X-Gitlab-Ref"));

    String sizeStr = response.getHeaderString("X-Gitlab-Size");
    file.setSize(sizeStr != null ? Integer.valueOf(sizeStr) : -1);

    return (file);
}
 
Example #7
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testCreateCommit() throws GitLabApiException {

    // Make sure the file to create does not exist.
    if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master").isPresent()) {
        gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master", "Deleted test file");
    }

    // Arrange
    CommitAction commitAction = new CommitAction()
            .withAction(Action.CREATE)
            .withContent("This is the original data in the file")
            .withFilePath(TEST_CREATE_COMMIT_FILEPATH);

    // Act
    Commit commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() create action", null, null, null, Arrays.asList(commitAction));

    // Assert
    assertNotNull(commit);

    // Arrange
    commitAction = new CommitAction()
            .withAction(Action.DELETE)
            .withFilePath(TEST_CREATE_COMMIT_FILEPATH);

    // Act
    commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() delete action", null, null, null, Arrays.asList(commitAction));

    // Assert
    assertNotNull(commit);

    Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master");
    assertFalse(repoFile.isPresent());
}
 
Example #8
Source File: TestRepositoryFileApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testRepositoryFileGetFile() throws GitLabApiException, IOException {

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

    RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README.md", "master");
    assertNotNull(fileInfo);
}
 
Example #9
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@BeforeClass
public static void setup() {
    // Must setup the connection to the GitLab test server and get the test Project instance
    gitLabApi = baseTestSetup();
    testProject = getTestProject();

    if (!gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_PROJECT_SUBDIRECTORY_PATH, "master").isPresent()) {
        try {
            RepositoryFile repoFile = new RepositoryFile();
            repoFile.setFilePath(TEST_PROJECT_SUBDIRECTORY_PATH);
            repoFile.setContent("This is a test project used to test GitLab4J-API.");
            gitLabApi.getRepositoryFileApi().createFile(testProject, repoFile, "master", "Initial commit.");
        } catch (GitLabApiException ignore) {}
    }
}
 
Example #10
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testCreateCommitFromFile() throws GitLabApiException {

    // Make sure the file to create does not exist.
    if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master").isPresent()) {
        try {
            gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master", "Deleted test file");
        } catch (GitLabApiException ignore) {}
    }

    // Arrange
    CommitAction commitAction = new CommitAction()
            .withAction(Action.CREATE)
            .withContent("This is the original data in the file")
            .withFilePath(TEST_CREATE_COMMIT_FILEPATH);

    // Act
    Commit commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() create action", null, null, null, Arrays.asList(commitAction));

    // Assert
    assertNotNull(commit);

    // Arrange
    commitAction = new CommitAction()
            .withAction(Action.DELETE)
            .withFilePath(TEST_CREATE_COMMIT_FILEPATH);

    // Act
    commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() delete action", null, null, null, Arrays.asList(commitAction));

    // Assert
    assertNotNull(commit);

    Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, "master");
    assertFalse(repoFile.isPresent());
}
 
Example #11
Source File: RepositoryFileApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
private Form file2form(RepositoryFile file, String branchName, String commitMessage) {

        Form form = new Form();
        if (isApiVersion(ApiVersion.V3)) {
            addFormParam(form, "file_path", file.getFilePath(), true);
            addFormParam(form, "branch_name", branchName, true);
        } else {
            addFormParam(form, "branch", branchName, true);
        }

        addFormParam(form, "encoding", file.getEncoding(), false);
        addFormParam(form, "content", file.getContent(), true);
        addFormParam(form, "commit_message", commitMessage, true);
        return form;
    }
 
Example #12
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@Test
public void testRevertCommit() throws GitLabApiException {

    // Make sure the file to create does not exist.
    String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test";
    if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) {
        gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file");
    }

    // Arrange
    CommitAction commitAction = new CommitAction()
            .withAction(Action.CREATE)
            .withContent("This is the original data in the file")
            .withFilePath(filePath);

    // Act
    Commit commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() create action", null, null, null, commitAction);

    // Assert
    assertNotNull(commit);
    Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
    assertTrue(repoFile.isPresent());

    // Act
    Commit revertedCommit = gitLabApi.getCommitsApi().revertCommit(testProject, commit.getId(), "master");

    // Assert
    assertNotEquals(commit.getId(), revertedCommit.getId());
    repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
    assertFalse(repoFile.isPresent());
}
 
Example #13
Source File: GitUtils.java    From swagger-showdoc with Apache License 2.0 5 votes vote down vote up
/**
 * 从Git中下载文件
 * @param filePath
 * @param projectName
 * @param ref
 * @return
 */
public static String downLoadFile(String filePath, String projectName, String ref){
    RepositoryFile file;
    byte[] bytes;
    try {
        Integer projectId = getProjectId(projectName);
        file = gitLabApi.getRepositoryFileApi().getFile(filePath, projectId, ref);
        String content = file.getContent();
        bytes = Base64.decodeBase64(content.getBytes());
    return new String(bytes,"UTF-8");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
 
Example #14
Source File: RepositoryFileApi.java    From choerodon-starters with Apache License 2.0 5 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, content.
 * Note that file content is Base64 encoded.
 * <p>
 * GET /projects/:id/repository/files
 *
 * @param filePath  (required) - Full path to new file. Ex. lib/class.rb
 * @param projectId (required) - the project ID
 * @param ref       (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {

    if (isApiVersion(ApiVersion.V3)) {
        return (getFileV3(filePath, projectId, ref));
    }

    Form form = new Form();
    addFormParam(form, "ref", ref, true);
    Response response = get(Response.Status.OK, form.asMap(),
            "projects", projectId, "repository", "files", urlEncode(filePath));
    return (response.readEntity(RepositoryFile.class));
}
 
Example #15
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
@Test
public void testCherryPickCommit() throws GitLabApiException {
    
    // Make sure the branch to cherry pick does not exist
    if(gitLabApi.getRepositoryApi().getOptionalBranch(testProject, "cherry-pick-branch").isPresent()) {
       gitLabApi.getRepositoryApi().deleteBranch(testProject, "cherry-pick-branch");
    }
    
    // Make sure the file to create does not exist.
    String filePath = TEST_CREATE_COMMIT_FILEPATH + ".test";
    if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master").isPresent()) {
        gitLabApi.getRepositoryFileApi().deleteFile(testProject, filePath, "master", "Deleted test file");
    }

    // Act
    Branch branch = gitLabApi.getRepositoryApi().createBranch(testProject, "cherry-pick-branch", "master");
    
    // Assert
    assertNotNull(branch);
    Optional<RepositoryFile> repoFileBranch = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
    assertFalse(repoFileBranch.isPresent());
    
    // Arrange
    CommitAction commitAction = new CommitAction()
            .withAction(Action.CREATE)
            .withContent("This is the original data in the file")
            .withFilePath(filePath);

    // Act
    Commit commit = gitLabApi.getCommitsApi().createCommit(
            testProject, "master", "Testing createCommit() create action", null, null, null, commitAction);

    // Assert
    assertNotNull(commit);
    Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, "master");
    assertTrue(repoFile.isPresent());
    
    // Act
    Commit cherryPickedCommit = gitLabApi.getCommitsApi().cherryPickCommit(testProject, commit.getId(), "cherry-pick-branch");
    
    // Assert
    assertNotNull(cherryPickedCommit);
    Optional<RepositoryFile> repoFileBranchCherryPicked = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, filePath, branch.getName());
    assertTrue(repoFileBranchCherryPicked.isPresent());
}
 
Example #16
Source File: TestGitLabApiBeans.java    From gitlab4j-api with MIT License 4 votes vote down vote up
@Test
public void testRepositoryFile() throws Exception {
    RepositoryFile file = unmarshalResource(RepositoryFile.class, "repository-file.json");
    assertTrue(compareJson(file, "repository-file.json"));
}
 
Example #17
Source File: TestCommitsApi.java    From gitlab4j-api with MIT License 4 votes vote down vote up
@Test
public void testCreateCommitWithPayload() throws GitLabApiException {

    String TEST_BRANCH = "create_commit_from_payload";

    Optional<Branch> testBranch = gitLabApi.getRepositoryApi().getOptionalBranch(testProject, TEST_BRANCH);
    if (!testBranch.isPresent()) {
        gitLabApi.getRepositoryApi().createBranch(testProject, TEST_BRANCH, "master");
    }

    if (gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH).isPresent()) {
        try {
            gitLabApi.getRepositoryFileApi().deleteFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH, "Deleted test file");
        } catch (GitLabApiException ignore) {}
    }

    // Arrange
    CommitPayload commitPayload = new CommitPayload()
            .withBranch(TEST_BRANCH)
            .withCommitMessage("Testing createCommit() create action")
            .withAction(Action.CREATE, "This is the original data in the file", TEST_CREATE_COMMIT_FILEPATH);

    // Act
    Commit commit = gitLabApi.getCommitsApi().createCommit(testProject, commitPayload);

    // Assert
    assertNotNull(commit);

    // Arrange
    commitPayload = new CommitPayload()
            .withBranch(TEST_BRANCH)
            .withCommitMessage("Testing createCommit() delete action")
            .withAction(Action.DELETE, TEST_CREATE_COMMIT_FILEPATH);

    // Act
    commit = gitLabApi.getCommitsApi().createCommit(testProject, commitPayload);

    // Assert
    assertNotNull(commit);

    Optional<RepositoryFile> repoFile = gitLabApi.getRepositoryFileApi().getOptionalFile(testProject, TEST_CREATE_COMMIT_FILEPATH, TEST_BRANCH);
    assertFalse(repoFile.isPresent());
}
 
Example #18
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Update existing file in repository
 *
 * <pre><code>GitLab Endpoint: PUT /projects/:id/repository/files</code></pre>
 *
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param file a ReposityoryFile instance with info for the file to update
 * @param branchName the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the updated file info
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {

    Form formData = createForm(file, branchName, commitMessage);
    Response response;
    if (isApiVersion(ApiVersion.V3)) {
        response = put(Response.Status.OK, formData.asMap(),
                "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files");
    } else {
        response = put(Response.Status.OK, formData.asMap(),
                "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
    }

    return (response.readEntity(RepositoryFile.class));
}
 
Example #19
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Create new file in repository
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/repository/files</code></pre>
 *
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param file a ReposityoryFile instance with info for the file to create
 * @param branchName the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the created file info
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {

    Form formData = createForm(file, branchName, commitMessage);
    Response response;
    if (isApiVersion(ApiVersion.V3)) {
        response = post(Response.Status.CREATED, formData,
                "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files");
    } else {
        response = post(Response.Status.CREATED, formData,
                "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
    }

    return (response.readEntity(RepositoryFile.class));
}
 
Example #20
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, content.
 * Note that file content is Base64 encoded.
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/repository/files</code></pre>
 *
 * @param filePath (required) - Full path to new file. Ex. lib/class.rb
 * @param projectId (required) - the project ID
 * @param ref (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info
 * @throws GitLabApiException if any exception occurs
 * @deprecated  Will be removed in version 5.0
 */
@Deprecated
protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException {
    Form form = new Form();
    addFormParam(form, "file_path", filePath, true);
    addFormParam(form, "ref", ref, true);
    Response response = get(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files");
    return (response.readEntity(RepositoryFile.class));
}
 
Example #21
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, and optionally content.
 * Note that file content is Base64 encoded.
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/repository/files</code></pre>
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param ref (required) - The name of branch, tag or commit
 * @param includeContent if true will also fetch file content
 * @return a RepositoryFile instance with the file info and optionally file content
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref, boolean includeContent) throws GitLabApiException {

    if (!includeContent) {
        return (getFileInfo(projectIdOrPath, filePath, ref));
    }

    Form form = new Form();
    addFormParam(form, "ref", (ref != null ? urlEncode(ref) : null), true);
    Response response = get(Response.Status.OK, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
    return (response.readEntity(RepositoryFile.class));
}
 
Example #22
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, content.
 * Note that file content is Base64 encoded.
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/repository/files</code></pre>
 *
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param projectId (required) - the project ID
 * @param ref (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info and file content
 * @throws GitLabApiException if any exception occurs
 * @deprecated  Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
 */
@Deprecated
public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {

    if (isApiVersion(ApiVersion.V3)) {
        return (getFileV3(filePath, projectId, ref));
    } else {
        return (getFile(projectId, filePath, ref, true));
    }
}
 
Example #23
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get an Optional instance with the value holding information and content for a file in the repository.
 * Allows you to receive information about file in repository like name, size, and content.
 * Only works with GitLab 11.1.0+, value will be an empty object for earlier versions of GitLab.
 *
 * <pre><code>GitLab Endpoint: HEAD /projects/:id/repository/files</code></pre>
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param ref (required) - The name of branch, tag or commit
 * @return an Optional instance with the specified RepositoryFile as a value
 */
public Optional<RepositoryFile> getOptionalFile(Object projectIdOrPath, String filePath, String ref) {
    try {
        return (Optional.ofNullable(getFile(projectIdOrPath, filePath, ref, true)));
    } catch (GitLabApiException glae) {
        return (GitLabApi.createOptionalFromException(glae));
    }
}
 
Example #24
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 3 votes vote down vote up
/**
 * Get an Optional instance with the value holding information on a file in the repository.
 * Allows you to receive information about file in repository like name, size.
 * Only works with GitLab 11.1.0+, value will be an empty object for earlier versions of GitLab.
 *
 * <pre><code>GitLab Endpoint: HEAD /projects/:id/repository/files</code></pre>
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param ref (required) - The name of branch, tag or commit
 * @return an Optional instance with the specified RepositoryFile as a value
 * @since GitLab-11.1.0
 */
public Optional<RepositoryFile> getOptionalFileInfo(Object projectIdOrPath, String filePath, String ref) {
    try {
        return (Optional.ofNullable(getFileInfo(projectIdOrPath, filePath, ref)));
    } catch (GitLabApiException glae) {
        return (GitLabApi.createOptionalFromException(glae));
    }
}
 
Example #25
Source File: RepositoryFileApi.java    From choerodon-starters with Apache License 2.0 3 votes vote down vote up
/**
 * Update existing file in repository
 * <p>
 * PUT /projects/:id/repository/files
 * <p>
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param file          a ReposityoryFile instance with info for the file to update
 * @param projectId     the project ID
 * @param branchName    the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the updated file info
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
    Form formData = file2form(file, branchName, commitMessage);
    Response response;
    if (isApiVersion(ApiVersion.V3)) {
        response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files");
    } else {
        response = put(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
    }

    return (response.readEntity(RepositoryFile.class));
}
 
Example #26
Source File: RepositoryFileApi.java    From choerodon-starters with Apache License 2.0 3 votes vote down vote up
/**
 * Create new file in repository
 * <p>
 * POST /projects/:id/repository/files
 * <p>
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param file          a ReposityoryFile instance with info for the file to create
 * @param projectId     the project ID
 * @param branchName    the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the created file info
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
    Form formData = file2form(file, branchName, commitMessage);
    Response response;
    if (isApiVersion(ApiVersion.V3)) {
        response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files");
    } else {
        response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
    }

    return (response.readEntity(RepositoryFile.class));
}
 
Example #27
Source File: RepositoryFileApi.java    From choerodon-starters with Apache License 2.0 3 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, content.
 * Note that file content is Base64 encoded.
 * <p>
 * GET /projects/:id/repository/files
 *
 * @param filePath  (required) - Full path to new file. Ex. lib/class.rb
 * @param projectId (required) - the project ID
 * @param ref       (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info
 * @throws GitLabApiException if any exception occurs
 */
protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException {
    Form form = new Form();
    addFormParam(form, "file_path", filePath, true);
    addFormParam(form, "ref", ref, true);
    Response response = get(Response.Status.OK, form.asMap(), "projects", projectId, "repository", "files");
    return (response.readEntity(RepositoryFile.class));
}
 
Example #28
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Update existing file in repository
 *
 * <pre><code>GitLab Endpoint: PUT /projects/:id/repository/files</code></pre>
 *
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param file a ReposityoryFile instance with info for the file to update
 * @param projectId the project ID
 * @param branchName the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the updated file info
 * @throws GitLabApiException if any exception occurs
 * @deprecated  Will be removed in version 5.0, replaced by {@link #updateFile(Object, RepositoryFile, String, String)}
 */
@Deprecated
public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
    return (updateFile(projectId, file, branchName, commitMessage));
}
 
Example #29
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Create new file in repository
 *
 * <pre><code>GitLab Endpoint: POST /projects/:id/repository/files</code></pre>
 *
 * file_path (required) - Full path to new file. Ex. lib/class.rb
 * branch_name (required) - The name of branch
 * encoding (optional) - 'text' or 'base64'. Text is default.
 * content (required) - File content
 * commit_message (required) - Commit message
 *
 * @param file a ReposityoryFile instance with info for the file to create
 * @param projectId the project ID
 * @param branchName the name of branch
 * @param commitMessage the commit message
 * @return a RepositoryFile instance with the created file info
 * @throws GitLabApiException if any exception occurs
 * @deprecated  Will be removed in version 5.0, replaced by {@link #createFile(Object, RepositoryFile, String, String)}
 */
@Deprecated
public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
    return (createFile(projectId, file, branchName, commitMessage));
}
 
Example #30
Source File: RepositoryFileApi.java    From gitlab4j-api with MIT License 2 votes vote down vote up
/**
 * Get file from repository. Allows you to receive information about file in repository like name, size, content.
 * Note that file content is Base64 encoded.
 *
 * <pre><code>GitLab Endpoint: GET /projects/:id/repository/files</code></pre>
 *
 * @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
 * @param filePath (required) - Full path to the file. Ex. lib/class.rb
 * @param ref (required) - The name of branch, tag or commit
 * @return a RepositoryFile instance with the file info and file content
 * @throws GitLabApiException if any exception occurs
 */
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {
    return (getFile(projectIdOrPath, filePath, ref, true));
}