使用java客户端将文件添加到GitHub - org.eclipse.egit.github.core

时间:2017-09-14 09:13:20

标签: github egit

我正在尝试使用以下代码将文件添加到存储库但我收到以下错误。我现在只想添加一个文件。

  

org.eclipse.egit.github.core.client.RequestException:请求无效。

     

对于' properties / email',nil不是字符串。对于'属性/名称',   nil不是一个字符串。对于' properties / email',nil不是字符串。对于   ' properties / name',nil不是字符串。 (422)at   org.eclipse.egit.github.core.client.GitHubClient.createException(GitHubClient.java:552)     在   org.eclipse.egit.github.core.client.GitHubClient.sendJson(GitHubClient.java:643)     在   org.eclipse.egit.github.core.client.GitHubClient.post(GitHubClient.java:757)     在   org.eclipse.egit.github.core.service.DataService.createCommit(DataService.java:397)

我觉得它期待一些属性,但如何提供这一点尚不清楚。我错过了什么?

参考https://gist.github.com/Detelca/2337731

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.CommitUser;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.TypedResource;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;

public class GHWriter {

public static void main(String[] args) {
    try {
        new GHWriter().writeFile("test_two.txt", "test content");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//https://gist.github.com/Detelca/2337731
public boolean writeFile(String fileName, String fileContent) throws IOException{
    // initialize github client
    GitHubClient client = new GitHubClient();
    //TextView password = (TextView)findViewById(R.id.textViewPassword);
    client.setCredentials("username", "password");

    // create needed services
    RepositoryService repositoryService = new RepositoryService();
    CommitService commitService = new CommitService(client);
    DataService dataService = new DataService(client);

    // get some sha's from current state in git
    Repository repository =  repositoryService.getRepository("username", "repositoryName");
    String baseCommitSha = repositoryService.getBranches(repository).get(0).getCommit().getSha();
    RepositoryCommit baseCommit = commitService.getCommit(repository, baseCommitSha);
    String treeSha = baseCommit.getSha();

    // create new blob with data
    Blob blob = new Blob();
        blob.setContent("[\"" + System.currentTimeMillis() + "\"]").setEncoding(Blob.ENCODING_UTF8);
    String blob_sha = dataService.createBlob(repository, blob);
    Tree baseTree = dataService.getTree(repository, treeSha);

    // create new tree entry
    TreeEntry treeEntry = new TreeEntry();
    treeEntry.setPath("testfile.txt");
    treeEntry.setMode(TreeEntry.MODE_BLOB);
    treeEntry.setType(TreeEntry.TYPE_BLOB);
    treeEntry.setSha(blob_sha);
    treeEntry.setSize(blob.getContent().length());
    Collection<TreeEntry> entries = new ArrayList<TreeEntry>();
    entries.add(treeEntry);
    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.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;    
}

}

编辑(已解决) :经过一些调试后,发现电子邮件和名称值为空,这是问题的根源。

添加以下两行将解决问题:

   author.setName( userName );
   author.setEmail(email);

由于

1 个答案:

答案 0 :(得分:0)

经过调试后,发现电子邮件和名称值以null开头,这是问题的根源。

添加以下两行将解决此问题:

author.setName( userName );
author.setEmail(email);