Skip to content

Instantly share code, notes, and snippets.

@elpete
Last active July 18, 2018 14:59
Show Gist options
  • Save elpete/c70a46b98afd400841c6201fe235e268 to your computer and use it in GitHub Desktop.
Save elpete/c70a46b98afd400841c6201fe235e268 to your computer and use it in GitHub Desktop.
Update all .travis.yml files for a given user to have a new engine in the matrix
/**
* Description of task
*/
component {
property name="wirebox" inject="wirebox";
function run(
string searchQuery,
string newEngine,
string user,
string forkUser,
string githubToken
) {
loadModule( "modules/hyper" );
var githubAPI = wirebox.getInstance( "HyperBuilder@hyper" );
githubAPI.defaults.setAutoSerializeQueryString( false );
githubAPI.defaults.setBaseUrl( "https://api.github.com" );
githubAPI.defaults.setHeader( "Authorization", "token #githubToken#" );
var results = githubAPI.get( "/search/code", {
"q" = "ENGINE%3Dadobe+user:#user#+filename:.travis.yml"
} );
results.json().items.each( function( file ) {
// fork
var forkResponse = githubAPI.post( "/repos/#file.repository.full_name#/forks" ).json();
sleep( 5000 );
// clone fork
var gitUrl = forkResponse.html_url & ".git";
var tempDirectory = createObject( "java", "java.io.File" )
.init( getTempDirectory() & file.repository.name & "-" & createUUID() );
var git = createObject( "java", "org.eclipse.jgit.api.Git" )
.cloneRepository()
.setURI( gitUrl )
.setDirectory( tempDirectory )
.call();
var gitDirectory = git.getRepository().getDirectory().getParentFile().getAbsolutePath()
var travisPath = gitDirectory & "/.travis.yml";
var fileContents = fileRead( travisPath );
var yamlReader = createObject( "java", "com.esotericsoftware.yamlbeans.YamlReader", "lib/yamlbeans-1.13/yamlbeans-1.13.jar" )
.init( fileContents );
var root = structNew( "ordered" ).append( yamlReader.read() );
if ( root.keyExists( "env" ) ) {
var env = structNew( "ordered" ).append( root.env );
if ( env.keyExists( "matrix" ) ) {
if ( ! env.matrix.containsNoCase( "ENGINE=adobe@2018" ) ) {
var newMatrix = env.matrix
.append( "ENGINE=#newEngine#" )
.sort( "text" )
.reverse();
var newEnv = env.append( {
matrix = newMatrix
} );
var newRoot = root.append( {
env = newEnv
} );
var charArrayWriter = createObject( "java", "java.io.CharArrayWriter" );
var yamlWriter = createObject( "java", "com.esotericsoftware.yamlbeans.YamlWriter", "lib/yamlbeans-1.13/yamlbeans-1.13.jar" )
.init( charArrayWriter );
var enumOption = createObject( "java", "com.esotericsoftware.yamlbeans.YamlConfig$WriteClassName", "lib/yamlbeans-1.13/yamlbeans-1.13.jar" ).NEVER;
yamlWriter.getConfig().writeConfig.setWriteClassname( enumOption );
yamlWriter.write( newRoot );
yamlWriter.close();
var newYaml = charArrayWriter.toString();
charArrayWriter.close();
// create and checkout branch
var branchName = "add-adobe-2018-to-ci";
git.checkout()
.setCreateBranch( true )
.setName( branchName )
.call();
// create change
fileWrite( gitDirectory & "/.travis.yml", newYaml );
// add change
git.add().addFilePattern( ".travis.yml" ).call();
// commit change
git.commit().setMessage( "Add adobe@2018 to .travis.yml" ).call();
// push change to fork
var credentialsProvider = createObject( "java", "org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider" )
.init( githubToken, "" );
git.push().setRemote( "origin" ).add( branchName ).setCredentialsProvider( credentialsProvider ).call();
// create PR to original repo
githubAPI.post( "/repos/#file.repository.full_name#/pulls", {
"title" = "Add Adobe 2018 to Travis CI",
"head" = "#forkUser#:#branchName#",
"base" = "master",
"body" = "This auto-generated PR adds Adobe 2018 to the Travis CI matrix",
"maintainer_can_modify" = true
} );
print.line( "PR sent to #file.repository.full_name#" ).toConsole();
}
}
}
} );
return;
print.greenLine( 'Complete!' );
}
}
{
"name":"Add Adobe 2018 to Travis Builds",
"version":"1.0.0",
"author":"Eric Peterson <eric@elpete.com>",
"location":"",
"repository":{
"type":"",
"URL":""
},
"slug":"add_new_engine_to_travis_builds",
"description":"Add a new engine to every .travis.yml file for a user",
"type":"commandbox-modules",
"private":false,
"license":[
{
"type":"MIT"
}
],
"dependencies":{
"hyper":"^1.14.5",
"yamlbeans-1.13":"jar:http://repo1.maven.org/maven2/com/esotericsoftware/yamlbeans/yamlbeans/1.13/yamlbeans-1.13.jar"
},
"devDependencies":{},
"installPaths":{
"hyper":"modules/hyper/",
"yamlbeans-1.13":"lib/yamlbeans-1.13"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment