How to create/upload a zip file in a GITHUB repository by using Java git client (org.eclipse.egit.github.core)

1.5k Views Asked by At

I have a requirement to upload zip file (zip files consists multiple xml files) on github repository. This action is to accomplished by a spring boot application. Currently I am using org.eclipse.mylyn.github (org.eclipse.egit.github.core) and by using egit library I am able to create and commit individual text/csv/etc. files but when I push a zip file it create as text file. and it appears like this image

The manual upload looks like

Below is the code which I'm using. I would like to thank in advance for the help. Kindly let me know what i'm missing here.

 public boolean mappingPushToGitHub(String path,byte[] zipDataInByte, String repo)
            throws IOException, KeyManagementException, NoSuchAlgorithmException, SerialException {

        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
        SSLContext.setDefault(ctx);

        // create needed services
        RepositoryService repositoryService = new RepositoryService(client);
        CommitService commitService = new CommitService(client);
        
        DataService dataService = new DataService(client);
        //ContentsService contentsService = new ContentsService(client);
         
        // get some sha's from current state in git
        Repository repository = repositoryService.getRepository("Applications", repo);
        String baseCommitSha = repositoryService.getBranches(repository).get(0).getCommit().getSha();
        RepositoryCommit baseCommit = commitService.getCommit(repository, baseCommitSha);
        String treeSha = baseCommit.getSha();

        Blob blob =  new Blob();        
        blob.setContent(zipDataInByte.toString());
        
        String blob_sha = dataService.createBlob(repository, blob);
        Tree baseTree = dataService.getTree(repository, treeSha);
        
        TreeEntry treeEntry = new TreeEntry();
        treeEntry.setPath(path);
        treeEntry.setMode(TreeEntry.MODE_BLOB);
        treeEntry.setType(TreeEntry.TYPE_BLOB);
        treeEntry.setSha(blob_sha);
        treeEntry.setSize(blob.getContent().length());
        
        //treeEntry.setUrl(subUrl);
        Collection<TreeEntry> entries = new ArrayList<TreeEntry>();
        entries.add(treeEntry);
        // repository.setUrl("https://github.wdf.sap.corp/DTApplications/DTAutomation/tree/master/AFSPEObjectsCSVFile");
        Tree newTree = dataService.createTree(repository, entries, baseTree.getSha());

        // create commit
        Commit commit = new Commit();
        commit.setMessage("first commit at " + new Date(System.currentTimeMillis()).toLocaleString());
        commit.setTree(newTree);
         
        UserService userService = new UserService(client);
        User user = userService.getUser();
        CommitUser author = new CommitUser();
        author.setName(user.getName());
        Calendar now = Calendar.getInstance();
        author.setName("test");
        author.setEmail("[email protected]");
        author.setDate(now.getTime());
        commit.setAuthor(author);
        commit.setCommitter(author);

        List<Commit> listOfCommits = new ArrayList<Commit>();
        listOfCommits.add(new Commit().setSha(baseCommitSha));
        // listOfCommits.containsAll(base_commit.getParents());
        commit.setParents(listOfCommits);       
        // commit.setSha(base_commit.getSha());
        Commit newCommit = dataService.createCommit(repository, commit);

        // create resource
        TypedResource commitResource = new TypedResource();
        commitResource.setSha(newCommit.getSha());
        commitResource.setType(TypedResource.TYPE_COMMIT);
        
        commitResource.setUrl(newCommit.getUrl());

        // get master reference and update it
        Reference reference = dataService.getReference(repository, "heads/master");
        reference.setObject(commitResource);
        dataService.editReference(repository, reference, true);
        System.out.println("Committed URL: " + newCommit.getUrl());
        return false;
    }
0

There are 0 best solutions below