Skip to content

Commit 7fb9b9b

Browse files
upload project to github repository
0 parents  commit 7fb9b9b

6 files changed

Lines changed: 367 additions & 0 deletions

File tree

pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.3.4.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.francescodisales</groupId>
12+
<artifactId>githubterminal</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>githubterminal</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>14</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>org.junit.vintage</groupId>
34+
<artifactId>junit-vintage-engine</artifactId>
35+
</exclusion>
36+
</exclusions>
37+
</dependency>
38+
39+
<!-- https://mvnrepository.com/artifact/org.springframework.shell/spring-shell-starter -->
40+
<dependency>
41+
<groupId>org.springframework.shell</groupId>
42+
<artifactId>spring-shell-starter</artifactId>
43+
<version>2.0.1.RELEASE</version>
44+
</dependency>
45+
46+
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
47+
<dependency>
48+
<groupId>org.jsoup</groupId>
49+
<artifactId>jsoup</artifactId>
50+
<version>1.13.1</version>
51+
</dependency>
52+
53+
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
54+
<dependency>
55+
<groupId>com.googlecode.json-simple</groupId>
56+
<artifactId>json-simple</artifactId>
57+
<version>1.1.1</version>
58+
</dependency>
59+
60+
61+
</dependencies>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.francescodisalesdev.gitcli;
2+
3+
import org.json.simple.parser.ParseException;
4+
import org.springframework.shell.standard.ShellComponent;
5+
import org.springframework.shell.standard.ShellMethod;
6+
import com.francescodisalesdev.gitcli.service.GitService;
7+
8+
import java.io.IOException;
9+
import java.util.List;
10+
11+
12+
@ShellComponent
13+
class GitCli
14+
{
15+
16+
@ShellMethod("search for a git repository")
17+
public void query(String repository,int page)
18+
{
19+
try
20+
{
21+
GitService gitService = new GitService();
22+
List<String> result = gitService.getResult(repository,page);
23+
24+
if(result==null)
25+
System.out.println("page number must be bigger than 0");
26+
else
27+
gitService.filterPage(result);
28+
29+
}
30+
catch(IOException | ParseException e)
31+
{
32+
System.out.println("something bad happened while processing the request");
33+
e.printStackTrace();
34+
}
35+
36+
}
37+
38+
@ShellMethod("counts the total record of your research")
39+
public void getPages(String repository)
40+
{
41+
GitService gitService = new GitService();
42+
43+
try
44+
{
45+
gitService.getPages(repository);
46+
}
47+
catch(IOException e)
48+
{
49+
System.out.println("something bad happened while processing the request");
50+
}
51+
}
52+
53+
@ShellMethod("clone the repository given an url")
54+
public void clone(String repository,String localPath)
55+
{
56+
GitService gitService = new GitService();
57+
58+
try
59+
{
60+
gitService.cloneRepository(repository,localPath);
61+
}
62+
catch(IOException | InterruptedException e)
63+
{
64+
System.out.println("something bad happened");
65+
}
66+
67+
}
68+
69+
@ShellMethod("clone the repository and the relative branch")
70+
public void cloneBranch(String repository,String localPath,String branch)
71+
{
72+
GitService gitService = new GitService();
73+
74+
try
75+
{
76+
gitService.cloneRepositoryBranch(repository, localPath, branch);
77+
}
78+
catch(InterruptedException | IOException e)
79+
{
80+
System.out.println("Something bad happened");
81+
}
82+
83+
}
84+
85+
@ShellMethod("see a list of files in a repository")
86+
public void info(String repository)
87+
{
88+
GitService gitService = new GitService();
89+
90+
try
91+
{
92+
gitService.getInfoRepository(repository);
93+
}
94+
catch (IOException e)
95+
{
96+
System.out.println("Something bad happened");
97+
}
98+
99+
}
100+
101+
102+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.francescodisalesdev.gitcli;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class GithubterminalApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(GithubterminalApplication.class, args);
11+
}
12+
13+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.francescodisalesdev.gitcli.service;
2+
3+
import com.francescodisalesdev.gitcli.utility.ConditionChecker;
4+
import org.json.simple.JSONObject;
5+
import org.json.simple.parser.JSONParser;
6+
import org.json.simple.parser.ParseException;
7+
8+
import org.jsoup.nodes.Document;
9+
import org.jsoup.nodes.Element;
10+
import org.jsoup.select.Elements;
11+
import org.jsoup.Jsoup;
12+
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.util.*;
17+
18+
19+
public class GitService
20+
{
21+
22+
public List<String> getResult(String param,int page) throws IOException, ParseException
23+
{
24+
25+
String URL;
26+
27+
if(page <0)
28+
return null;
29+
30+
if(page == 1)
31+
URL="https://github.com/search?q="+param;
32+
else
33+
URL="https://github.com/search?p="+page+"&q="+param+"&type=Repositories";
34+
35+
Document document = Jsoup.connect(URL).get();
36+
37+
List<String> repo = new ArrayList<String>();
38+
Elements elementsLinks = document.select("a");
39+
40+
for(Element link : elementsLinks)
41+
{
42+
String repositoryJSON = link.attr("data-hydro-click").toString();
43+
JSONParser jsonParser = new JSONParser();
44+
45+
if(!repositoryJSON.isBlank())
46+
{
47+
JSONObject jsonObject = (JSONObject) jsonParser.parse(repositoryJSON);
48+
49+
String payload = jsonObject.get("payload").toString();
50+
JSONObject result = (JSONObject) jsonParser.parse(payload);
51+
52+
if(result.containsKey("result"))
53+
{
54+
String resultString = result.get("result").toString();
55+
JSONObject three = (JSONObject) jsonParser.parse(resultString);
56+
57+
String urlString = three.get("url").toString();
58+
repo.add(urlString);
59+
}
60+
61+
}
62+
63+
}
64+
65+
return repo;
66+
}
67+
68+
public void getPages(String param) throws IOException
69+
{
70+
String URL = "https://github.com/search?q="+param;
71+
Document document = Jsoup.connect(URL).get();
72+
Elements elementsLinks = document.select("em");
73+
74+
System.out.println(elementsLinks.attr("data-total-pages").toString());
75+
76+
}
77+
78+
public void filterPage(List<String> response)
79+
{
80+
81+
Iterator responseIterator = response.iterator();
82+
83+
while(responseIterator.hasNext())
84+
{
85+
System.out.println(responseIterator.next());
86+
}
87+
88+
}
89+
90+
public void cloneRepository(String repository,String localPath) throws IOException, InterruptedException
91+
{
92+
String gitRepository = repository+".git";
93+
94+
ProcessBuilder processBuilder = new ProcessBuilder("git","clone",gitRepository);
95+
processBuilder.directory(new File(localPath));
96+
Process process = processBuilder.start();
97+
98+
System.out.println("cloning ...");
99+
process.waitFor();
100+
101+
System.out.println("cloning done!");
102+
103+
}
104+
105+
public void cloneRepositoryBranch(String repository,String localPath,String branch) throws IOException,InterruptedException
106+
{
107+
String gitRepository = repository+".git";
108+
109+
ProcessBuilder processBuilder = new ProcessBuilder("git","clone","-b",branch,gitRepository);
110+
processBuilder.directory(new File(localPath));
111+
Process process = processBuilder.start();
112+
113+
System.out.println("cloning ...");
114+
process.waitFor();
115+
116+
System.out.println("cloning done!");
117+
118+
119+
}
120+
121+
public void getInfoRepository(String repository) throws IOException
122+
{
123+
Document document = Jsoup.connect(repository).get();
124+
125+
Elements elements = document.select("a");
126+
List<String> info = new ArrayList<String>();
127+
128+
ConditionChecker conditionChecker = new ConditionChecker();
129+
130+
for(Element element : elements)
131+
{
132+
if(conditionChecker.ConditionParserInfoA(element) && conditionChecker.ConditionParserInfoB(element))
133+
info.add(element.attr("href"));
134+
135+
}
136+
137+
Iterator iterator = info.iterator();
138+
139+
while(iterator.hasNext())
140+
System.out.println(iterator.next());
141+
142+
}
143+
144+
145+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.francescodisalesdev.gitcli.utility;
2+
3+
import org.jsoup.nodes.Element;
4+
5+
public class ConditionChecker
6+
{
7+
public boolean ConditionParserInfoA(Element element)
8+
{
9+
if(!element.attr("href").isEmpty() && !element.attr("href").contains("https://") && !element.attr("href").contains("http://"))
10+
return true;
11+
12+
return false;
13+
}
14+
15+
public boolean ConditionParserInfoB(Element element)
16+
{
17+
if(!element.attr("href").contains("mailto") && !element.attr("href").contains("#") && element.attr("href").contains("/tree/"))
18+
return true;
19+
20+
return false;
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.francescodisalesdev.gitcli;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class GithubterminalApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)