try {
var branchName = "BRANCH-" + random.nextLong();
var action = new CommitAction();
action.withContent(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data))
.withFilePath(JSON_FILE_PATH).setAction(Action.UPDATE);
try (var glapi = new GitLabApi(GIT_HOSTNAME, token)) {
glapi.enableRequestResponseLogging(java.util.logging.Level.INFO);
glapi.getCommitsApi().createCommit(REPO_NAME, branchName, message, VERSION, null, null, action);
return glapi.getMergeRequestApi().createMergeRequest(DB_REPO_NAME, branchName, VERSION, message,
message, ADMIN_USER_ID);
}
} catch (Exception ex) {
throw new DataException(ex);
}
如何创建一个新的存储库、一个新的主分支和第一次提交:
try (var glapi = new GitLabApi("https://gitlab.com/", token)) {
var projectApi = glapi.getProjectApi();
var project = projectApi.createProject("my-repo");
// my-repo created
var action = new CommitAction();
action.withContent("### ignore some files ###").withFilePath(".gitignore").setAction(Action.CREATE);
glapi.getCommitsApi().createCommit("my-repo", "master", "my first commit", "master", "author@mail.com",
"author", action);
// yoru first commit
}
// to use basic auth
var credentials = new UsernamePasswordCredentialsProvider("user", "password");
// to clone a repository
Git.cloneRepository().setURI("https://github.com/my-repo.git").setCredentialsProvider(credentials).call();
// to init a new repository
Git.init().setDirectory(new File("/opt/my-repo")).setInitialBranch("master").call();
// to use a cloned repo
var git = Git.open(new File("/opt/my-repo"));
// to pull changes from remote
git.pull().setCredentialsProvider(credentials).call();
// to stage one file
git.add().setUpdate(true).addFilepattern("dir/file.txt").call();
// to stage all files in a directory
git.add().setUpdate(true).addFilepattern("dir").call();
// to create a commit with the staged files
git.commit().setMessage("just adding some files.").call();
// to push changes to remote
git.push().call();
3条答案
按热度按时间2izufjch1#
您可以使用GitHub Rest API。
从
Settings > Developers Settings > Personal Access Tokens
生成个人访问令牌一旦生成,使用它调用端点-
带主体
和标题
curl 示例:
参考文件:https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-a-repository-for-the-authenticated-user
x6h2sr282#
gitlab4j API是一个很好的库:https://github.com/gitlab4j/gitlab4j-api
我正在使用从repo读取文件、提交和创建合并请求:
如何创建一个新的存储库、一个新的主分支和第一次提交:
对于github,您可以使用https://github.com/hub4j/github-api
提交多个文件:
使用Eclipse jgit
示例:
v440hwme3#
我尝试使用jgit向GitHub repo添加文件。Commit正在创建,但文件没有添加,在addfilepattern中-我也尝试了.“”。下面是我的代码片段: