You are viewing a plain text version of this content. The canonical link for it is here.
Posted to submarine-dev@hadoop.apache.org by zh...@apache.org on 2019/10/25 03:37:58 UTC
[hadoop-submarine] branch master updated: SUBMARINE-236. Add JGit
Utils class
This is an automated email from the ASF dual-hosted git repository.
zhouquan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hadoop-submarine.git
The following commit(s) were added to refs/heads/master by this push:
new db8998b SUBMARINE-236. Add JGit Utils class
db8998b is described below
commit db8998bd70ed908d3b9ea3d54210db03aa37c5f4
Author: LinhaoZhu <zh...@163.com>
AuthorDate: Fri Oct 25 10:51:52 2019 +0800
SUBMARINE-236. Add JGit Utils class
### What is this PR for?
Set up version control for notebooks using GitHub through the workbench.
Here is the background code implementation of the demo, including some of the main git operations.
Implementation depends on the org.eclipse.jgit library,and with reference to the lot developer documentation: https://developer.github.com/v3/
### What type of PR is it?
[ Feature ]
### Todos
* [ ] - Task
### What is the Jira issue?
* [SUBMARINE-236](https://issues.apache.org/jira/browse/SUBMARINE-236)
### How should this be tested?
* [CI Pass](https://travis-ci.org/LinhaoZhu/hadoop-submarine/builds/602115461)
### Screenshots (if appropriate)
### Questions:
* Does the licenses files need update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Author: LinhaoZhu <zh...@163.com>
Closes #51 from LinhaoZhu/SUBMARINE-236 and squashes the following commits:
c95e95e [LinhaoZhu] [SUBMARINE-236] Move the GitUtils from org.apache.submarine.server to org.apache.submarine.database.utils.
e962306 [LinhaoZhu] [SUBMARINE-236] Add random value for BRANCHNAME and LOCALPATH. Delete token print. Rename WorkbenchGitHubServer to GithubUtils.
9e51169 [LinhaoZhu] [SUBMARINE-236] remove test token.
d93c9f8 [LinhaoZhu] [SUBMARINE-236] Comment has incorrect indentation level 0, expected is 4, indentation should be the same leve
84ae08e [LinhaoZhu] Merge branch 'master' of https://github.com/apache/hadoop-submarine into SUBMARINE-236
479119f [LinhaoZhu] [SUBMARINE-236] Fix bugs and add test cases
85703eb [LinhaoZhu] [SUBMARINE-236] Test delete watching
10a22b1 [LinhaoZhu] [SUBMARINE-236] Remove export GIT_TOKEN="${gitToken}"
8d4065c [LinhaoZhu] [SUBMARINE-236] export GIT_TOKEN="${gitToken}"
75f1318 [LinhaoZhu] [SUBMARINE-236] Add after_script.
4106014 [LinhaoZhu] [SUBMARINE-236] File does not end with a newline.
bfc8d70 [LinhaoZhu] [SUBMARINE-236] export GIT_TOKEN
9f8f314 [LinhaoZhu] [SUBMARINE-236] Remove Unused import.
7c123d7 [LinhaoZhu] [SUBMARINE-236] Configure git token
bbffa57 [LinhaoZhu] [SUBMARINE-236] remove main method.
4f19e5d [LinhaoZhu] Merge branch 'SUBMARINE-236' of https://github.com/LinhaoZhu/hadoop-submarine into SUBMARINE-236
ef19e8a [LinhaoZhu] Merge branch 'SUBMARINE-236' of https://github.com/LinhaoZhu/hadoop-submarine into SUBMARINE-236
7ede3ce [LinhaoZhu] [SUBMARINE-236] update token
b7c2a26 [LinhaoZhu] [SUBMARINE-236] JGit demo
dfb8708 [LinhaoZhu] [SUBMARINE-236] update token
b6e06f3 [LinhaoZhu] [SUBMARINE-236] JGit demo
---
pom.xml | 1 +
submarine-workbench/workbench-server/pom.xml | 11 +
.../apache/submarine/database/utils/GitUtils.java | 322 +++++++++++++++++++++
.../submarine/database/utils/HttpRequestUtil.java | 113 ++++++++
.../org/apache/submarine/server/GitUtilsTest.java | 257 ++++++++++++++++
5 files changed, 704 insertions(+)
diff --git a/pom.xml b/pom.xml
index 70c31fa..1ad57f4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -112,6 +112,7 @@
<mybatis-generator.version>1.3.7</mybatis-generator.version>
<derby.version>10.15.1.3</derby.version>
<zeppelin.version>0.9.0-SNAPSHOT</zeppelin.version>
+ <jgit.version>5.5.1.201910021850-r</jgit.version>
<atomix.version>3.0.0-rc4</atomix.version>
<libthrift.version>0.12.0</libthrift.version>
</properties>
diff --git a/submarine-workbench/workbench-server/pom.xml b/submarine-workbench/workbench-server/pom.xml
index 4d2ee67..264f25e 100644
--- a/submarine-workbench/workbench-server/pom.xml
+++ b/submarine-workbench/workbench-server/pom.xml
@@ -214,6 +214,17 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.eclipse.jgit</groupId>
+ <artifactId>org.eclipse.jgit</artifactId>
+ <version>${jgit.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
<build>
diff --git a/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/GitUtils.java b/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/GitUtils.java
new file mode 100644
index 0000000..0c0c58a
--- /dev/null
+++ b/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/GitUtils.java
@@ -0,0 +1,322 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.submarine.database.utils;
+
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.ListBranchCommand;
+import org.eclipse.jgit.api.PullResult;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.transport.CredentialsProvider;
+import org.eclipse.jgit.transport.PushResult;
+import org.eclipse.jgit.transport.URIish;
+import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.List;
+
+public class GitUtils {
+ private static final Logger LOG = LoggerFactory.getLogger(GitUtils.class);
+
+ /**
+ * To execute clone command
+ * @param remotePath
+ * @param localPath
+ * @param token
+ * @param branch
+ * @throws GitAPIException
+ */
+ public void clone(String remotePath, String localPath, String token, String branch) {
+ // Clone the code base command
+ // Sets the token on the remote server
+ CredentialsProvider credentialsProvider =
+ new UsernamePasswordCredentialsProvider("PRIVATE-TOKEN", token);
+
+ Git git = null;
+ try {
+ git = Git.cloneRepository().setURI(remotePath) // Set remote URI
+ .setBranch(branch) // Set the branch down from clone
+ .setDirectory(new File(localPath)) // Set the download path
+ .setCredentialsProvider(credentialsProvider) // Set permission validation
+ .call();
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ } finally {
+ if (git != null) {
+ git.close();
+ }
+ }
+ LOG.info("git.tag(): {}", git.tag());
+ }
+
+ /**
+ * To execute add command
+ * @param localPath
+ * @param fileName
+ * relative path
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public DirCache add(String localPath, String fileName) {
+ // Git repository address
+ File myfile = new File(localPath + fileName);
+ if (!myfile.exists()) {
+ myfile.getParentFile().mkdirs();
+ try {
+ myfile.createNewFile();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+ DirCache dirCache = null;
+ try (Git git = Git.open(new File(localPath))) {
+ // Add files
+ dirCache = git.add().addFilepattern(fileName.substring(1)).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return dirCache;
+ }
+
+ /**
+ * To execute rm command
+ * @param localPath
+ * @param fileName
+ * relative path
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public DirCache rm(String localPath, String fileName) {
+ DirCache dirCache = null;
+ // Git repository address
+ try (Git git = Git.open(new File(localPath))) {
+ // rm files
+ dirCache = git.rm().addFilepattern(fileName.substring(1)).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return dirCache;
+ }
+
+
+ /**
+ * To execute reset command
+ * @param localPath
+ * @param fileName
+ * relative path
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public void reset(String localPath, String fileName) {
+ // Git repository address
+ try (Git git = Git.open(new File(localPath))) {
+ // reset files
+ git.reset().addPath(fileName.substring(1)).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * To execute pull command
+ * @param localPath
+ * @param token
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public PullResult pull(String localPath, String token, String branch) {
+ CredentialsProvider credentialsProvider =
+ new UsernamePasswordCredentialsProvider("PRIVATE-TOKEN", token);
+ PullResult pullResult = null;
+ // Git repository address
+ try (Git git = Git.open(new File(localPath))) {
+ pullResult = git.pull().setRemoteBranchName(branch).
+ setCredentialsProvider(credentialsProvider).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return pullResult;
+ }
+
+ /**
+ * To execute commit command
+ * @param localPath
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public RevCommit commit(String localPath, String message) {
+ RevCommit revCommit = null;
+ // Git repository address
+ try (Git git = Git.open(new File(localPath))) {
+ // Submit code
+ revCommit = git.commit().setMessage(message).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return revCommit;
+ }
+
+ /**
+ * To execute push command
+ * @param localPath
+ * @param token
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public Iterable<PushResult> push(String localPath, String token, String remote) {
+ CredentialsProvider credentialsProvider =
+ new UsernamePasswordCredentialsProvider("PRIVATE-TOKEN", token);
+ Iterable<PushResult> iterable = null;
+ // Git repository address
+ try (Git git = Git.open(new File(localPath))) {
+ iterable = git.push().setRemote(remote).setCredentialsProvider(credentialsProvider).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return iterable;
+ }
+
+ /**
+ * To execute branchCreate command
+ * @param localPath
+ * @param branchName
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public Ref branchCreate(String localPath, String branchName) {
+ Ref result = null;
+ try (Git git = Git.open(new File(localPath))) {
+ ListBranchCommand listBranchCommand = git.branchList();
+ List<Ref> list = listBranchCommand.call();
+ boolean existsBranch = false;
+ for (Ref ref : list) {
+ if (ref.getName().endsWith(branchName)) {
+ existsBranch = true;
+ break;
+ }
+ }
+ if (!existsBranch) {
+ // Create branch
+ result = git.branchCreate().setName(branchName).call();
+ } else {
+ LOG.warn("{} already exists.", branchName);
+ }
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return result;
+ }
+
+ /**
+ * To execute branchDelete command
+ * @param localPath
+ * @param branchName
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public List<String> branchDelete(String localPath, String branchName) {
+ List<String> list = null;
+ try (Git git = Git.open(new File(localPath))) {
+ list = git.branchDelete().setForce(true).setBranchNames(branchName).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return list;
+ }
+
+ /**
+ * To execute checkout command
+ * @param localPath
+ * @param branchName
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public Ref checkout(String localPath, String branchName) {
+ Ref result = null;
+ try (Git git = Git.open(new File(localPath))) {
+ result = git.checkout().setName(branchName).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ return result;
+ }
+
+ /**
+ * To execute rebase command
+ * @param localPath
+ * @param branchName
+ * @param upstreamName
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public void rebase(String localPath, String branchName, String upstreamName) {
+ try (Git git = Git.open(new File(localPath))) {
+ git.rebase().setUpstream(branchName).setUpstreamName(upstreamName).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * To execute remoteAdd command
+ * @param localPath
+ * @param uri
+ * @param remoteName
+ * @throws IOException
+ * @throws GitAPIException
+ */
+ public void remoteAdd(String localPath, String uri, String remoteName) {
+ try (Git git = Git.open(new File(localPath))) {
+ URIish urIish = new URIish(uri);
+ git.remoteAdd().setName(remoteName).setUri(urIish).call();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (GitAPIException e) {
+ LOG.error(e.getMessage(), e);
+ } catch (URISyntaxException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+}
diff --git a/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/HttpRequestUtil.java b/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/HttpRequestUtil.java
new file mode 100644
index 0000000..fb392c4
--- /dev/null
+++ b/submarine-workbench/workbench-server/src/main/java/org/apache/submarine/database/utils/HttpRequestUtil.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.submarine.database.utils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Date;
+import java.util.Map;
+
+public class HttpRequestUtil {
+ private static final Logger LOG = LoggerFactory.getLogger(HttpRequestUtil.class);
+
+ /**
+ * Sends an HTTP request to the specified URL
+ * @param url
+ * @param parameter
+ * @param method
+ * @return
+ * @throws Exception
+ */
+ public static String sendHttpRequest(String url, Map<String, String> headParams,
+ byte[] parameter, String method) {
+ // Create a URLConnection
+ URLConnection urlConnection = null;
+ try {
+ urlConnection = (new URL(url)).openConnection();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ // Defines StringBuilder to facilitate string concatenation later
+ // when reading web pages and returning byte stream information
+ StringBuilder stringBuilder = new StringBuilder();
+
+ // Force URLConnection class to HttpURLConnection class
+ HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
+ httpURLConnection.setDoInput(true);
+ httpURLConnection.setDoOutput(true);
+ // Set the request, which could be delete put post get
+ try {
+ httpURLConnection.setRequestMethod(method);
+ } catch (ProtocolException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ if (parameter != null) {
+ // Sets the length of the content
+ httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameter.length));
+ }
+ // Set encoding format
+ httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
+ // Sets the format of the receive return parameter
+ httpURLConnection.setRequestProperty("accept", "application/json");
+ httpURLConnection.setRequestProperty("connection", "Keep-Alive");
+ httpURLConnection.setRequestProperty("user-agent",
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+ // Timestamp (accurate to seconds)
+ httpURLConnection.setRequestProperty("timestamp", String.valueOf(new Date().getTime() / 1000));
+
+ if (headParams != null) {
+ for (String key : headParams.keySet()) {
+ httpURLConnection.setRequestProperty(key, headParams.get(key));
+ }
+ }
+ httpURLConnection.setUseCaches(false);
+
+ if (parameter != null) {
+ try (BufferedOutputStream outputStream =
+ new BufferedOutputStream(httpURLConnection.getOutputStream())) {
+ outputStream.write(parameter);
+ outputStream.flush();
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+ try (InputStreamReader inputStreamReader =
+ new InputStreamReader(httpURLConnection.getInputStream(), "utf-8");
+ BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
+ String line;
+ while ((line = bufferedReader.readLine()) != null) {
+ stringBuilder.append(line);
+ }
+ } catch (IOException e) {
+ LOG.error(e.getMessage(), e);
+ }
+
+ LOG.info("result:{}", stringBuilder.toString());
+ return stringBuilder.toString();
+ }
+}
diff --git a/submarine-workbench/workbench-server/src/test/java/org/apache/submarine/server/GitUtilsTest.java b/submarine-workbench/workbench-server/src/test/java/org/apache/submarine/server/GitUtilsTest.java
new file mode 100644
index 0000000..f2026de
--- /dev/null
+++ b/submarine-workbench/workbench-server/src/test/java/org/apache/submarine/server/GitUtilsTest.java
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.submarine.server;
+
+import com.google.common.collect.Lists;
+import com.google.gson.Gson;
+import org.apache.submarine.database.utils.GitUtils;
+import org.eclipse.jgit.api.PullResult;
+import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.transport.PushResult;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.apache.submarine.database.utils.HttpRequestUtil.sendHttpRequest;
+
+public class GitUtilsTest {
+ private static final Logger LOG = LoggerFactory.getLogger(GitUtilsTest.class);
+
+ private static final String OWNER = "submarine-thirdparty";
+ private static final String REPO = "submarine_git_test";
+ GitUtils gitUtils = new GitUtils();
+ private static final String REMOTE_PATH = "https://github.com/" + OWNER + "/" + REPO + ".git";
+ private static final long TIME = new Date().getTime();
+ private static final String BRANCHNAME = "Branch-" + TIME;
+ private static final String LOCALPATH =
+ GitUtilsTest.class.getResource("").toString().substring(6) + REPO + TIME;
+
+ private static final String token = System.getenv("gitToken");
+
+ @After
+ public void restoreAllOperations() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ Map<String, String> map = new HashMap<>();
+ map.put("Authorization", "token " + token);
+ sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO + "/subscription",
+ map, null, "DELETE");
+ sendHttpRequest("https://api.github.com/user/starred/" + OWNER + "/" + REPO + "",
+ map, null, "DELETE");
+
+ String result = sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/branches", map, null, "GET");
+ Gson gson = new Gson();
+ List<Map<String, String>> list = gson.fromJson(result, List.class);
+ for (Map<String, String> stringStringMap : list) {
+ if (BRANCHNAME.equals(stringStringMap.get("name"))) {
+ sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/git/refs/heads/" + BRANCHNAME, map, null, "DELETE");
+ }
+ }
+ LOG.info("LOCALPATH: {}", LOCALPATH);
+ deleteDirectory(LOCALPATH);
+ }
+ }
+
+ public void deleteDirectory(String dir) {
+ File dirFile = new File(dir);
+ File[] files = dirFile.listFiles();
+ if (files != null) {
+ for (File file : files) {
+ if (file.isFile()) {
+ new File(file.getAbsolutePath()).delete();
+ } else if (file.isDirectory()) {
+ deleteDirectory(file.getAbsolutePath());
+ }
+ }
+ dirFile.delete();
+ }
+ }
+
+ @Test
+ public void addWatching() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ Map<String, String> map = new HashMap<>();
+ map.put("Authorization", "token " + token);
+ sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO + "/subscription",
+ map, null, "PUT");
+
+ String result = sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/subscribers", null, null, "GET");
+ LOG.info("Watching result: {}", result);
+
+ Gson gson = new Gson();
+ List<Map<String, String>> list = gson.fromJson(result, List.class);
+ assertEquals(1, list.size());
+ String login = list.get(0).get("login");
+ LOG.info("login: {}", login);
+ assertEquals(OWNER, login);
+ }
+ }
+
+ @Test
+ public void deleteWatching() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ Map<String, String> map = new HashMap<>();
+ map.put("Authorization", "token " + token);
+ sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO + "/subscription",
+ map, null, "PUT");
+
+ sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO + "/subscription",
+ map, null, "DELETE");
+
+ String result = sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/subscribers", null, null, "GET");
+ LOG.info("result: {}", result);
+
+ Gson gson = new Gson();
+ List<Map<String, String>> list = gson.fromJson(result, List.class);
+ assertEquals(0, list.size());
+ }
+ }
+
+ @Test
+ public void addStarring() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ Map<String, String> map = new HashMap<>();
+ map.put("Authorization", "token " + token);
+ sendHttpRequest("https://api.github.com/user/starred/" + OWNER + "/" + REPO + "",
+ map, null, "PUT");
+
+ String result = sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/stargazers", null, null, "GET");
+ LOG.info("Starring result: {}", result);
+
+ Gson gson = new Gson();
+ List<Map<String, String>> list = gson.fromJson(result, List.class);
+ assertEquals(1, list.size());
+ String login = list.get(0).get("login");
+ LOG.info("login: {}", login);
+ assertEquals(OWNER, login);
+ }
+ }
+
+ @Test
+ public void deleteStarring() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ Map<String, String> map = new HashMap<>();
+ map.put("Authorization", "token " + token);
+ sendHttpRequest("https://api.github.com/user/starred/" + OWNER + "/" + REPO + "",
+ map, null, "PUT");
+
+ sendHttpRequest("https://api.github.com/user/starred/" + OWNER + "/" + REPO + "",
+ map, null, "DELETE");
+
+ String result = sendHttpRequest("https://api.github.com/repos/" + OWNER + "/" + REPO
+ + "/stargazers", null, null, "GET");
+ LOG.info("result: {}", result);
+
+ Gson gson = new Gson();
+ List<Map<String, String>> list = gson.fromJson(result, List.class);
+ assertEquals(0, list.size());
+ }
+ }
+
+ @Before
+ public void cloneTest() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ gitUtils.clone(REMOTE_PATH, LOCALPATH, token, "master");
+ File dirFile = new File(LOCALPATH);
+ File[] files = dirFile.listFiles();
+ assertEquals(1, files.length);
+ assertEquals(".git", files[0].getName());
+ }
+ }
+
+ @Test
+ public void addAndRest() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ DirCache dirCache = gitUtils.add(LOCALPATH, "/aa/bb/log4j.properties");
+ assertEquals(1, dirCache.getEntryCount());
+
+ gitUtils.reset(LOCALPATH, "/aa/bb/log4j.properties");
+ }
+ }
+
+ @Test
+ public void pull() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ gitUtils.add(LOCALPATH, "/log4j.properties");
+ gitUtils.commit(LOCALPATH, "add new file.");
+ Iterable<PushResult> iterable = gitUtils.push(LOCALPATH, token, REMOTE_PATH);
+ assertEquals(1, Lists.newArrayList(iterable).size());
+
+ PullResult pullResult = gitUtils.pull(LOCALPATH, token, "master");
+ assertEquals(1, pullResult.getFetchResult().getTrackingRefUpdates().size());
+
+ gitUtils.rm(LOCALPATH, "/log4j.properties");
+ gitUtils.commit(LOCALPATH, "add new file.");
+ gitUtils.push(LOCALPATH, token, REMOTE_PATH);
+ }
+ }
+
+ @Test
+ public void branchCreateAndCheckout() {
+ if (token == null) {
+ LOG.warn("Token not set!");
+ return;
+ } else {
+ gitUtils.commit(LOCALPATH, "add new file.");
+ Ref ref = gitUtils.branchCreate(LOCALPATH, BRANCHNAME);
+ assertEquals(true, ref.getName().endsWith(BRANCHNAME));
+
+ ref = gitUtils.checkout(LOCALPATH, BRANCHNAME);
+ assertEquals(true, ref.getName().endsWith(BRANCHNAME));
+ }
+ }
+}