Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation of several macros for the token-macro plugin
- Loading branch information
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,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
66
src/main/java/hudson/plugins/git/GitRevisionTokenMacro.java
This file contains 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,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())); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/resources/hudson/plugins/git/GitBranchTokenMacro/help.jelly
This file contains 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,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> |
15 changes: 15 additions & 0 deletions
15
src/main/resources/hudson/plugins/git/GitRevisionTokenMacro/help.jelly
This file contains 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,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> |