Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
refactored actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Karsten Kraus authored and Karsten Kraus committed Feb 24, 2017
1 parent 61d905f commit d04057f
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 115 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;


enum BuildStatusPublishMode {
public enum BuildStatusPublishMode {
NONE,
RESULT,
STAGES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ static GitLabSCMBranchHead createBranch(int projectId, String name, String hash,
@Nonnull
public abstract SCMRevisionImpl getRevision();

abstract int getProjectId();
public abstract int getProjectId();

@Nonnull
abstract String getRef();
public abstract String getRef();

@Nonnull
abstract GitLabSCMRefSpec getRefSpec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ final GitLabSCMRefSpec getRefSpec() {
}

@Override
final int getProjectId() {
public final int getProjectId() {
return projectId;
}

@Nonnull
@Override
String getRef() {
public String getRef() {
return refSpec.destinationRef(getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@


public final class GitLabSCMIcons {
enum Size {
public enum Size {
SMALL("icon-sm", "16x16", ICON_SMALL_STYLE),
MEDIUM("icon-md", "24x24", ICON_MEDIUM_STYLE),
LARGE("icon-lg", "32x32", ICON_LARGE_STYLE),
XLARGE("icon-xlg", "48x48", ICON_XLARGE_STYLE);

static Size byDimensions(String dimensions) {
public static Size byDimensions(String dimensions) {
for (Size s : values()) {
if (s.dimensions.equals(dimensions)) {
return s;
Expand All @@ -50,12 +50,13 @@ static Size byDimensions(String dimensions) {
}
}

public static final String ICON_PROJECT = "icon-project";
public static final String ICON_BRANCH = "icon-branch";
public static final String ICON_COMMIT = "icon-commit";
public static final String ICON_MERGE_REQUEST = "icon-merge-request";
public static final String ICON_TAG = "icon-tag";

static final String ICON_GITLAB = "icon-gitlab";
static final String ICON_PROJECT = "icon-project";
static final String ICON_BRANCH = "icon-branch";
static final String ICON_COMMIT = "icon-commit";
static final String ICON_MERGE_REQUEST = "icon-merge-request";
static final String ICON_TAG = "icon-tag";
private static final String ICON_PATH = "plugin/gitlab-branch-source/images/";

@Initializer
Expand All @@ -68,11 +69,7 @@ public static void initialize() {
addIcon(ICON_TAG);
}

static String iconfilePathPattern(String name) {
return ICON_PATH + ":size/" + name + ".png";
}

static String iconFileName(String name, Size size) {
public static String iconFileName(String name, Size size) {
Icon icon = icons.getIconByClassSpec(classSpec(name, size));
if (icon == null) {
return null;
Expand All @@ -83,7 +80,7 @@ static String iconFileName(String name, Size size) {
return icon.getQualifiedUrl(ctx);
}

static String avatarFileName(int projectId, String connectionName, Size size) {
public static String avatarFileName(int projectId, String connectionName, Size size) {
// TODO: size!
try {
GitLabProject project = gitLabAPI(connectionName).getProject(projectId);
Expand All @@ -98,6 +95,10 @@ static String avatarFileName(int projectId, String connectionName, Size size) {

}

static String iconFilePathPattern(String name) {
return ICON_PATH + ":size/" + name + ".png";
}

private static String groupAvatarUrl(GitlabProject project, String connectionName) throws GitLabAPIException {
GitlabNamespace namespace = project.getNamespace();
if (namespace.getOwnerId() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ GitLabSCMHead getSource() {

@Nonnull
@Override
String getRef() {
public String getRef() {
return getRefSpec().destinationRef(sourceBranch.getName());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;


import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabLinkAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabProjectSelector;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabProjectVisibility;
import argelbargel.jenkins.plugins.gitlab_branch_source.hooks.GitLabSCMWebHook;
Expand Down Expand Up @@ -38,7 +39,7 @@
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabHelper.gitLabConnection;
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabHelper.gitLabConnectionId;
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMIcons.ICON_GITLAB;
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMIcons.iconfilePathPattern;
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMIcons.iconFilePathPattern;


// TODO: extract common interface for GitLabSCMSource, GitLabSCMNavigator and SourceSettings
Expand Down Expand Up @@ -433,7 +434,7 @@ public String getIconClassName() {

@Override
public String getIconFilePathPattern() {
return iconfilePathPattern(getIconClassName());
return iconFilePathPattern(getIconClassName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;


import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMAcceptMergeRequestAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMCauseAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMHeadMetadataAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMPublishAction;
import hudson.Extension;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -16,20 +20,22 @@
public class GitLabSCMRunListener extends RunListener<Run<?, ?>> {
@Override
public void onStarted(Run<?, ?> build, TaskListener listener) {
GitLabSCMCauseAction causeAction = build.getParent().getAction(GitLabSCMCauseAction.class);
GitLabSCMHeadMetadataAction metadata = getMetadataAction(build);
GitLabSCMPublishAction publishAction = build.getParent().getAction(GitLabSCMPublishAction.class);
if (causeAction != null && publishAction != null) {
publishAction.updateBuildDescription(build, causeAction, listener);
publishAction.publishStarted(build, causeAction);
if (metadata != null && publishAction != null) {
GitLabSCMCauseAction cause = build.getAction(GitLabSCMCauseAction.class);
String description = (cause != null) ? cause.getDescription() : "";
publishAction.updateBuildDescription(build, description, listener);
publishAction.publishStarted(build, metadata, description);
}
}

@Override
public void onCompleted(Run<?, ?> build, @Nonnull TaskListener listener) {
GitLabSCMCauseAction causeAction = build.getParent().getAction(GitLabSCMCauseAction.class);
GitLabSCMHeadMetadataAction metadata = getMetadataAction(build);
GitLabSCMPublishAction publishAction = build.getParent().getAction(GitLabSCMPublishAction.class);
if (causeAction != null && publishAction != null) {
publishAction.publishResult(build, causeAction);
if (metadata != null && publishAction != null) {
publishAction.publishResult(build, metadata);
}

if (build.getResult() == SUCCESS) {
Expand All @@ -39,4 +45,13 @@ public void onCompleted(Run<?, ?> build, @Nonnull TaskListener listener) {
}
}
}

private GitLabSCMHeadMetadataAction getMetadataAction(Run<?, ?> build) {
GitLabSCMHeadMetadataAction metadata = build.getAction(GitLabSCMHeadMetadataAction.class);
if (metadata == null) {
metadata = build.getParent().getAction(GitLabSCMHeadMetadataAction.class);
}

return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected SCMRevision retrieve(@Nonnull SCMHead head, @Nonnull TaskListener list
@Nonnull
@Override
protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event, @Nonnull TaskListener listener) throws IOException {
return actions.retrieveActions(event, listener);
return actions.retrieve(event, listener);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;


import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabLinkAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabProjectAvatarMetadataAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMAcceptMergeRequestAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMCauseAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMHeadMetadataAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.actions.GitLabSCMPublishAction;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabAPIException;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabMergeRequest;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabProject;
Expand All @@ -25,8 +31,8 @@
import java.util.List;

import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabHelper.gitLabAPI;
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMHead.REVISION_HEAD;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;


class SourceActions {
Expand All @@ -37,77 +43,79 @@ class SourceActions {
}

@Nonnull
List<Action> retrieveActions(@CheckForNull SCMSourceEvent event, @Nonnull TaskListener listener) throws IOException {
List<Action> retrieve(@CheckForNull SCMSourceEvent event, @Nonnull TaskListener listener) throws IOException {
GitLabProject project = source.getProject();
return asList(
new ObjectMetadataAction(null, project.getDescription(), project.getWebUrl()),
new ObjectMetadataAction(project.getNameWithNamespace(), project.getDescription(), project.getWebUrl()),
new GitLabProjectAvatarMetadataAction(project.getId(), source.getConnectionName()),
GitLabLinkAction.toProject(project));
}

@Nonnull
List<Action> retrieve(@Nonnull SCMHead head, @CheckForNull SCMHeadEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
if (head instanceof GitLabSCMHead) {
return retrieve((GitLabSCMHead) head, event, listener);
}

return emptyList();
}

@Nonnull
private List<Action> retrieve(@Nonnull GitLabSCMHead head, @CheckForNull SCMHeadEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
List<Action> actions = new ArrayList<>();

actions.add(new GitLabSCMPublishAction(
source.getUpdateBuildDescription(),
buildStatusPublishMode(head),
source.getPublishUnstableBuildsAsSuccess(),
source.getPublisherName()
));

Action linkAction;

if (head instanceof ChangeRequestSCMHead) {
linkAction = GitLabLinkAction.toMergeRequest(source.getProject(), ((ChangeRequestSCMHead) head).getId());

GitLabMergeRequest mr = retrieveMergeRequest((ChangeRequestSCMHead) head, listener);
Action linkAction = GitLabLinkAction.toMergeRequest(source.getProject(), ((ChangeRequestSCMHead) head).getId());
actions.add(new ObjectMetadataAction(mr.getTitle(), mr.getDescription(), linkAction.getUrlName()));
actions.add(linkAction);
if (acceptMergeRequest(head)) {
boolean removeSourceBranch = mr.getRemoveSourceBranch() || removeSourceBranch(head);
actions.add(new GitLabSCMAcceptMergeRequestAction(mr.getProjectId(), mr.getId(), source.getMergeCommitMessage(), removeSourceBranch));
}
} else {
Action linkAction = (head instanceof TagSCMHead) ? GitLabLinkAction.toTag(source.getProject(), head.getName()) : GitLabLinkAction.toBranch(source.getProject(), head.getName());
actions.add(new ObjectMetadataAction(head.getName(), "", linkAction.getUrlName()));
actions.add(linkAction);
linkAction = (head instanceof TagSCMHead) ? GitLabLinkAction.toTag(source.getProject(), head.getName()) : GitLabLinkAction.toBranch(source.getProject(), head.getName());
if (head instanceof GitLabSCMBranchHead && StringUtils.equals(source.getProject().getDefaultBranch(), head.getName())) {
actions.add(new PrimaryInstanceMetadataAction());
}
}

SCMRevisionImpl rev = (head instanceof GitLabSCMHead) ? ((GitLabSCMHead) head).getRevision() : new SCMRevisionImpl(head, REVISION_HEAD);
actions.addAll(retrieveRevisionActions(rev, event, listener)); // TODO: why is this neccessary? Would be nicer if those actions were added to the run

actions.add(new GitLabSCMHeadMetadataAction(head, linkAction.getUrlName()));
actions.add(linkAction);
return actions;
}


@Nonnull
List<Action> retrieve(@Nonnull SCMRevision revision, @CheckForNull SCMHeadEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
List<Action> actions = new ArrayList<>();

if (revision instanceof SCMRevisionImpl) {
String hash = ((SCMRevisionImpl) revision).getHash();
Action linkAction = GitLabLinkAction.toCommit(source.getProject(), hash);
actions.add(new ObjectMetadataAction(hash, "", linkAction.getUrlName()));
actions.add(linkAction);
actions.addAll(retrieveRevisionActions((SCMRevisionImpl) revision, event, listener));
return retrieve((SCMRevisionImpl) revision, event, listener);
}

return actions;
return emptyList();
}

@Nonnull
private List<Action> retrieveRevisionActions(@Nonnull SCMRevisionImpl revision, @CheckForNull SCMHeadEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
private List<Action> retrieve(@Nonnull SCMRevisionImpl revision, @CheckForNull SCMHeadEvent event, @Nonnull TaskListener listener) throws IOException, InterruptedException {
List<Action> actions = new ArrayList<>();

actions.add(new GitLabSCMPublishAction(
source.getUpdateBuildDescription(),
buildStatusPublishMode(revision.getHead()),
source.getPublishUnstableBuildsAsSuccess(),
source.getPublisherName()
));
String hash = revision.getHash();
Action linkAction = GitLabLinkAction.toCommit(source.getProject(), hash);
actions.add(new GitLabSCMHeadMetadataAction((GitLabSCMHead) revision.getHead(), linkAction.getUrlName(), hash));
actions.add(linkAction);

if (revision.getHead() instanceof ChangeRequestSCMHead) {
if (acceptMergeRequest(revision.getHead())) {
GitLabMergeRequest mr = retrieveMergeRequest((ChangeRequestSCMHead) revision.getHead(), listener);
boolean removeSourceBranch = mr.getRemoveSourceBranch() || removeSourceBranch(revision.getHead());
actions.add(new GitLabSCMAcceptMergeRequestAction(mr.getProjectId(), mr.getId(), source.getMergeCommitMessage(), removeSourceBranch));
}
if (event instanceof GitLabSCMEvent) {
actions.add(new GitLabSCMCauseAction(revision, ((GitLabSCMEvent) event).getCause()));
}

actions.add(
(event instanceof GitLabSCMEvent)
? new GitLabSCMCauseAction(revision, ((GitLabSCMEvent) event).getCause())
: new GitLabSCMCauseAction(revision));

return actions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package argelbargel.jenkins.plugins.gitlab_branch_source;
package argelbargel.jenkins.plugins.gitlab_branch_source.actions;


import argelbargel.jenkins.plugins.gitlab_branch_source.Messages;
import argelbargel.jenkins.plugins.gitlab_branch_source.api.GitLabProject;
import hudson.model.Action;
import org.gitlab.api.models.GitlabProject;
Expand All @@ -20,9 +21,9 @@
import static argelbargel.jenkins.plugins.gitlab_branch_source.GitLabSCMIcons.iconFileName;


class GitLabLinkAction implements Action, IconSpec {
public final class GitLabLinkAction implements Action, IconSpec {
@Nonnull
static GitLabLinkAction create(@CheckForNull String displayName, @Nonnull String iconFileName, @Nonnull String url) {
public static GitLabLinkAction create(@CheckForNull String displayName, @Nonnull String iconFileName, @Nonnull String url) {
if (displayName == null) {
return create("", iconFileName, url);
}
Expand All @@ -31,28 +32,28 @@ static GitLabLinkAction create(@CheckForNull String displayName, @Nonnull String
}

@Nonnull
static GitLabLinkAction toProject(GitlabProject project) {
public static GitLabLinkAction toProject(GitlabProject project) {
return create(Messages.GitLabLink_DisplayName_Project(), ICON_PROJECT, project.getWebUrl());
}

@Nonnull
static GitLabLinkAction toBranch(GitlabProject project, String branchName) {
public static GitLabLinkAction toBranch(GitlabProject project, String branchName) {
return create(Messages.GitLabLink_DisplayName_Branch(), ICON_BRANCH, project, "tree/" + branchName);
}

@Nonnull
static GitLabLinkAction toTag(GitLabProject project, String tagName) {
public static GitLabLinkAction toTag(GitLabProject project, String tagName) {
return create(Messages.GitLabLink_DisplayName_Tag(), ICON_TAG, project, "tree/" + tagName);
}

@Nonnull
static GitLabLinkAction toCommit(GitlabProject project, String hash) {
public static GitLabLinkAction toCommit(GitlabProject project, String hash) {
return create(Messages.GitLabLink_DisplayName_Commit(), ICON_COMMIT, project,
"commits/" + hash);
}

@Nonnull
static GitLabLinkAction toMergeRequest(GitlabProject project, String id) {
public static GitLabLinkAction toMergeRequest(GitlabProject project, String id) {
return create(Messages.GitLabLink_DisplayName_MergeRequest(), ICON_MERGE_REQUEST, project,
"merge_requests/" + String.valueOf(id));
}
Expand Down
Loading

0 comments on commit d04057f

Please sign in to comment.