Java Examples for org.gitlab.api.GitlabAPI
The following java examples will help you to understand the usage of org.gitlab.api.GitlabAPI. These source code samples are taken from different open source projects.
Example 1
| Project: gitlab-api-master File: GitlabHTTPRequestor.java View source code |
private void submitAttachments(HttpURLConnection connection) throws IOException {
// Just generate some unique random value.
String boundary = Long.toHexString(System.currentTimeMillis());
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
String charset = "UTF-8";
// Line separator required by multipart/form-data.
String CRLF = "\r\n";
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
try {
for (Map.Entry<String, Object> paramEntry : data.entrySet()) {
String paramName = paramEntry.getKey();
String param = GitlabAPI.MAPPER.writeValueAsString(paramEntry.getValue());
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + paramName + "\"").append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append(param).append(CRLF).flush();
}
for (Map.Entry<String, File> attachMentEntry : attachments.entrySet()) {
File binaryFile = attachMentEntry.getValue();
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + attachMentEntry.getKey() + "\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(binaryFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Reader fileReader = new FileReader(binaryFile);
try {
IOUtils.copy(fileReader, output);
} finally {
fileReader.close();
}
// Important before continuing with writer!
output.flush();
// CRLF is important! It indicates end of boundary.
writer.append(CRLF).flush();
}
writer.append("--" + boundary + "--").append(CRLF).flush();
} finally {
writer.close();
}
}Example 2
| Project: jenkins-gitlab-security-plugin-master File: GitLabSecurityRealm.java View source code |
@Override
protected UserDetails authenticate(String username, String password) throws AuthenticationException {
try {
LOGGER.info("Trying to authenticate with username: " + username);
GitlabSession session = GitlabAPI.connect(this.gitLabUrl, username, password);
return this.userDetailsBuilder.buildUserDetails(this.gitLabUrl, session, session.getPrivateToken());
} catch (Exception e) {
this.LOGGER.log(Level.WARNING, "Authentication request failed for username: " + username, e);
throw new AuthenticationServiceException("Unable to process authentication for username: " + username, e);
}
}Example 3
| Project: mylyn-gitlab-master File: GitlabTaskDataHandler.java View source code |
@Override
public RepositoryResponse postTaskData(TaskRepository repository, TaskData data, Set<TaskAttribute> attributes, IProgressMonitor monitor) throws CoreException {
GitlabAttributeMapper attributeMapper = (GitlabAttributeMapper) data.getAttributeMapper();
TaskAttribute root = data.getRoot();
String labels = root.getAttribute(GitlabAttribute.LABELS.getTaskKey()).getValue();
String title = root.getAttribute(GitlabAttribute.TITLE.getTaskKey()).getValue();
String body = root.getAttribute(GitlabAttribute.BODY.getTaskKey()).getValue();
Integer assigneeId = 0;
// assignee
for (TaskAttribute a : attributes) {
if (a.getId().equals(GitlabAttribute.ASSIGNEE.getTaskKey())) {
GitlabProjectMember assignee = attributeMapper.findProjectMemberByName(root.getAttribute(GitlabAttribute.ASSIGNEE.getTaskKey()).getValue());
assigneeId = (assignee == null ? -1 : assignee.getId());
}
}
GitlabMilestone milestone = attributeMapper.findMilestoneByName(root.getAttribute(GitlabAttribute.MILESTONE.getTaskKey()).getValue());
Integer milestoneId = (milestone == null ? 0 : milestone.getId());
GitlabConnection connection = ConnectionManager.get(repository);
GitlabAPI api = connection.api();
try {
monitor.beginTask("Uploading task", IProgressMonitor.UNKNOWN);
GitlabIssue issue = null;
if (data.isNew()) {
issue = api.createIssue(connection.project.getId(), assigneeId, milestoneId, labels, body, title);
return new RepositoryResponse(ResponseKind.TASK_CREATED, "" + issue.getId());
} else {
if (root.getAttribute(TaskAttribute.COMMENT_NEW) != null && !root.getAttribute(TaskAttribute.COMMENT_NEW).getValue().equals("")) {
api.createNote(connection.project.getId(), GitlabConnector.getTicketId(data.getTaskId()), root.getAttribute(TaskAttribute.COMMENT_NEW).getValue());
}
String action = root.getAttribute(TaskAttribute.OPERATION).getValue();
issue = api.editIssue(connection.project.getId(), GitlabConnector.getTicketId(data.getTaskId()), assigneeId, milestoneId, labels, body, title, GitlabAction.find(action).getGitlabIssueAction());
return new RepositoryResponse(ResponseKind.TASK_UPDATED, "" + issue.getId());
}
} catch (IOException e) {
throw new GitlabException("Unknown connection error!");
} finally {
monitor.done();
}
}Example 4
| Project: git-as-svn-master File: GitLabMappingConfig.java View source code |
@NotNull
@Override
public VcsRepositoryMapping create(@NotNull SharedContext context) throws IOException, SVNException {
final GitLabContext gitlab = context.sure(GitLabContext.class);
final GitlabAPI api = gitlab.connect();
// Get repositories.
final GitLabMapping mapping = new GitLabMapping(context, this);
for (GitlabProject project : api.getAllProjects()) {
mapping.addRepository(project);
}
// Web hook for repository list update.
final WebServer webServer = WebServer.get(context);
final URL hookUrl = new URL(gitlab.getHookUrl());
webServer.addServlet(hookUrl.getPath(), new GitLabHookServlet(mapping));
if (!isHookInstalled(api, hookUrl.toString())) {
api.addSystemHook(hookUrl.toString());
}
return mapping;
}Example 5
| Project: jenkins-gitlab-merge-request-sonar-plugin-master File: Gitlab.java View source code |
public static GitlabAPI get() { if (API == null) { String privateToken = GitlabSonarReporter.DESCRIPTOR.getBotApiToken(); String apiUrl = GitlabSonarReporter.DESCRIPTOR.getGitlabHostUrl(); API = GitlabAPI.connect(apiUrl, privateToken); } return API; }