Skip to content

Instantly share code, notes, and snippets.

@kevinsawicki
Created August 2, 2011 23:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinsawicki/1121516 to your computer and use it in GitHub Desktop.
Save kevinsawicki/1121516 to your computer and use it in GitHub Desktop.
Add downloads to a GitHub repository
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.eclipse.egit.github.core.Download;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.DownloadService;
/**
* Uploads a file to be made available as a repository download
*/
public class UploadFile {
public static void main(String... args) throws IOException {
GitHubClient client = new GitHubClient();
client.setCredentials("user", "secret");
// Owner and name of repository such as 'rails/rails'
RepositoryId repository = new RepositoryId("user", "project");
DownloadService service = new DownloadService(client);
File file = File.createTempFile("download", ".txt");
PrintWriter writer = new PrintWriter(file);
writer.println("Content of the file");
writer.close();
Download download = new Download();
download.setDescription("Attaching some file");
download.setName(file.getName());
download.setSize(file.length());
service.createDownload(repository, download, file);
}
}
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.eclipse.egit.github.core.Download;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.DownloadService;
/**
* Uploads a String to be made available as a repository download
*/
public class UploadStream {
public static void main(String... args) throws IOException {
GitHubClient client = new GitHubClient();
client.setCredentials("user", "secret");
// Owner and name of repository such as 'rails/rails'
RepositoryId repository = new RepositoryId("user", "project");
DownloadService service = new DownloadService(client);
byte[] content = "download content".getBytes("UTF-8");
Download download = new Download();
download.setDescription("Attaching some text");
download.setName("simple.txt");
download.setSize(content.length);
ByteArrayInputStream stream = new ByteArrayInputStream(content);
service.createDownload(repository, download, stream, content.length);
}
}
@kevinsawicki
Copy link
Author

Documentation for the downloads API is available here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment