Using Jenkinsfile to Modify an existing text file during build process - jenkins

I'm very new to modifying jenkinsfiles and would appreciate some pointers.
I want to be able to modify an existing txt file in my repository with the detail of the changes made as part of that checkin. I've got the list of changes by using the below:
def getChangeLog() {
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
echo " ${file.editType.name} ${file.path}"
}
}
}
}
But then need to somehow be able to write this to an existing txt file before the build triggers. I also need to be able to read in the file, and confirm that the changes do exist there as well as to not insert duplicate entries.
All of this needs to be done in the Jenkinsfile somehow.
I've been able to find a lot of addons which do something like this but nothing that points to how to implement it in the Jenkinsfile itself.

Related

dart: changing list[i] changes list[i-1] too

I have a List of the type Model. when I loop all its elements and loop the next one except for the last one, then change the last one manually, the one before changes.
here is a little code to reproduce the problem (also you can run it directly in dartpad from here)
void main() {
List<Model> s = [];
for (int i = 0; i < 5; i++) {
s.add(Model(i));
}
for (int i = 0; i < s.length - 1; i++) {
s[i] = s[i + 1];
}
print(s);
s[s.length-1].x = 100;
print(s);
}
class Model {
int x;
Model(this.x);
#override
String toString() => 'x: ' + this.x.toString();
}
notice that this problem does not happen when you comment out the for loop or the manual change, or instead of changing the last one's property, you reassign a new value to it, like s[s.length - 1] = Model(100);. seems like dart for some reason is re-running the loop.
When you run the second for loop, you assign the i + 1th Model to the ith position in the list:
// initialise list
for (int i = 0; i < s.length; i++) {
s[i] = s[i + 1]
}
If you unwrap the loop, it looks roughly like this:
s[0] = s[1];
s[1] = s[2];
s[2] = s[3];
s[3] = s[4];
Notice that this leaves s[4] unchanged, but also assigns it to s[3].
In Dart, variables contain references to objects. This means that when your list runs s[3] = s[4];, both s[3] and s[4] point to the same object.
Now, if you modify s[4] you, are actually modifying the objects that s[4] refers to, which happens to also be the object that s[3] refers to, so they both appear to change in your list.

How to get list of changed files in Jenkinsfile for a pull request?

Is it possible to get a list of changed files in the Jenkinsfile during a pull request build? I am currently doing this...
def changeLogSets = currentBuild.rawBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
print file.path
}
}
}
but if I am building a new branch for the very first time this method does not return any changes because there is no previous build to compare to. Has anyone found a solution to this?
Thanks
As far as I know there's no built-in functionality to do this.
Fortunately, you can find that out programmatically:
def local_branch = sh (
script: "git rev-parse --abbrev-ref HEAD",
label: "Getting current branch name",
returnStdout: true
).trim()
println "Local branch is ${local_branch}"
def base_branch = 'master'
// This is very naive.
// In reality, you need a better way to find out what your base branch is.
// One way is to have a file with a name of a base branch.
// Another one is to invoke API, e.g. GitHub API, to find out base branch.
// Use whatever works for you.
println "Base branch is ${base_branch}"
sh script: "git fetch origin --no-tags ${base_branch}", label: "Getting base branch"
def git_diff = sh (
script: "git diff --name-only origin/${base_branch}..${local_branch}",
returnStdout: true
).trim()
Now you have a list of changed files in the git_diff variable.
Using Jenkins changesets is mostly not working, and is reserved for other purposes anyhow.
This can also be achieved with the pipeline-github plugin:
def changedFiles = pullRequest.files.collect {
it.getFilename()
}

FPDFText_SetText change nothing

I can change the text of an pdf text object and pdfium function says "all is ok". But when i save the changed pdf doc no changes are visible. Something is missing and i dont know what.
int co = FPDFPage_CountObjects(page);
for (int j = 0; j < co; ++j) {
FPDF_PAGEOBJECT pobj = FPDFPage_GetObject(page, j);
if (FPDFPageObj_GetType(pobj) == FPDF_PAGEOBJ_TEXT) {
...
if (FPDFText_SetText(pobj, (FPDF_WIDESTRING)L"New Text")) {
std::cout << "#VAL1 was changed\n";
//FPDFPage_GenerateContent(page);
}
}
}
...
FPDF_ClosePage(page);
...
FPDF_FILEWRITE_EX fw;
fw.pFile = fopen("C:\\work\\newpdf.pdf", "wb");
fw.version = 1;
fw.WriteBlock = MyDelegateSaveFunc;
FPDF_SaveAsCopy(doc, &fw, FPDF_NO_INCREMENTAL);
fclose(fw.pFile);
FPDF_CloseDocument(doc);
If i do call GenerateContent (not needed i think) then the whole saved page is empty. I do use the last pdfium binary version 3764.
You need to uncomment the line FPDFPage_GenerateContent(page), after changing the object you need to update the object or generate the whole page.
An empty page after FPDFPage_GenerateContent is a bug already fixed in last builds.

For Loop to call multiple variables from properties file

I have a properties file which I call inside my Jenkins Pipeline Script to get multiple variables.
BuildCounter = n
BuildName1 = Name 1
BuildName2 = Name 2
...
Buildnamen = Name n
I call my properties file with: def props = readProperties file: Path
Now I want to create a loop to print all my BuildNames
for (i = 0; i < BuildJobCounterInt; i++){
tmp = 'BuildName' + i+1
println props.tmp
}
But of course this is not working. ne last println call is searching for a variable called tmp in my properties file. Is there a way to perform this or am I completely wrong?
EDIT:
This is my .properties file:
BuildJobCounter = 1
BuildName1 = 'Win32'
BuildPath1 = '_Build/MBE3_Win32'
BuildName2 = 'empty'
BuildPath2 = 'empty'
TestJobCounter = '0'
TestName1 = 'empty'
TestPath1 = 'empty'
TestName2 = 'empty'
TestPath2 = 'empty'
In my Jenkins pipeline I want to have the possibility to check the ammount of Build/TestJobs and automatically calle the Jobs (each BuildName and BuildPath is a Freestyle Job) To call all these Job I thought of calling the variables inside a for loop. So for every istep I have the Name/Path pair.
Try the below:
Change from:
println props.tmp
To:
println props[tmp]
or
println props."$tmp"
EDIT : based on OP comment
change from:
tmp = 'BuildName' + i+1
To:
def tmp = "BuildName${(i+1).toString()}"

Box2DWeb Destroy body and fixture

I have created a list of the bodies and fixtures, everything works fine, but cannot delete objects by some reason, here is my code ("id" is the id of the div that is linked to a body and fixture, that info is being holding in the body and fixture data): HEEEELLPPPPP!!!
var bodyItem;
for(var j=0; j < bodieList.length; j++){
if (bodieList[j].userData.id == id) bodyItem = bodieList[j];
};
//world.DestroyBody (bodyItem);
var fixItem;
for(var i=0; i < fixtureList.length; i++){
//console.log (fixtureList[i].userData.id);
if (fixtureList[i].userData.id == id) fixItem = fixtureList[i];
};
bodyItem.DestroyFixture(fixItem);
world.DestroyBody(bodyItem);

Resources