Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@kevinsawicki kevinsawicki commented Aug 2, 2011

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