-
Notifications
You must be signed in to change notification settings - Fork 7
move utils module here from gocd-s3-artifacts plugin #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rucha3
wants to merge
5
commits into
indix:master
Choose a base branch
from
rucha3:move-gocd-utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
util-gocd/src/main/java/com/indix/gocd/models/Artifact.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.indix.gocd.models; | ||
|
|
||
| public class Artifact { | ||
| String pipelineName; | ||
| String stageName; | ||
| String jobName; | ||
| Revision revision; | ||
|
|
||
| public Artifact(String pipelineName, String stageName, String jobName) { | ||
| this.pipelineName = pipelineName; | ||
| this.stageName = stageName; | ||
| this.jobName = jobName; | ||
| } | ||
|
|
||
| public Artifact(String pipelineName, String stageName, String jobName, Revision revision) { | ||
| this.pipelineName = pipelineName; | ||
| this.stageName = stageName; | ||
| this.jobName = jobName; | ||
| this.revision = revision; | ||
| } | ||
|
|
||
| public Artifact withRevision(Revision revision) { | ||
| this.revision = revision; | ||
| return this; | ||
| } | ||
|
|
||
| public String prefix(){ | ||
| return String.format("%s/%s/%s/", pipelineName, stageName, jobName); | ||
| } | ||
|
|
||
| public String prefixWithRevision(){ | ||
| if(revision != null) | ||
| return String.format("%s/%s/%s/%s/", pipelineName, stageName, jobName, revision.getRevision()); | ||
| else | ||
| return prefix(); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
util-gocd/src/main/java/com/indix/gocd/models/ResponseMetadataConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.indix.gocd.models; | ||
|
|
||
| public class ResponseMetadataConstants { | ||
| public static final String TRACEBACK_URL = "traceback_url"; | ||
| public static final String USER = "user"; | ||
| public static final String REVISION_COMMENT = "revision_comment"; | ||
| public static final String COMPLETED = "completed"; | ||
| public static final String GO_PIPELINE_LABEL = "go_pipeline_label"; | ||
| } |
49 changes: 49 additions & 0 deletions
49
util-gocd/src/main/java/com/indix/gocd/models/Revision.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.indix.gocd.models; | ||
|
|
||
| public class Revision implements Comparable { | ||
| private String revision; | ||
| private String[] parts; | ||
| private Integer major; | ||
| private Integer minor; | ||
| private Integer patch; | ||
|
|
||
| public Revision(String revision) { | ||
| this.revision = revision; | ||
| this.parts = revision.split("\\."); | ||
| this.major = Integer.valueOf(parts[0]); | ||
| this.minor = Integer.valueOf(parts[1]); | ||
| if (parts.length == 3) { | ||
| this.patch = Integer.valueOf(parts[2]); | ||
| } else { | ||
| this.patch = 0; | ||
| } | ||
| } | ||
|
|
||
| public static Revision base() { | ||
| return new Revision("0.0.0"); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(Object otherInstance) { | ||
| if(! (otherInstance instanceof Revision)) | ||
| throw new RuntimeException("Cannot compare a non-Revision type with Revision type"); | ||
| Revision that = (Revision)otherInstance; | ||
| int majorDiff = this.major.compareTo(that.major); | ||
| int minorDiff = this.minor.compareTo(that.minor); | ||
| int patchDiff = this.patch.compareTo(that.patch); | ||
|
|
||
| if(majorDiff != 0) | ||
| return majorDiff; | ||
| else if(minorDiff != 0) | ||
| return minorDiff; | ||
| else if(patchDiff != 0) | ||
| return patchDiff; | ||
| else | ||
| return 0; | ||
| } | ||
|
|
||
| public String getRevision() { | ||
| return revision; | ||
| } | ||
|
|
||
| } |
41 changes: 41 additions & 0 deletions
41
util-gocd/src/main/java/com/indix/gocd/models/RevisionStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.indix.gocd.models; | ||
|
|
||
| import com.amazonaws.util.StringUtils; | ||
|
|
||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class RevisionStatus { | ||
| public Revision revision; | ||
| public Date lastModified; | ||
| public String tracebackUrl; | ||
| public String user; | ||
| public String revisionLabel; | ||
| private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; | ||
|
|
||
| public RevisionStatus(Revision revision, Date lastModified, String tracebackUrl, String user) { | ||
| this(revision, lastModified, tracebackUrl, user, ""); | ||
| } | ||
|
|
||
| public RevisionStatus(Revision revision, Date lastModified, String tracebackUrl, String user, String revisionLabel) { | ||
| this.revision = revision; | ||
| this.lastModified = lastModified; | ||
| this.tracebackUrl = tracebackUrl; | ||
| this.user = user; | ||
| this.revisionLabel = revisionLabel; | ||
| } | ||
|
|
||
| public Map toMap() { | ||
| final HashMap result = new HashMap(); | ||
| result.put("revision", revision.getRevision()); | ||
| result.put("timestamp", new SimpleDateFormat(DATE_PATTERN).format(lastModified)); | ||
| result.put("user", user); | ||
| result.put("revisionComment", String.format("Original revision number: %s", | ||
| StringUtils.isNullOrEmpty(revisionLabel) ? "unavailable" : revisionLabel)); | ||
| result.put("trackbackUrl", tracebackUrl); | ||
|
|
||
| return result; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
util-gocd/src/main/java/com/indix/gocd/utils/Constants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.indix.gocd.utils; | ||
|
|
||
| public class Constants { | ||
| public static final String METADATA_USER = "user"; | ||
| public static final String METADATA_TRACEBACK_URL = "traceback_url"; | ||
| public static final String COMPLETED = "completed"; | ||
|
|
||
| public static final String GO_ARTIFACTS_S3_BUCKET = "GO_ARTIFACTS_S3_BUCKET"; | ||
| public static final String GO_SERVER_DASHBOARD_URL = "GO_SERVER_DASHBOARD_URL"; | ||
|
|
||
| public static final String SOURCEDESTINATIONS = "sourceDestinations"; | ||
| public static final String DESTINATION_PREFIX = "destinationPrefix"; | ||
| public static final String ARTIFACTS_BUCKET = "artifactsBucket"; | ||
|
|
||
| public static final String AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"; | ||
| public static final String AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"; | ||
| public static final String AWS_REGION = "AWS_REGION"; | ||
| public static final String AWS_USE_IAM_ROLE = "AWS_USE_IAM_ROLE"; | ||
| public static final String AWS_STORAGE_CLASS = "AWS_STORAGE_CLASS"; | ||
| public static final String STORAGE_CLASS_STANDARD = "standard"; | ||
| public static final String STORAGE_CLASS_STANDARD_IA = "standard-ia"; | ||
| public static final String STORAGE_CLASS_RRS = "rrs"; | ||
| public static final String STORAGE_CLASS_GLACIER = "glacier"; | ||
|
|
||
| public static final String GO_PIPELINE_LABEL = "GO_PIPELINE_LABEL"; | ||
|
|
||
| public static final String MATERIAL_TYPE = "MaterialType"; | ||
| public static final String REPO = "Repo"; | ||
| public static final String PACKAGE = "Package"; | ||
| public static final String MATERIAL = "Material"; | ||
| public static final String JOB = "Job"; | ||
| public static final String STAGE = "Stage"; | ||
| public static final String SOURCE = "Source"; | ||
| public static final String SOURCE_PREFIX = "SourcePrefix"; | ||
| public static final String DESTINATION = "Destination"; | ||
|
|
||
| public static final String REQUIRED_FIELD_MESSAGE = "This field is required"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.indix.gocd.utils; | ||
|
|
||
|
|
||
| import com.thoughtworks.go.plugin.api.task.JobConsoleLogger; | ||
|
|
||
| import java.nio.file.Paths; | ||
| import java.util.Map; | ||
|
|
||
| public class Context { | ||
| private final Map environmentVariables; | ||
| private final String workingDir; | ||
| private final JobConsoleLogger console; | ||
|
|
||
| public Context(Map context) { | ||
| environmentVariables = (Map) context.get("environmentVariables"); | ||
| workingDir = (String) context.get("workingDirectory"); | ||
| console = new JobConsoleLogger() {}; | ||
| } | ||
|
|
||
| public void printMessage(String message) { | ||
| console.printLine(message); | ||
| } | ||
|
|
||
| public void printEnvironment() { | ||
| console.printEnvironment(environmentVariables); | ||
| } | ||
|
|
||
| public Map getEnvironmentVariables() { | ||
| return environmentVariables; | ||
| } | ||
|
|
||
| public String getWorkingDir() { | ||
| return workingDir; | ||
| } | ||
|
|
||
| public String getAbsoluteWorkingDir() { | ||
| return Paths.get("").toAbsolutePath().resolve(workingDir).toString(); | ||
| } | ||
| } |
124 changes: 124 additions & 0 deletions
124
util-gocd/src/main/java/com/indix/gocd/utils/GoEnvironment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package com.indix.gocd.utils; | ||
|
|
||
| import org.apache.commons.lang3.BooleanUtils; | ||
|
|
||
| import java.lang.StringBuffer; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import static com.indix.gocd.utils.Constants.*; | ||
| import static org.apache.commons.lang3.StringUtils.isNotEmpty; | ||
|
|
||
| /** | ||
| * Wrapper around Go's Environment variables | ||
| */ | ||
| public class GoEnvironment { | ||
| private Pattern envPat = Pattern.compile("\\$\\{(\\w+)\\}"); | ||
| private Map<String, String> environment = new HashMap<>(); | ||
|
|
||
| public GoEnvironment() { | ||
| this.environment.putAll(System.getenv()); | ||
| } | ||
|
|
||
| public GoEnvironment(Map<String, String> defaultEnvironment) { | ||
| this(); | ||
| this.environment.putAll(defaultEnvironment); | ||
| } | ||
|
|
||
| public GoEnvironment putAll(Map<String, String> existing) { | ||
| environment.putAll(existing); | ||
| return this; | ||
| } | ||
|
|
||
| public Map<String,String> asMap() { return environment; } | ||
|
|
||
| public String get(String name) { | ||
| return environment.get(name); | ||
| } | ||
|
|
||
| public String getOrElse(String name, String defaultValue) { | ||
| if(has(name)) return get(name); | ||
| else return defaultValue; | ||
| } | ||
|
|
||
| public boolean has(String name) { | ||
| return environment.containsKey(name) && isNotEmpty(get(name)); | ||
| } | ||
|
|
||
| public boolean isAbsent(String name) { | ||
| return !has(name); | ||
| } | ||
|
|
||
| public String traceBackUrl() { | ||
| String serverUrl = get(GO_SERVER_DASHBOARD_URL); | ||
| String pipelineName = get("GO_PIPELINE_NAME"); | ||
| String pipelineCounter = get("GO_PIPELINE_COUNTER"); | ||
| String stageName = get("GO_STAGE_NAME"); | ||
| String stageCounter = get("GO_STAGE_COUNTER"); | ||
| String jobName = get("GO_JOB_NAME"); | ||
| return String.format("%s/go/tab/build/detail/%s/%s/%s/%s/%s", serverUrl, pipelineName, pipelineCounter, stageName, stageCounter, jobName); | ||
| } | ||
|
|
||
| public String triggeredUser() { | ||
| return get("GO_TRIGGER_USER"); | ||
| } | ||
|
|
||
| public String replaceVariables(String str) { | ||
| Matcher m = envPat.matcher(str); | ||
|
|
||
| StringBuffer sb = new StringBuffer(); | ||
| while (m.find()) { | ||
| String replacement = get(m.group(1)); | ||
| if(replacement != null) { | ||
| m.appendReplacement(sb, replacement); | ||
| } | ||
| } | ||
|
|
||
| m.appendTail(sb); | ||
|
|
||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Version Format on S3 is <code>pipeline/stage/job/pipeline_counter.stage_counter</code> | ||
| */ | ||
| public String artifactsLocationTemplate() { | ||
| String pipeline = get("GO_PIPELINE_NAME"); | ||
| String stageName = get("GO_STAGE_NAME"); | ||
| String jobName = get("GO_JOB_NAME"); | ||
|
|
||
| String pipelineCounter = get("GO_PIPELINE_COUNTER"); | ||
| String stageCounter = get("GO_STAGE_COUNTER"); | ||
| return artifactsLocationTemplate(pipeline, stageName, jobName, pipelineCounter, stageCounter); | ||
| } | ||
|
|
||
| public String artifactsLocationTemplate(String pipeline, String stageName, String jobName, String pipelineCounter, String stageCounter) { | ||
| return String.format("%s/%s/%s/%s.%s", pipeline, stageName, jobName, pipelineCounter, stageCounter); | ||
| } | ||
|
|
||
| private static final List<String> validUseIamRoleValues = new ArrayList<String>(Arrays.asList("true", "false", "yes", "no", "on", "off")); | ||
| public boolean hasAWSUseIamRole() { | ||
| if (!has(AWS_USE_IAM_ROLE)) { | ||
| return false; | ||
| } | ||
|
|
||
| String useIamRoleValue = get(AWS_USE_IAM_ROLE); | ||
| Boolean result = BooleanUtils.toBooleanObject(useIamRoleValue); | ||
| if (result == null) { | ||
| throw new IllegalArgumentException(getEnvInvalidFormatMessage(AWS_USE_IAM_ROLE, | ||
| useIamRoleValue, validUseIamRoleValues.toString())); | ||
| } | ||
| else { | ||
| return result.booleanValue(); | ||
| } | ||
| } | ||
|
|
||
| private String getEnvInvalidFormatMessage(String environmentVariable, String value, String expected){ | ||
| return String.format( | ||
| "Unexpected value in %s environment variable; was %s, but expected one of the following %s", | ||
| environmentVariable, value, expected); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to enforce 1.8? Scala 2.11 can work with 1.6. Forcing this to 1.8 might break compatibility with Scala 2.11 assumption. Unless we're using Lamdas' and Streams API introduced in JDK8.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are using lambdas in the gocd-util module. So, I put that in common settings. Should I make it module specific only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah Okay, see the above message on moving to Scala. If we decide to do that this comment becomes redundant.