Skip to content

Commit

Permalink
implementation of several macros for the token-macro plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Mar 1, 2011
1 parent d8f835f commit 9e58472
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -106,6 +106,12 @@
<version>2.4</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.0-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
87 changes: 87 additions & 0 deletions src/main/java/hudson/plugins/git/GitBranchTokenMacro.java
@@ -0,0 +1,87 @@
/*
* The MIT License
*
* Copyright 2011 CloudBees, Inc.
*
* 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.
*/
package hudson.plugins.git;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.git.util.BuildData;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

import java.io.IOException;

/**
* {@code GIT_BRANCH} token that expands to the branch(es) that was built.
*
* @author Kohsuke Kawaguchi
*/
@Extension
public class GitBranchTokenMacro extends DataBoundTokenMacro {
/**
* If true, list up all the branches not just the first one.
*/
@Parameter
public boolean all;

/**
* If true, include all the prefixes of the branch name
*/
@Parameter
public boolean fullName;

@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals("GIT_BRANCH");
}

@Override
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
BuildData data = context.getAction(BuildData.class);
if (data == null) {
return ""; // shall we report an error more explicitly?
}

Revision lb = data.getLastBuiltRevision();
if (lb==null || lb.branches.isEmpty()) return "";

if (all) {
StringBuilder buf = new StringBuilder();
for (Branch b : lb.branches) {
if (buf.length()>0) buf.append(',');
buf.append(format(b));
}
return buf.toString();
} else {
return format(lb.branches.iterator().next());
}
}

private String format(Branch b) {
String n = b.getName();
if (fullName) return n;
return n.substring(n.lastIndexOf('/')+1); // trim off '/'
}
}

66 changes: 66 additions & 0 deletions src/main/java/hudson/plugins/git/GitRevisionTokenMacro.java
@@ -0,0 +1,66 @@
/*
* The MIT License
*
* Copyright 2011 CloudBees, Inc.
*
* 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.
*/
package hudson.plugins.git;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.plugins.git.util.BuildData;
import org.jenkinsci.plugins.tokenmacro.DataBoundTokenMacro;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;

import java.io.IOException;

/**
* {@code GIT_REVISION} token that expands to the SHA1 of the commit that was built.
*
* @author Kohsuke Kawaguchi
*/
@Extension
public class GitRevisionTokenMacro extends DataBoundTokenMacro {
/**
* Number of chars to use
*/
@Parameter
public int length=40;

@Override
public boolean acceptsMacroName(String macroName) {
return macroName.equals("GIT_REVISION");
}

@Override
public String evaluate(AbstractBuild<?, ?> context, TaskListener listener, String macroName) throws MacroEvaluationException, IOException, InterruptedException {
BuildData data = context.getAction(BuildData.class);
if (data == null) {
return ""; // shall we report an error more explicitly?
}

Revision lb = data.getLastBuiltRevision();
if (lb==null) return "";

String s = lb.getSha1String();
return s.substring(0,Math.min(length,s.length()));
}
}
@@ -0,0 +1,20 @@
<j:jelly xmlns:j="jelly:core">
<dt>$${GIT_BRANCH}</dt>
<dd>
Expands to the name of the branch that was built.
</dd>

<h3>Parameters</h3>
<dl>
<dt>all</dt>
<dd>
If specified, all the branches that point to the given commit is listed.
By default, the token expands to just one of them.
</dd>
<dt>fullName</dt>
<dd>
If specified, this token expands to the full branch name, such as 'origin/master'.
Otherwise, it only expands to the short name, such as 'master'.
</dd>
</dl>
</j:jelly>
@@ -0,0 +1,15 @@
<j:jelly xmlns:j="jelly:core">
<dt>$${GIT_REVISION}</dt>
<dd>
Expands to the Git SHA1 commit ID that points to the commit that was built.
</dd>

<h3>Parameters</h3>
<dl>
<dt>length=N (optional, default to 40)</dt>
<dd>
Specify the commit ID length. Full SHA1 commit ID is 40 character long, but it is common
to cut it off at 8 or 12 as that often provide enough uniqueness and is a lot more legible.
</dd>
</dl>
</j:jelly>

0 comments on commit 9e58472

Please sign in to comment.