Skip to content

Commit

Permalink
Initial publication.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed May 2, 2021
0 parents commit ab332f7
Show file tree
Hide file tree
Showing 24 changed files with 951 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# IDE/Project Settings
*.idea
*.iml

.gradle
*/build/
teamcity
downloads
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2021 ParchmentMC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Axe
Custom triggers for TeamCity which react to the releases of Minecraft versions.
12 changes: 12 additions & 0 deletions axe-common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: 'java'

repositories {
mavenCentral()
maven {
url 'http://repository.jetbrains.com/all'
}
}

dependencies {
testCompile "junit:junit:3.8.1"
}
37 changes: 37 additions & 0 deletions axe-common/src/main/java/net/mojang/manifest/versions/Latest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.mojang.manifest.versions;

public class Latest
{
private String release;
private String snapshot;

public Latest()
{
}

public Latest(final String release, final String snapshot)
{
this.release = release;
this.snapshot = snapshot;
}

public String getRelease()
{
return release;
}

public void setRelease(String release)
{
this.release = release;
}

public String getSnapshot()
{
return snapshot;
}

public void setSnapshot(String snapshot)
{
this.snapshot = snapshot;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package net.mojang.manifest.versions;

import java.util.List;

public class Manifest{

private Latest latest;
private List<Version> versions;

public Manifest()
{
}

public Manifest(final Latest latest, final List<Version> versions)
{
this.latest = latest;
this.versions = versions;
}

public Latest getLatest()
{
return latest;
}

public void setLatest(Latest latest)
{
this.latest = latest;
}

public List<Version> getVersions()
{
return versions;
}

public void setVersions(List<Version> versions)
{
this.versions = versions;
}
}

99 changes: 99 additions & 0 deletions axe-common/src/main/java/net/mojang/manifest/versions/Version.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package net.mojang.manifest.versions;

import java.util.Date;

public class Version
{
private String id;
private String type;
private String url;
private Date time;
private Date releaseTime;
private String sha1;
private int complianceLevel;

public Version()
{
}

public Version(final String id, final String type, final String url, final Date time, final Date releaseTime, final String sha1, final int complianceLevel)
{
this.id = id;
this.type = type;
this.url = url;
this.time = time;
this.releaseTime = releaseTime;
this.sha1 = sha1;
this.complianceLevel = complianceLevel;
}

public String getId()
{
return id;
}

public void setId(String id)
{
this.id = id;
}

public String getType()
{
return type;
}

public void setType(String type)
{
this.type = type;
}

public String getUrl()
{
return url;
}

public void setUrl(String url)
{
this.url = url;
}

public Date getTime()
{
return time;
}

public void setTime(Date time)
{
this.time = time;
}

public Date getReleaseTime()
{
return releaseTime;
}

public void setReleaseTime(Date releaseTime)
{
this.releaseTime = releaseTime;
}

public String getSha1()
{
return sha1;
}

public void setSha1(String sha1)
{
this.sha1 = sha1;
}

public int getComplianceLevel()
{
return complianceLevel;
}

public void setComplianceLevel(int complianceLevel)
{
this.complianceLevel = complianceLevel;
}
}
21 changes: 21 additions & 0 deletions axe-common/src/main/java/org/parchmentmc/axe/common/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.parchmentmc.axe.common;

public interface Constants {

String MINECRAFT_RELEASE_TRIGGER_NAME = "minecraftReleaseTrigger";
String MINECRAFT_RELEASE_TRIGGER_DISPLAYNAME = "Minecraft Release";
String MINECRAFT_RELEASE_TRIGGER_DESCRIPTION = "Triggers a build when ever Mojang releases a new release version of Minecraft";

String MINECRAFT_SNAPSHOT_TRIGGER_NAME = "minecraftSnapshotTrigger";
String MINECRAFT_SNAPSHOT_TRIGGER_DISPLAYNAME = "Minecraft Snapshot";
String MINECRAFT_SNAPSHOT_TRIGGER_DESCRIPTION = "Triggers a build when ever Mojang releases a new snapshot version of Minecraft";

String MINECRAFT_TRIGGER_NAME = "minecraftTrigger";
String MINECRAFT_TRIGGER_DISPLAYNAME = "Minecraft Snapshot or Release";
String MINECRAFT_TRIGGER_DESCRIPTION = "Triggers a build when ever Mojang releases a new version of Minecraft";


String MINECRAFT_RELEASE_OR_SNAPSHOT_POLICY_LAST_VERSION_KEY = "lastVersion";

String MINECRAFT_RELEASE_VERSIONS_MANIFEST = "https://launchermeta.mojang.com/mc/game/version_manifest_v2.json";
}
11 changes: 11 additions & 0 deletions axe-server/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply plugin: 'java'
apply plugin: 'com.github.rodm.teamcity-server'

dependencies {
compile project(':axe-common')
provided "org.jetbrains.teamcity.internal:server:${teamcityVersion}"
}

teamcity {
version = teamcityVersion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.parchmentmc.axe.build.trigger;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jetbrains.buildServer.buildTriggers.BuildTriggerException;
import jetbrains.buildServer.buildTriggers.PolledTriggerContext;
import jetbrains.buildServer.buildTriggers.async.BaseAsyncPolledBuildTrigger;
import net.mojang.manifest.versions.Manifest;
import net.mojang.manifest.versions.Version;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.parchmentmc.axe.common.Constants;

import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Date;

public class MinecraftBuildTriggerPolicy extends BaseAsyncPolledBuildTrigger
{
private static final Gson GSON = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss+00:00")
.create();

private static Date getDateForRelease(final Manifest manifest, final String releaseName) {
return manifest.getVersions().stream().filter(v -> v.getId().equals(releaseName)).map(Version::getReleaseTime).findFirst().orElse(Date.from(Instant.EPOCH));
}

@Nullable
@Override
public String triggerBuild(@Nullable final String previousValue, @NotNull final PolledTriggerContext context) throws BuildTriggerException
{
try(final CloseableHttpClient client = HttpClientBuilder.create().build()) {
final HttpGet versionsGetRequest = new HttpGet(Constants.MINECRAFT_RELEASE_VERSIONS_MANIFEST);
try(final CloseableHttpResponse response = client.execute(versionsGetRequest)) {
if (response.getStatusLine().getStatusCode() < 200 && response.getStatusLine().getStatusCode() > 299)
{
throw new IllegalStateException("Could not successfully download the versions json.");
}

final HttpEntity entity = response.getEntity();
final String bodyJson = EntityUtils.toString(entity, StandardCharsets.UTF_8);

final Manifest manifest = GSON.fromJson(bodyJson, Manifest.class);
final Date currentDate = previousValue != null ? getDateForRelease(manifest, previousValue) : Date.from(Instant.EPOCH);
final Date latestSnapshotDate = getDateForRelease(manifest, manifest.getLatest().getSnapshot());
final Date latestReleaseDate = getDateForRelease(manifest, manifest.getLatest().getRelease());

if (currentDate.before(latestSnapshotDate) && latestSnapshotDate.before(latestReleaseDate)) {
context.getBuildType().addToQueue(Constants.MINECRAFT_TRIGGER_NAME);
return manifest.getLatest().getRelease();
}
else if (currentDate.before(latestReleaseDate) && latestReleaseDate.before(latestSnapshotDate)) {
context.getBuildType().addToQueue(Constants.MINECRAFT_TRIGGER_NAME);
return manifest.getLatest().getSnapshot();
}
else if (latestSnapshotDate.before(currentDate) && currentDate.before(latestReleaseDate)) {
context.getBuildType().addToQueue(Constants.MINECRAFT_TRIGGER_NAME);
return manifest.getLatest().getRelease();
}
else if (latestReleaseDate.before(currentDate) && currentDate.before(latestSnapshotDate)) {
context.getBuildType().addToQueue(Constants.MINECRAFT_TRIGGER_NAME);
return manifest.getLatest().getSnapshot();
}

return previousValue;
}
}
catch(Exception ex) {
throw new BuildTriggerException("Failed to trigger build", ex);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.parchmentmc.axe.build.trigger;

import com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.buildTriggers.BuildTriggerDescriptor;
import jetbrains.buildServer.buildTriggers.BuildTriggerService;
import jetbrains.buildServer.buildTriggers.BuildTriggeringPolicy;
import jetbrains.buildServer.buildTriggers.async.AsyncPolledBuildTriggerFactory;
import jetbrains.buildServer.log.Loggers;
import org.jetbrains.annotations.NotNull;
import org.parchmentmc.axe.common.Constants;

public class MinecraftBuildTriggerService extends BuildTriggerService
{
@NotNull
private static final Logger LOG = Logger.getInstance(Loggers.VCS_CATEGORY + MinecraftBuildTriggerService.class);
private final BuildTriggeringPolicy policy;

public MinecraftBuildTriggerService(
@NotNull final AsyncPolledBuildTriggerFactory triggerFactory
)
{
this.policy = triggerFactory.createBuildTrigger(new MinecraftSnapshotBuildTriggerPolicy(), LOG);
}

@NotNull
@Override
public String getName()
{
return Constants.MINECRAFT_TRIGGER_NAME;
}

@NotNull
@Override
public String getDisplayName()
{
return Constants.MINECRAFT_TRIGGER_DISPLAYNAME;
}

@NotNull
@Override
public String describeTrigger(@NotNull final BuildTriggerDescriptor trigger)
{
return Constants.MINECRAFT_TRIGGER_DESCRIPTION;
}

@NotNull
@Override
public BuildTriggeringPolicy getBuildTriggeringPolicy()
{
return policy;
}
}
Loading

0 comments on commit ab332f7

Please sign in to comment.