From 1d9d7bab91b8acfa9a6788fd9482851e2cc89ab7 Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Mon, 19 Oct 2020 19:49:31 +0200 Subject: [PATCH 1/2] Add appveyor provider for CI information --- README.md | 1 + .../impl/appveyor/AppveyorInformation.java | 58 +++++++++++++++++++ .../appveyor/AppveyorInformationProvider.java | 15 +++++ ...le.cidetect.provider.CiInformationProvider | 1 + .../appveyor/AppveyorInformationTest.java | 54 +++++++++++++++++ 5 files changed, 129 insertions(+) create mode 100644 src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java create mode 100644 src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationProvider.java create mode 100644 src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java diff --git a/README.md b/README.md index f9d888d..8ccc453 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Supported CI platforms: * [Jenkins](https://jenkins.io) * [GitLab CI](https://about.gitlab.com/product/continuous-integration/) * [Github actions](https://github.com/features/actions) + * [Appveyor](https://www.appveyor.com) ### Programmatic usage diff --git a/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java new file mode 100644 index 0000000..e6bba22 --- /dev/null +++ b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java @@ -0,0 +1,58 @@ +package be.vbgn.gradle.cidetect.impl.appveyor; + +import be.vbgn.gradle.cidetect.CiInformation; +import java.util.Map; +import javax.annotation.Nullable; + +/** + * https://www.appveyor.com/docs/environment-variables/ + */ +public class AppveyorInformation implements CiInformation { + + private final Map env; + + AppveyorInformation(Map env) { + this.env = env; + } + + @Override + public boolean isCi() { + return env.containsKey("APPVEYOR"); + } + + @Nullable + @Override + public String getBuildNumber() { + return env.getOrDefault("APPVEYOR_BUILD_NUMBER", null); + } + + @Nullable + @Override + public String getBranch() { + if (isPullRequest()) { + return env.getOrDefault("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH", null); + } + return env.getOrDefault("APPVEYOR_REPO_BRANCH", null); + } + + @Nullable + @Override + public String getPullRequest() { + return env.getOrDefault("APPVEYOR_PULL_REQUEST_NUMBER", null); + } + + @Nullable + @Override + public String getPullRequestTargetBranch() { + if (isPullRequest()) { + return env.getOrDefault("APPVEYOR_REPO_BRANCH", null); + } + return null; + } + + @Nullable + @Override + public String getTag() { + return env.getOrDefault("APPVEYOR_REPO_TAG_NAME", null); + } +} diff --git a/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationProvider.java b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationProvider.java new file mode 100644 index 0000000..e91a609 --- /dev/null +++ b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationProvider.java @@ -0,0 +1,15 @@ +package be.vbgn.gradle.cidetect.impl.appveyor; + +import be.vbgn.gradle.cidetect.CiInformation; +import be.vbgn.gradle.cidetect.provider.CiInformationProvider; +import javax.annotation.Nullable; +import org.gradle.api.Project; + +public class AppveyorInformationProvider implements CiInformationProvider { + + @Nullable + @Override + public CiInformation newCiInformation(@Nullable Project project) { + return new AppveyorInformation(System.getenv()); + } +} diff --git a/src/main/resources/META-INF/services/be.vbgn.gradle.cidetect.provider.CiInformationProvider b/src/main/resources/META-INF/services/be.vbgn.gradle.cidetect.provider.CiInformationProvider index 97176c1..9105e87 100644 --- a/src/main/resources/META-INF/services/be.vbgn.gradle.cidetect.provider.CiInformationProvider +++ b/src/main/resources/META-INF/services/be.vbgn.gradle.cidetect.provider.CiInformationProvider @@ -2,3 +2,4 @@ be.vbgn.gradle.cidetect.impl.travis.TravisCiInformationProvider be.vbgn.gradle.cidetect.impl.jenkins.JenkinsInformationProvider be.vbgn.gradle.cidetect.impl.gitlab.GitlabCiInformationProvider be.vbgn.gradle.cidetect.impl.github.GithubActionsInformationProvider +be.vbgn.gradle.cidetect.impl.appveyor.AppveyorInformationProvider diff --git a/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java b/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java new file mode 100644 index 0000000..b453bfd --- /dev/null +++ b/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java @@ -0,0 +1,54 @@ +package be.vbgn.gradle.cidetect.impl.appveyor; + +import static org.junit.Assert.assertEquals; + +import be.vbgn.gradle.cidetect.CiInformation; +import be.vbgn.gradle.cidetect.impl.AbstractCiInformationTest; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.junit.Test; + +public class AppveyorInformationTest extends AbstractCiInformationTest { + + @Override + protected Map getBaseEnv(String buildNumber) { + Map env = new HashMap<>(); + env.put("APPVEYOR", "True"); + env.put("APPVEYOR_BUILD_NUMBER", buildNumber); + return env; + } + + @Override + protected Map getBranchBuildEnv(String branch) { + return Collections.singletonMap("APPVEYOR_REPO_BRANCH", branch); + } + + @Override + protected Map getPullRequestBuildEnv(String sourceBranch, String targetBranch, String prNumber) { + Map environment = new HashMap<>(); + environment.put("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH", sourceBranch); + environment.put("APPVEYOR_REPO_BRANCH", targetBranch); + environment.put("APPVEYOR_PULL_REQUEST_NUMBER", prNumber); + return environment; + } + + @Override + protected Map getTagBuildEnv(String tagName) { + Map environment = new HashMap<>(); + environment.put("APPVEYOR_REPO_BRANCH", tagName); + environment.put("APPVEYOR_REPO_TAG_NAME", tagName); + environment.put("APPVEYOR_REPO_TAG", "true"); + return environment; + } + + @Override + protected CiInformation createCiInformation(Map env) { + return new AppveyorInformation(env); + } + + @Test + public void testPlatform() { + assertEquals("Appveyor", new AppveyorInformation(getBaseEnv("12")).getPlatform()); + } +} From 845e8d4e67aa43326600b0ff81621e759342cd04 Mon Sep 17 00:00:00 2001 From: Lars Vierbergen Date: Mon, 19 Oct 2020 19:55:58 +0200 Subject: [PATCH 2/2] Fix branch name detection for case where appveyor receives a tag --- .../gradle/cidetect/impl/appveyor/AppveyorInformation.java | 3 +++ .../gradle/cidetect/impl/appveyor/AppveyorInformationTest.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java index e6bba22..5f35573 100644 --- a/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java +++ b/src/main/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformation.java @@ -29,6 +29,9 @@ public String getBuildNumber() { @Nullable @Override public String getBranch() { + if (isTag()) { + return null; + } if (isPullRequest()) { return env.getOrDefault("APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH", null); } diff --git a/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java b/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java index b453bfd..a956bc2 100644 --- a/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java +++ b/src/test/java/be/vbgn/gradle/cidetect/impl/appveyor/AppveyorInformationTest.java @@ -36,7 +36,7 @@ protected Map getPullRequestBuildEnv(String sourceBranch, String @Override protected Map getTagBuildEnv(String tagName) { Map environment = new HashMap<>(); - environment.put("APPVEYOR_REPO_BRANCH", tagName); + environment.put("APPVEYOR_REPO_BRANCH", "some-branch-name"); environment.put("APPVEYOR_REPO_TAG_NAME", tagName); environment.put("APPVEYOR_REPO_TAG", "true"); return environment;