org.gitlab4j.api.GitLabApi Java Examples
The following examples show how to use
org.gitlab4j.api.GitLabApi.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitLabOwner.java From gitlab-branch-source-plugin with MIT License | 6 votes |
@NonNull public static GitLabOwner fetchOwner(GitLabApi gitLabApi, String projectOwner) { try { Group group = gitLabApi.getGroupApi().getGroup(projectOwner); return new GitLabGroup(group.getName(), group.getWebUrl(), group.getAvatarUrl(), group.getId(), group.getFullName(), group.getDescription()); } catch (GitLabApiException e) { if (e.getHttpStatus() != 404) { throw new IllegalStateException("Unable to fetch Group", e); } try { User user = gitLabApi.getUserApi().getUser(projectOwner); // If user is not found, null is returned if (user == null) { throw new IllegalStateException( String.format("Owner '%s' is neither a user/group/subgroup", projectOwner)); } return new GitLabUser(user.getName(), user.getWebUrl(), user.getAvatarUrl(), user.getId()); } catch (GitLabApiException e1) { throw new IllegalStateException("Unable to fetch User", e1); } } }
Example #2
Source File: GitLabSCMFileSystem.java From gitlab-branch-source-plugin with MIT License | 6 votes |
protected GitLabSCMFileSystem( GitLabApi gitLabApi, String projectPath, String ref, @CheckForNull SCMRevision rev) throws IOException { super(rev); this.gitLabApi = gitLabApi; this.projectPath = projectPath; if (rev != null) { if (rev.getHead() instanceof MergeRequestSCMHead) { this.ref = ((MergeRequestSCMRevision) rev).getOrigin().getHash(); } else if (rev instanceof BranchSCMRevision) { this.ref = ((BranchSCMRevision) rev).getHash(); } else if (rev instanceof GitTagSCMRevision) { this.ref = ((GitTagSCMRevision) rev).getHash(); } else { this.ref = ref; } } else { this.ref = ref; } }
Example #3
Source File: GitLabHookCreator.java From gitlab-branch-source-plugin with MIT License | 6 votes |
public static String createWebHookWhenMissing(GitLabApi gitLabApi, String project, String hookUrl, String secretToken) throws GitLabApiException { ProjectHook projectHook = gitLabApi.getProjectApi().getHooksStream(project) .filter(hook -> hookUrl.equals(hook.getUrl())) .findFirst() .orElseGet(GitLabHookCreator::createWebHook); if (projectHook.getId() == null) { gitLabApi.getProjectApi().addHook(project, hookUrl, projectHook, false, secretToken); return "created"; } // Primarily done due to legacy reason, secret token might not be configured in previous releases. So setting up hook url with the token. if(!isTokenEqual(projectHook.getToken(), secretToken)) { projectHook.setToken(secretToken); gitLabApi.getProjectApi().modifyHook(projectHook); return "modified"; } return "already created"; }
Example #4
Source File: GitLabHookCreator.java From gitlab-branch-source-plugin with MIT License | 6 votes |
public static void createSystemHookWhenMissing(GitLabServer server, PersonalAccessToken credentials) { String systemHookUrl = getHookUrl(server, false); try { GitLabApi gitLabApi = new GitLabApi(server.getServerUrl(), credentials.getToken().getPlainText()); SystemHook systemHook = gitLabApi.getSystemHooksApi() .getSystemHookStream() .filter(hook -> systemHookUrl.equals(hook.getUrl())) .findFirst() .orElse(null); if (systemHook == null) { gitLabApi.getSystemHooksApi().addSystemHook(systemHookUrl, server.getSecretToken().getPlainText(), false, false, false); } } catch (GitLabApiException e) { LOGGER.log(Level.INFO, "User is not admin so cannot set system hooks", e); } }
Example #5
Source File: GitLabSCMSource.java From gitlab-branch-source-plugin with MIT License | 6 votes |
public int getProjectId(@QueryParameter String projectPath, @QueryParameter String serverName) { List<GitLabServer> gitLabServers = GitLabServers.get().getServers(); if (gitLabServers.size() == 0) { return -1; } try { GitLabApi gitLabApi; if (StringUtils.isBlank(serverName)) { gitLabApi = apiBuilder(gitLabServers.get(0).getName()); } else { gitLabApi = apiBuilder(serverName); } if (StringUtils.isNotBlank(projectPath)) { return gitLabApi.getProjectApi().getProject(projectPath).getId(); } } catch (GitLabApiException e) { return -1; } return -1; }
Example #6
Source File: GitLabHelper.java From gitlab-branch-source-plugin with MIT License | 5 votes |
public static GitLabApi apiBuilder(String serverName) { GitLabServer server = GitLabServers.get().findServer(serverName); if (server != null) { PersonalAccessToken credentials = server.getCredentials(); if (credentials != null) { return new GitLabApi(server.getServerUrl(), credentials.getToken().getPlainText()); } return new GitLabApi(server.getServerUrl(), GitLabServer.EMPTY_TOKEN); } throw new IllegalStateException( String.format("No server found with the name: %s", serverName)); }
Example #7
Source File: GitLabPipelineStatusNotifier.java From gitlab-branch-source-plugin with MIT License | 5 votes |
/** * Log comment on Commits and Merge Requests upon build complete. */ private static void logComment(Run<?, ?> build, TaskListener listener) { GitLabSCMSource source = getSource(build); if (source == null) { return; } final GitLabSCMSourceContext sourceContext = getSourceContext(build, source); if (!sourceContext.logCommentEnabled()) { return; } String url = getRootUrl(build); if (url.isEmpty()) { listener.getLogger().println( "Can not determine Jenkins root URL. Comments are disabled until a root URL is" + " configured in Jenkins global configuration."); return; } Result result = build.getResult(); LOGGER.log(Level.FINE, String.format("Log Comment Result: %s", result)); String note = ""; String symbol = ""; if (Result.SUCCESS.equals(result)) { if (!sourceContext.doLogSuccess()) { return; } symbol = ":heavy_check_mark: "; note = "The Jenkins CI build passed "; } else if (Result.UNSTABLE.equals(result)) { symbol = ":heavy_multiplication_x: "; note = "The Jenkins CI build failed "; } else if (Result.FAILURE.equals(result)) { symbol = ":heavy_multiplication_x: "; note = "The Jenkins CI build failed "; } else if (result != null) { // ABORTED, NOT_BUILT. symbol = ":no_entry_sign: "; note = "The Jenkins CI build aborted "; } String suffix = " - [Details](" + url + ")"; SCMRevision revision = SCMRevisionAction.getRevision(source, build); try { GitLabApi gitLabApi = GitLabHelper.apiBuilder(source.getServerName()); String sudoUsername = sourceContext.getSudoUser(); if (!sudoUsername.isEmpty()) { gitLabApi.sudo(sudoUsername); } final String buildName = "**" + getStatusName(sourceContext, build, revision) + ":** "; final String hash; if (revision instanceof BranchSCMRevision) { hash = ((BranchSCMRevision) revision).getHash(); gitLabApi.getCommitsApi().addComment( source.getProjectPath(), hash, symbol + buildName + note + suffix ); } else if (revision instanceof MergeRequestSCMRevision) { MergeRequestSCMHead head = (MergeRequestSCMHead) revision.getHead(); gitLabApi.getNotesApi().createMergeRequestNote( source.getProjectPath(), Integer.valueOf(head.getId()), symbol + buildName + note + suffix ); } else if (revision instanceof GitTagSCMRevision) { hash = ((GitTagSCMRevision) revision).getHash(); gitLabApi.getCommitsApi().addComment( source.getProjectPath(), hash, symbol + buildName + note + suffix ); } } catch (GitLabApiException e) { LOGGER.log(Level.WARNING, "Exception caught:" + e, e); } }
Example #8
Source File: GitLabSCMNavigator.java From gitlab-branch-source-plugin with MIT License | 5 votes |
public static FormValidation doCheckProjectOwner(@QueryParameter String projectOwner, @QueryParameter String serverName) { if (projectOwner.equals("")) { return FormValidation.ok(); } GitLabApi gitLabApi = null; try { gitLabApi = apiBuilder(serverName); GitLabOwner gitLabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner); return FormValidation.ok(projectOwner + " is a valid " + gitLabOwner.getWord()); } catch (IllegalStateException e) { return FormValidation.error(e, e.getMessage()); } }
Example #9
Source File: GitLabSCMFileSystem.java From gitlab-branch-source-plugin with MIT License | 5 votes |
@Override public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @CheckForNull SCMRevision rev) throws IOException, InterruptedException { GitLabSCMSource gitlabScmSource = (GitLabSCMSource) source; GitLabApi gitLabApi = apiBuilder(gitlabScmSource.getServerName()); String projectPath = gitlabScmSource.getProjectPath(); return build(head, rev, gitLabApi, projectPath); }
Example #10
Source File: GitLabSCMFileSystem.java From gitlab-branch-source-plugin with MIT License | 5 votes |
public SCMFileSystem build(@NonNull SCMHead head, @CheckForNull SCMRevision rev, @NonNull GitLabApi gitLabApi, @NonNull String projectPath) throws IOException, InterruptedException { String ref; if (head instanceof MergeRequestSCMHead) { ref = ((MergeRequestSCMHead) head).getOriginName(); } else if (head instanceof BranchSCMHead) { ref = head.getName(); } else if (head instanceof GitLabTagSCMHead) { ref = head.getName(); } else { return null; } return new GitLabSCMFileSystem(gitLabApi, projectPath, ref, rev); }
Example #11
Source File: GitLabSCMFile.java From gitlab-branch-source-plugin with MIT License | 5 votes |
public GitLabSCMFile(GitLabApi gitLabApi, String projectPath, String ref) { super(); this.gitLabApi = gitLabApi; this.isDir = true; type(Type.DIRECTORY); this.projectPath = projectPath; this.ref = ref; }
Example #12
Source File: GitLabSCMSource.java From gitlab-branch-source-plugin with MIT License | 5 votes |
protected Project getGitlabProject(GitLabApi gitLabApi) { if (gitlabProject == null) { try { gitlabProject = gitLabApi.getProjectApi().getProject(projectPath); sshRemote = gitlabProject.getSshUrlToRepo(); httpRemote = gitlabProject.getHttpUrlToRepo(); projectId = gitlabProject.getId(); } catch (GitLabApiException e) { throw new IllegalStateException("Failed to retrieve project " + projectPath, e); } } return gitlabProject; }
Example #13
Source File: GitLabSCMSource.java From gitlab-branch-source-plugin with MIT License | 5 votes |
public HashMap<String, AccessLevel> getMembers() { HashMap<String, AccessLevel> members = new HashMap<>(); try { GitLabApi gitLabApi = apiBuilder(serverName); for (Member m : gitLabApi.getProjectApi().getAllMembers(projectPath)) { members.put(m.getUsername(), m.getAccessLevel()); } } catch (GitLabApiException e) { LOGGER.log(Level.WARNING, "Exception while fetching members" + e, e); return new HashMap<>(); } return members; }
Example #14
Source File: GitUtils.java From swagger-showdoc with Apache License 2.0 | 4 votes |
@Autowired public void setGitLabApi(GitLabApi gitLabApi){ GitUtils.gitLabApi = gitLabApi; }
Example #15
Source File: GitlabConfig.java From swagger-showdoc with Apache License 2.0 | 4 votes |
@Bean public GitLabApi gitLabApi (){ return new GitLabApi("http://121.40.242.195", "Hgyedp4XZYV7Bppx3sR3"); }
Example #16
Source File: GitLabHookCreator.java From gitlab-branch-source-plugin with MIT License | 4 votes |
public static void register(GitLabSCMSource source, GitLabHookRegistration webhookMode, GitLabHookRegistration systemhookMode) { PersonalAccessToken credentials = null; GitLabServer server = GitLabServers.get().findServer(source.getServerName()); if (server == null) { return; } switch (webhookMode) { case DISABLE: break; case SYSTEM: if (!server.isManageWebHooks()) { break; } credentials = server.getCredentials(); if (credentials == null) { LOGGER.log(Level.WARNING, "No System credentials added, cannot create web hook"); } break; case ITEM: credentials = source.credentials(); if (credentials == null) { LOGGER.log(Level.WARNING, "No Item credentials added, cannot create web hook"); } break; default: return; } String hookUrl = getHookUrl(server, true); String secretToken = server.getSecretToken().getPlainText(); if (hookUrl.equals("")) { return; } if (credentials != null) { try { GitLabApi gitLabApi = new GitLabApi(server.getServerUrl(), credentials.getToken().getPlainText()); createWebHookWhenMissing(gitLabApi, source.getProjectPath(), hookUrl, secretToken); } catch (GitLabApiException e) { LOGGER.log(Level.WARNING, "Could not manage project hooks for " + source.getProjectPath() + " on " + server.getServerUrl(), e); } } switch (systemhookMode) { case DISABLE: return; case SYSTEM: if (!server.isManageSystemHooks()) { return; } credentials = server.getCredentials(); if (credentials == null) { LOGGER.log(Level.WARNING, "No System credentials added, cannot create system hook"); } break; case ITEM: credentials = source.credentials(); if (credentials == null) { LOGGER.log(Level.WARNING, "No Item credentials added, cannot create system hook"); } break; default: return; } // add system hooks if (credentials != null) { createSystemHookWhenMissing(server, credentials); } }
Example #17
Source File: GitLabSCMNavigator.java From gitlab-branch-source-plugin with MIT License | 4 votes |
private GitLabOwner getGitlabOwner(GitLabApi gitLabApi) { if (gitlabOwner == null) { gitlabOwner = GitLabOwner.fetchOwner(gitLabApi, projectOwner); } return gitlabOwner; }
Example #18
Source File: GitLabPipelineStatusNotifier.java From gitlab-branch-source-plugin with MIT License | 4 votes |
/** * Sends notifications to GitLab on Checkout (for the "In Progress" Status). */ private static void sendNotifications(Run<?, ?> build, TaskListener listener) { GitLabSCMSource source = getSource(build); if (source == null) { return; } final GitLabSCMSourceContext sourceContext = getSourceContext(build, source); if (sourceContext.notificationsDisabled()) { return; } String url = getRootUrl(build); if (url.isEmpty()) { listener.getLogger().println( "Can not determine Jenkins root URL. Commit status notifications are disabled until a root URL is" + " configured in Jenkins global configuration."); return; } Result result = build.getResult(); LOGGER.log(Level.FINE, String.format("Result: %s", result)); CommitStatus status = new CommitStatus(); Constants.CommitBuildState state; status.setTargetUrl(url); if (Result.SUCCESS.equals(result)) { status.setDescription(build.getParent().getFullName() + ": This commit looks good"); status.setStatus("SUCCESS"); state = Constants.CommitBuildState.SUCCESS; } else if (Result.UNSTABLE.equals(result)) { status.setDescription( build.getParent().getFullName() + ": This commit has test failures"); status.setStatus("FAILED"); state = Constants.CommitBuildState.FAILED; } else if (Result.FAILURE.equals(result)) { status.setDescription( build.getParent().getFullName() + ": There was a failure building this commit"); status.setStatus("FAILED"); state = Constants.CommitBuildState.FAILED; } else if (result != null) { // ABORTED, NOT_BUILT. status.setDescription(build.getParent().getFullName() + ": Something is wrong with the build of this commit"); status.setStatus("CANCELED"); state = Constants.CommitBuildState.CANCELED; } else { status.setDescription(build.getParent().getFullName() + ": Build started..."); status.setStatus("RUNNING"); state = Constants.CommitBuildState.RUNNING; } final SCMRevision revision = SCMRevisionAction.getRevision(source, build); String hash; if (revision instanceof BranchSCMRevision) { listener.getLogger() .format("[GitLab Pipeline Status] Notifying branch build status: %s %s%n", status.getStatus(), status.getDescription()); hash = ((BranchSCMRevision) revision).getHash(); } else if (revision instanceof MergeRequestSCMRevision) { listener.getLogger() .format("[GitLab Pipeline Status] Notifying merge request build status: %s %s%n", status.getStatus(), status.getDescription()); hash = ((MergeRequestSCMRevision) revision).getOrigin().getHash(); } else if (revision instanceof GitTagSCMRevision) { listener.getLogger() .format("[GitLab Pipeline Status] Notifying tag build status: %s %s%n", status.getStatus(), status.getDescription()); hash = ((GitTagSCMRevision) revision).getHash(); } else { return; } status.setName(getStatusName(sourceContext, build, revision)); final JobScheduledListener jsl = ExtensionList.lookup(QueueListener.class) .get(JobScheduledListener.class); if (jsl != null) { // we are setting the status, so don't let the queue listener background thread change it to pending synchronized (jsl.resolving) { jsl.resolving.remove(build.getParent()); } } try { GitLabApi gitLabApi = GitLabHelper.apiBuilder(source.getServerName()); LOGGER.log(Level.FINE, String.format("Notifiying commit: %s", hash)); gitLabApi.getCommitsApi().addCommitStatus( source.getProjectPath(), hash, state, status); listener.getLogger().format("[GitLab Pipeline Status] Notified%n"); } catch (GitLabApiException e) { if(!e.getMessage().contains(("Cannot transition status"))) { LOGGER.log(Level.WARNING, String.format("Exception caught: %s",e.getMessage())); } } }
Example #19
Source File: GitLabSCMSourceRequest.java From gitlab-branch-source-plugin with MIT License | 2 votes |
/** * Provides the {@link GitLabApi} to use for the request. * * @param gitLabApi {@link GitLabApi} to use for the request. */ public void setGitLabApi(@CheckForNull GitLabApi gitLabApi) { this.gitLabApi = gitLabApi; }
Example #20
Source File: GitLabSCMSourceRequest.java From gitlab-branch-source-plugin with MIT License | 2 votes |
/** * Returns the {@link GitLabApi} to use for the request. * * @return the {@link GitLabApi} to use for the request or {@code null} if caller should * establish their own. */ @CheckForNull public GitLabApi getGitLabApi() { return gitLabApi; }