Search in sources :

Example 1 with FileContent

use of org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent in project intellij-community by JetBrains.

the class GithubCreateGistAction method getContentFromFile.

@NotNull
private static List<FileContent> getContentFromFile(@NotNull final VirtualFile file, @NotNull Project project, @Nullable String prefix) {
    if (file.isDirectory()) {
        return getContentFromDirectory(file, project, prefix);
    }
    if (file.getFileType().isBinary()) {
        GithubNotifications.showWarning(project, FAILED_TO_CREATE_GIST, "Can't upload binary file: " + file);
        return Collections.emptyList();
    }
    String content = ReadAction.compute(() -> {
        try {
            Document document = FileDocumentManager.getInstance().getDocument(file);
            if (document != null) {
                return document.getText();
            } else {
                return new String(file.contentsToByteArray(), file.getCharset());
            }
        } catch (IOException e) {
            LOG.info("Couldn't read contents of the file " + file, e);
            return null;
        }
    });
    if (content == null) {
        GithubNotifications.showWarning(project, FAILED_TO_CREATE_GIST, "Couldn't read the contents of the file " + file);
        return Collections.emptyList();
    }
    if (StringUtil.isEmptyOrSpaces(content)) {
        return Collections.emptyList();
    }
    String filename = addPrefix(file.getName(), prefix, false);
    return Collections.singletonList(new FileContent(filename, content));
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with FileContent

use of org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent in project intellij-community by JetBrains.

the class GithubCreateGistContentTest method testCreateFromEmptyDirectory.

public void testCreateFromEmptyDirectory() throws Throwable {
    List<FileContent> expected = new ArrayList<>();
    VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_folder");
    assertNotNull(file);
    List<FileContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
    checkEquals(expected, actual);
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList)

Example 3 with FileContent

use of org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent in project intellij-community by JetBrains.

the class GithubCreateGistContentTest method testCreateFromEmptyFile.

public void testCreateFromEmptyFile() throws Throwable {
    List<FileContent> expected = new ArrayList<>();
    VirtualFile file = myProjectRoot.findFileByRelativePath("folder/empty_file");
    assertNotNull(file);
    List<FileContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
    checkEquals(expected, actual);
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList)

Example 4 with FileContent

use of org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent in project intellij-community by JetBrains.

the class GithubCreateGistContentTest method testCreateFromDirectory.

public void testCreateFromDirectory() throws Throwable {
    List<FileContent> expected = new ArrayList<>();
    expected.add(new FileContent("folder_file1", "file1 content"));
    expected.add(new FileContent("folder_file2", "file2 content"));
    expected.add(new FileContent("folder_dir_file3", "file3 content"));
    VirtualFile file = myProjectRoot.findFileByRelativePath("folder");
    assertNotNull(file);
    List<FileContent> actual = GithubCreateGistAction.collectContents(myProject, null, file, null);
    checkEquals(expected, actual);
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList)

Example 5 with FileContent

use of org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent in project intellij-community by JetBrains.

the class GithubCreateGistTest method testAnonymous.

public void testAnonymous() throws Throwable {
    List<FileContent> expected = createContent();
    String url = GithubCreateGistAction.createGist(myProject, new GithubAuthDataHolder(GithubAuthData.createAnonymous(myHost)), myIndicator, expected, true, GIST_DESCRIPTION, null);
    assertNotNull(url);
    GIST_ID = url.substring(url.lastIndexOf('/') + 1);
    checkGistExists();
    checkGistAnonymous();
    checkGistPrivate();
    checkGistDescription(GIST_DESCRIPTION);
    checkGistContent(expected);
    // anonymous gists - undeletable
    GIST_ID = null;
    GIST = null;
}
Also used : FileContent(org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent) GithubAuthDataHolder(org.jetbrains.plugins.github.util.GithubAuthDataHolder)

Aggregations

FileContent (org.jetbrains.plugins.github.api.requests.GithubGistRequest.FileContent)14 ArrayList (java.util.ArrayList)11 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Document (com.intellij.openapi.editor.Document)4 NotNull (org.jetbrains.annotations.NotNull)4 IOException (java.io.IOException)2 BrowserUtil (com.intellij.ide.BrowserUtil)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 CommonDataKeys (com.intellij.openapi.actionSystem.CommonDataKeys)1 ReadAction (com.intellij.openapi.application.ReadAction)1 Logger (com.intellij.openapi.diagnostic.Logger)1 Editor (com.intellij.openapi.editor.Editor)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileTypeManager (com.intellij.openapi.fileTypes.FileTypeManager)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 DumbAwareAction (com.intellij.openapi.project.DumbAwareAction)1 Project (com.intellij.openapi.project.Project)1 Ref (com.intellij.openapi.util.Ref)1 StringUtil (com.intellij.openapi.util.text.StringUtil)1