Skip to content

Commit

Permalink
Update jbang test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jmini committed Nov 21, 2024
1 parent a09c0b7 commit 89a3632
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 33 deletions.
44 changes: 30 additions & 14 deletions gitlab4j-test/FileUploadScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS https://github.com/gitlab4j/gitlab4j-api/commit/5805b41bae64b6c287abdbd51f63ce84d1ec8b90
//JAVA 17

import java.io.FileInputStream;
Expand Down Expand Up @@ -31,6 +31,9 @@ class FileUploadScript implements Callable<Integer> {
@Option(names = { "-p", "--project" }, description = "project (id or path)")
private String project;

@Option(names = { "-g", "--group" }, description = "group")
private String group;

@Option(names = { "-f", "--file" }, description = "Path to the file to uplaod")
private String filePath;

Expand All @@ -40,6 +43,9 @@ class FileUploadScript implements Callable<Integer> {
@Option(names = { "-c", "--config" }, description = "configuration file location")
String configFile;

@Option(names = { "-v", "--verbose" }, description = "log http trafic")
Boolean logHttp;

@Override
public Integer call() throws Exception {
Path file;
Expand All @@ -59,30 +65,40 @@ public Integer call() throws Exception {
throw new IllegalStateException("Can not find file: " + fileToUpload.toAbsolutePath());
}

try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
projectIdMandatory();
// FileUpload uplaodedFile = gitLabApi.getProjectApi()
// .uploadFile(idOrPath(projectId), fileToUpload.toFile());
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
if (filename == null) {
filename = fileToUpload.getFileName()
.toString();
}
FileUpload uplaodedFile = gitLabApi.getProjectApi()
.uploadFile(idOrPath(project), Files.newInputStream(fileToUpload), filename, null);
System.out.println(uplaodedFile);
if(project == null && group == null) {
throw new IllegalStateException("Project or group is mandatory");
} else if(project != null && group != null) {
throw new IllegalStateException("Project and group can't be set at the same time");
} else if(project != null) {
FileUpload uplaodedFile = gitLabApi.getProjectApi()
.uploadFile(idOrPath(project), Files.newInputStream(fileToUpload), filename, null);
System.out.println(uplaodedFile);
} else if(group != null) {
FileUpload uplaodedFile = gitLabApi.getGroupApi()
.uploadFile(idOrPath(group), Files.newInputStream(fileToUpload), filename, null);
System.out.println(uplaodedFile);
}
}
return 0;
}

private void filePathMandatory() {
if (filePath == null) {
throw new IllegalStateException("File is mandatory");
private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
if (logHttp != null && logHttp) {
return new GitLabApi(gitLabUrl, gitLabAuthValue)
.withRequestResponseLogging(java.util.logging.Level.INFO) ;
}
return new GitLabApi(gitLabUrl, gitLabAuthValue);
}

private void projectIdMandatory() {
if (project == null) {
throw new IllegalStateException("Project is mandatory");

private void filePathMandatory() {
if (filePath == null) {
throw new IllegalStateException("File is mandatory");
}
}

Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/GitLabCiYamlScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.File;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/GitLabScript.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.util.logging.Level;
Expand Down
34 changes: 29 additions & 5 deletions gitlab4j-test/GroupScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS https://github.com/simon-weimann/gitlab4j-api/commit/7842c72bad980f2e582b649a91fce326c7dba7e1
//DEPS https://github.com/gitlab4j/gitlab4j-api/commit/5805b41bae64b6c287abdbd51f63ce84d1ec8b90
//JAVA 17

import java.io.FileInputStream;
Expand Down Expand Up @@ -39,8 +39,11 @@ public class GroupScript implements Callable<Integer> {
@Option(names = { "-c", "--config" }, description = "configuration file location")
String configFile;

@Option(names = { "-v", "--verbose" }, description = "log http trafic")
Boolean logHttp;

private static enum Action {
GET_GROUP
GET_GROUP, GET_GROUP_LABELS, GET_GROUP_UPLOADS
}

@Override
Expand All @@ -57,13 +60,25 @@ public Integer call() throws Exception {
final String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");


try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
switch (action) {
case GET_GROUP:
ensureExists(group, "group");
var result = gitLabApi.getGroupApi()
var groupResponse = gitLabApi.getGroupApi()
.getGroup(idOrPath(group));
System.out.println(result);
System.out.println(groupResponse);
break;
case GET_GROUP_LABELS:
ensureExists(group, "group");
var labelsResponse = gitLabApi.getLabelsApi()
.getGroupLabels(idOrPath(group));
System.out.println(labelsResponse);
break;
case GET_GROUP_UPLOADS:
ensureExists(group, "group");
var uploadsResponse = gitLabApi.getGroupApi()
.getUploadFiles(idOrPath(group));
System.out.println(uploadsResponse);
break;
default:
throw new IllegalArgumentException("Unexpected value: " + action);
Expand All @@ -72,6 +87,15 @@ public Integer call() throws Exception {
return 0;
}

private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
if (logHttp != null && logHttp) {
return new GitLabApi(gitLabUrl, gitLabAuthValue)
.withRequestResponseLogging(java.util.logging.Level.INFO) ;
}
return new GitLabApi(gitLabUrl, gitLabAuthValue);
}


private void ensureExists(Object value, String optionName) {
if (value == null) {
throw new IllegalStateException("--" + optionName + " must be set");
Expand Down
4 changes: 2 additions & 2 deletions gitlab4j-test/IssueScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS https://github.com/jmini/gitlab4j-api/commit/c3981f425828088f6d8f25d1435b5c2953fc1a35
//DEPS https://github.com/jmini/gitlab4j-api/commit/436fb4edadb6b72641dbf836ee4fdb792ac0ab32
//JAVA 17

import java.io.FileInputStream;
Expand Down Expand Up @@ -99,7 +99,7 @@ public Integer call() throws Exception {
case GET_LINKED_ISSUES:
ensureExists(project, "project");
ensureExists(issueIid, "issue");
List<Issue> linkedIssue = gitLabApi.getIssuesApi()
var linkedIssue = gitLabApi.getIssuesApi()
.getIssueLinks(idOrPath(project), issueIid);
System.out.println(linkedIssue);
break;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/IterationScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.FileInputStream;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/JobScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.*;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/LabelScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.FileInputStream;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/MetadataScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.File;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/NoteScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.FileInputStream;
Expand Down
16 changes: 14 additions & 2 deletions gitlab4j-test/ProjectsScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.File;
Expand All @@ -14,6 +14,7 @@
import java.util.List;
import java.util.Properties;
import java.util.concurrent.Callable;
import java.util.logging.Level;

import org.gitlab4j.api.*;
import org.gitlab4j.api.models.*;
Expand Down Expand Up @@ -52,6 +53,9 @@ class ProjectsScript implements Callable<Integer> {
@Option(names = { "-c", "--config" }, description = "configuration file location")
String configFile;

@Option(names = { "-v", "--verbose" }, description = "log http trafic")
Boolean logHttp;

private static enum Action {
PRINT_PROJECT, PRINT_PROJECTS, CREATE_PROJECT, UPDATE_PROJECT, DELETE_PROJECT
}
Expand All @@ -69,7 +73,7 @@ public Integer call() throws Exception {
String gitLabUrl = readProperty(prop, "GITLAB_URL", "https://gitlab.com");
String gitLabAuthValue = readProperty(prop, "GITLAB_AUTH_VALUE");

try (GitLabApi gitLabApi = new GitLabApi(gitLabUrl, gitLabAuthValue)) {
try (GitLabApi gitLabApi = createGitLabApi(gitLabUrl, gitLabAuthValue)) {
System.out.println("Action " + action + " ...");

Project project;
Expand Down Expand Up @@ -120,6 +124,14 @@ public Integer call() throws Exception {
return 0;
}

private GitLabApi createGitLabApi(String gitLabUrl, String gitLabAuthValue) {
if (logHttp != null && logHttp) {
return new GitLabApi(gitLabUrl, gitLabAuthValue)
.withRequestResponseLogging(java.util.logging.Level.INFO);
}
return new GitLabApi(gitLabUrl, gitLabAuthValue);
}

private void projectIdMandatory() {
if (projectId == null) {
throw new IllegalStateException("Project id is mandatory");
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/ReleaseScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.FileInputStream;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/SearchScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.FileInputStream;
Expand Down
2 changes: 1 addition & 1 deletion gitlab4j-test/TopicsScript.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS info.picocli:picocli:4.6.3
//DEPS org.gitlab4j:gitlab4j-api:5.4.0
//DEPS org.gitlab4j:gitlab4j-api:5.6.0
//JAVA 17

import java.io.File;
Expand Down
8 changes: 8 additions & 0 deletions gitlab4j-test/command.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ With:

File to include `*.java`

--------

Build all with jbang:

```
find . -name '*.java' -exec jbang build {} \;
```

==========================================

Have a look at the PR https://github.com/gitlab4j/gitlab4j-api/pull/0000 (feel free to review it and give feedback)
Expand Down

0 comments on commit 89a3632

Please sign in to comment.