Skip to content

Commit

Permalink
Update plugin for git-plugin 4.0.0-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptiste Gaillard committed Jun 11, 2019
1 parent 4e05b9a commit d04d8b5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.coravy.hudson.plugins.github</groupId>
<artifactId>github</artifactId>
<version>1.29.5-SNAPSHOT</version>
<version>1.30.0-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>GitHub plugin</name>
Expand Down Expand Up @@ -62,7 +62,7 @@
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
Expand Down Expand Up @@ -99,13 +99,13 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>git</artifactId>
<version>3.4.0</version>
<version>4.0.0-rc</version>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>2.2.0</version>
<version>2.3.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>apache-httpcomponents-client-4-api</artifactId>
<version>4.5.3-2.1</version>
<version>4.5.5-3.0</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
package org.jenkinsci.plugins.github.util;

import hudson.model.Action;
import hudson.model.Job;
import hudson.model.Run;
import hudson.plugins.git.Revision;
import hudson.plugins.git.util.Build;
import hudson.plugins.git.util.BuildData;
import hudson.plugins.git.util.BuildDetails;
import org.eclipse.jgit.lib.ObjectId;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Stores common methods for {@link BuildData} handling.
*
* @author Oleg Nenashev <[email protected]>
* @since 1.10
*/
public final class BuildDataHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildDataHelper.class);

private BuildDataHelper() {
}

Expand All @@ -34,30 +42,40 @@ private BuildDataHelper() {
* @param buildDataList the list of build datas from a build run
* @return the build data related to the project, null if not found
*/
public static BuildData calculateBuildData(
String parentName, String parentFullName, List<BuildData> buildDataList
public static Action calculateBuildDataOrDetails(
String parentName, String parentFullName, List<Action> buildDataOrDetailsList
) {
LOGGER.debug("Name of parent build: {}", parentName);
LOGGER.debug("Name of parent full build: {}", parentFullName);

if (buildDataList == null) {
if (buildDataOrDetailsList == null) {
return null;
}

if (buildDataList.size() == 1) {
return buildDataList.get(0);
if (buildDataOrDetailsList.size() == 1) {
return buildDataOrDetailsList.get(0);
}

String projectName = parentFullName.replace(parentName, "");

LOGGER.debug("Project name: {}", projectName);

if (projectName.endsWith("/")) {
projectName = projectName.substring(0, projectName.lastIndexOf('/'));
}

for (BuildData buildData : buildDataList) {
Set<String> remoteUrls = buildData.getRemoteUrls();
LOGGER.debug("Project name: {}", projectName);

for (Action buildDataOrDetails : buildDataOrDetailsList) {
Set<String> remoteUrls = buildDataOrDetails instanceof BuildData
? ((BuildData) buildDataOrDetails).getRemoteUrls()
: ((BuildDetails) buildDataOrDetails).getRemoteUrls();

for (String remoteUrl : remoteUrls) {

LOGGER.debug("Remote URL: {}", remoteUrl);
if (remoteUrl.contains(projectName)) {
return buildData;
return buildDataOrDetails;
}
}
}
Expand All @@ -76,19 +94,38 @@ public static BuildData calculateBuildData(
@Nonnull
public static ObjectId getCommitSHA1(@Nonnull Run<?, ?> build) throws IOException {
List<BuildData> buildDataList = build.getActions(BuildData.class);
List<BuildDetails> buildDetailsList = build.getActions(BuildDetails.class);

//
List<Action> buildDataOrDetailsList = new ArrayList<Action>();
buildDataOrDetailsList.addAll(buildDataList);
buildDataOrDetailsList.addAll(buildDetailsList);

/*
List<Action> actions = build.getActions();
for (Action action : actions) {
LOGGER.debug(
"Action, class: {}, displayName: {}, urlName: {}",
action.getClass().getName(),
action.getDisplayName(),
action.getUrlName()
);
}
*/

Job<?, ?> parent = build.getParent();

BuildData buildData = calculateBuildData(
parent.getName(), parent.getFullName(), buildDataList
Action buildDataOrDetails = calculateBuildDataOrDetails(
parent.getName(), parent.getFullName(), buildDataOrDetailsList
);

if (buildData == null) {
if (buildDataOrDetails == null) {
throw new IOException(Messages.BuildDataHelper_NoBuildDataError());
}

// buildData?.lastBuild?.marked and fall back to .revision with null check everywhere to be defensive
Build b = buildData.lastBuild;
Build b = buildDataOrDetails instanceof BuildData
? ((BuildData) buildDataOrDetails).lastBuild : ((BuildDetails) buildDataOrDetails).getBuild();
if (b != null) {
Revision r = b.marked;
if (r == null) {
Expand Down

2 comments on commit d04d8b5

@Narimm
Copy link

@Narimm Narimm commented on d04d8b5 Jun 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bgaillard be great to get this into the main ASAP

@bgaillard
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Narimm thanks, the PR is now published here jenkinsci#216. Do not hesitate to add a thumb up to promote it :-)

Please sign in to comment.