jenkins.plugins.git.GitTagSCMRevision Java Examples

The following examples show how to use jenkins.plugins.git.GitTagSCMRevision. 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: GitLabPipelineStatusNotifier.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
private static String getStatusName(final GitLabSCMSourceContext sourceContext, final String fullDisplayName, final SCMRevision revision) {
    final String type;
    if (revision instanceof BranchSCMRevision) {
        type = "branch";
    } else if (revision instanceof MergeRequestSCMRevision) {
        type = getMrBuildName(fullDisplayName);
    } else if (revision instanceof GitTagSCMRevision) {
        type = "tag";
    } else {
        type = "UNKNOWN";
        LOGGER.log(Level.WARNING, () -> "Unknown SCMRevision implementation "
            + revision.getClass().getName() + ", append" + type + " to status name");
    }

    String customPrefix = sourceContext.getBuildStatusNameCustomPart();
    if (!customPrefix.isEmpty())
    {
        customPrefix = customPrefix + GITLAB_PIPELINE_STATUS_DELIMITER;
    }

    final String statusName = GITLAB_PIPELINE_STATUS_PREFIX + GITLAB_PIPELINE_STATUS_DELIMITER + customPrefix + type;
    LOGGER.log(Level.FINEST, () -> "Retrieved status name is: " + statusName);
    return statusName;
}
 
Example #2
Source File: GitLabSCMFileSystem.java    From gitlab-branch-source-plugin with MIT License 6 votes vote down vote up
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: GitLabTagPushSCMEvent.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
@NonNull
@Override
public Map<SCMHead, SCMRevision> headsFor(GitLabSCMSource source) {
    String ref = getPayload().getRef();
    ref = ref.startsWith(Constants.R_TAGS) ? ref.substring(Constants.R_TAGS.length()) : ref;
    long time = 0L;
    if (getType() == CREATED) {
        time = getPayload().getCommits().get(0).getTimestamp().getTime();
    }
    GitLabTagSCMHead h = new GitLabTagSCMHead(ref, time);
    String hash = getPayload().getCheckoutSha();
    return Collections.<SCMHead, SCMRevision>singletonMap(h,
        (getType() == CREATED)
            ? new GitTagSCMRevision(h, hash) : null);
}
 
Example #4
Source File: GitLabPipelineStatusNotifier.java    From gitlab-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * 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 #5
Source File: GitLabPipelineStatusNotifier.java    From gitlab-branch-source-plugin with MIT License 4 votes vote down vote up
/**
 * 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()));
        }
    }
}