Skip to content
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

Jenkins 13493 - Automatic git cache maintenance #1277

Open
wants to merge 144 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
f3db47a
Init git maintenance
Hrushi20 May 30, 2022
ae4b066
Added basic layout
Hrushi20 May 31, 2022
c1b2d5c
Added help files
Hrushi20 Jun 1, 2022
7cb82bd
added permissions
Hrushi20 Jun 8, 2022
97c3478
Init maintenance config
Hrushi20 Jun 8, 2022
ad0222e
Conditional rendering button in UI
Hrushi20 Jun 9, 2022
2130708
added taskNames and TaskTest
Hrushi20 Jun 10, 2022
d0befd7
FormValidation init
Hrushi20 Jun 10, 2022
c9e298b
Added Form Validation
Hrushi20 Jun 10, 2022
fdc423f
Added checkbox to tasks in UI
Hrushi20 Jun 14, 2022
e414152
Suppress a spotbugs warning
MarkEWaite Jun 15, 2022
95c2bd6
saving data in xml file
Hrushi20 Jun 16, 2022
7c9f3bf
Fixed EI_EXPOSE_REP spotbug error
Hrushi20 Jun 16, 2022
cd36d1e
Loading maintenance data from xml files
Hrushi20 Jun 16, 2022
12e4926
Added jenkinsRule for failing tests
Hrushi20 Jun 16, 2022
a75d13d
init maintenance scheduling
Hrushi20 Jun 17, 2022
a987fa3
added task scheduling
Hrushi20 Jun 17, 2022
ded1f1b
init maintenance task executor
Hrushi20 Jun 17, 2022
634c486
added permissions and git version in UI
Hrushi20 Jun 18, 2022
ad7e603
Use JenkinsRule in TaskSchedulerTest
MarkEWaite Jun 19, 2022
a88800c
Make checkIsTaskInQueue testable (pacakge protected)
MarkEWaite Jun 19, 2022
0562df7
Add a few task tests
MarkEWaite Jun 19, 2022
2e0d8db
Fix spotbugs warnings
MarkEWaite Jun 19, 2022
a0cbc63
Added tests to TaskScheduler
Hrushi20 Jun 19, 2022
ff425c6
updated logic in taskExecutor
Hrushi20 Jun 19, 2022
ff5b49d
getAllCaches on controller added
Hrushi20 Jun 20, 2022
078dfb4
added gitClient initialization
Hrushi20 Jun 20, 2022
a23f395
added gitClient initialization
Hrushi20 Jun 20, 2022
b7c3f1e
attempt to fix spotbug issue
Hrushi20 Jun 20, 2022
9a6eadb
Depend on incremental git client build
MarkEWaite Jun 22, 2022
a311417
updated verion of git-client plugin
Hrushi20 Jun 22, 2022
a432fad
Revert "updated verion of git-client plugin"
MarkEWaite Jun 22, 2022
6938415
logic to execute maintenance tasks
Hrushi20 Jun 23, 2022
10b92ba
Retrieve cacheEntries
Hrushi20 Jun 23, 2022
f5c8b99
Init execution of maintenance tasks using locks
Hrushi20 Jun 23, 2022
52d4c07
init tests for maintenance task execution
Hrushi20 Jun 23, 2022
45b1a64
fixed NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE spotbug issue
Hrushi20 Jun 23, 2022
79ee4ab
added Parameterized class to TestExecutorTest
Hrushi20 Jun 23, 2022
ec598ba
test to create executor thread
Hrushi20 Jun 23, 2022
149550f
removed synchronization on queue
Hrushi20 Jun 23, 2022
b9a5fdb
tests for fetching git caches on jenkins controller
Hrushi20 Jun 25, 2022
206427c
added taskExecutorTests
Hrushi20 Jun 26, 2022
80c5f01
invalid cronSyntax is stored in file
Hrushi20 Jun 28, 2022
2655691
verifying cron syntax during execution of maintenance tasks
Hrushi20 Jun 28, 2022
27da7b4
maintain order of maintenance task
Hrushi20 Jun 28, 2022
b8d1c2b
added logs to MaintenanceUI
Hrushi20 Jun 29, 2022
cedc797
Added logging to MaintenanceTaskConfiguration
Hrushi20 Jun 30, 2022
31eb4c2
added logging to TaskScheduler
Hrushi20 Jun 30, 2022
4908181
Logging for TaskExecutor added
Hrushi20 Jun 30, 2022
8d8f4e9
fetch git version in maintenance UI
Hrushi20 Jul 2, 2022
8829576
package protected getGitVersion
Hrushi20 Jul 3, 2022
d451ffc
Executing maintenance tasks only using CliGit
Hrushi20 Jul 3, 2022
b3cbab2
Added LogTaskListener
Hrushi20 Jul 3, 2022
efaf644
Logic to terminate TaskExecutorThread and clear MaintenanceQueue
Hrushi20 Jul 6, 2022
b877793
Logging caches on Controller to debug tmp caches
Hrushi20 Jul 6, 2022
3aea0d0
updated logs
Hrushi20 Jul 7, 2022
1e3dc7a
init legacy git maintenance
Hrushi20 Jul 7, 2022
8080639
added tests and made few fields private
Hrushi20 Jul 7, 2022
cfca99c
updated logic to not create caches by GitMaintenanceSCM
Hrushi20 Jul 7, 2022
536de07
updated logic to execute git maintenance legacy
Hrushi20 Jul 8, 2022
1b97858
added few tests TaskExecutor
Hrushi20 Jul 8, 2022
70d547c
Added hash to CronTab
Hrushi20 Jul 9, 2022
9b56558
added/updated tests
Hrushi20 Jul 10, 2022
06e400a
Use latest git client plugin
MarkEWaite Jul 12, 2022
3517854
Fix spotbugs warning for randomization in TaskScheduler
MarkEWaite Jul 12, 2022
46e2a96
Fix spots warning for random generator
MarkEWaite Jul 12, 2022
81a84f2
Fix spotbugs warning for character set encoding
MarkEWaite Jul 13, 2022
e40ca2d
Improve git version assertions
MarkEWaite Jul 13, 2022
16fd180
Use hamcrest assertions for readability
MarkEWaite Jul 13, 2022
08cf2a4
Use random, not RANDOM for static final object
MarkEWaite Jul 13, 2022
53411d5
Fix test that I broke earlier
MarkEWaite Jul 13, 2022
2254d1d
Prevent NPE in test iteration of caches
MarkEWaite Jul 13, 2022
4eb5db7
Avoid NPE in task executor getClient()
MarkEWaite Jul 13, 2022
24c8a8a
Efficient way of terminating maintenance thread
Hrushi20 Jul 13, 2022
a20c50c
removed assert syntax
Hrushi20 Jul 14, 2022
ba20351
fixed bug which adds invalid cache directory
Hrushi20 Jul 14, 2022
7574ac3
terminate thread when executing invalid maintenance task
Hrushi20 Jul 14, 2022
55fd7ee
Added notifications in UI
Hrushi20 Jul 29, 2022
f42d664
Skip caches when locked
Hrushi20 Aug 2, 2022
890a1d5
Added more tests, fixed spotbug issue
Hrushi20 Aug 13, 2022
b1f75af
xml storage logic for maintenance tasks
Hrushi20 Aug 15, 2022
a0916a5
Removed jgit version.
Hrushi20 Aug 16, 2022
3137c98
init table creation in UI
Hrushi20 Aug 16, 2022
2ea52fc
Storing maintenance data into xml file
Hrushi20 Aug 17, 2022
a495f9a
displaying data in UI
Hrushi20 Aug 17, 2022
1d4f766
fixed spotbug issue
Hrushi20 Aug 17, 2022
28c496f
Refactor code
Hrushi20 Aug 18, 2022
4753426
insert data to front of linked list
Hrushi20 Aug 19, 2022
c201e0b
store records based on caches
Hrushi20 Aug 25, 2022
24729e3
Added collapse and expand feature in table
Hrushi20 Aug 25, 2022
e16f547
Storing 5 records for each maintenance task
Hrushi20 Aug 25, 2022
4f6eba1
fixed bug cronVerification due to calender
Hrushi20 Aug 25, 2022
a8d9570
update top most maintenance data for a cache
Hrushi20 Aug 25, 2022
ed177e9
added serial no. to table
Hrushi20 Aug 25, 2022
39a5d6a
remove unwanted code
Hrushi20 Aug 25, 2022
89e0212
fixed missing record in table
Hrushi20 Aug 25, 2022
3e4a94e
added xml file path
Hrushi20 Aug 26, 2022
249e1e4
Improve performance by reading data once from xml file
Hrushi20 Aug 26, 2022
da7a6bc
Added logs to XmlSerialize.java
Hrushi20 Aug 26, 2022
6509ef9
changed logging level
Hrushi20 Aug 26, 2022
6061711
added tests to CacheRecord
Hrushi20 Aug 26, 2022
1d7e094
fixed failing test
Hrushi20 Aug 27, 2022
4954fdf
changed incorret order of assertEquals
Hrushi20 Aug 27, 2022
c0bf7c9
added RecordListTest
Hrushi20 Aug 27, 2022
0a703c7
fixed null name check error
Hrushi20 Aug 27, 2022
6cf7ca1
updated index while checking sort in test file
Hrushi20 Aug 27, 2022
aea6718
javadoc Task.java
Hrushi20 Sep 2, 2022
dab58c0
javadoc MaintenanceTaskConfiguration.java
Hrushi20 Sep 2, 2022
48c8b41
javadoc TaskType.java
Hrushi20 Sep 2, 2022
42f9cd4
Use most recent git client incremental
MarkEWaite Sep 3, 2022
ce68d14
updated gc to garbage collection
Hrushi20 Sep 2, 2022
aa27fc7
fixed expand btn in table
Hrushi20 Sep 5, 2022
89865b7
javadoc TaskScheduler.java
Hrushi20 Sep 5, 2022
b06a83c
javadoc TaskExecutor.java
Hrushi20 Sep 5, 2022
72d1a4d
javadoc GitMaintenanceSCM.java
Hrushi20 Sep 5, 2022
9058f21
updated missing javadoc
Hrushi20 Sep 5, 2022
7260b77
check null pointer exception
Hrushi20 Sep 5, 2022
bb21cab
UI fixes
Hrushi20 Sep 5, 2022
cffbe4b
added format for execution time in UI
Hrushi20 Sep 5, 2022
4da3432
Removed git maintenance legacy api
Hrushi20 Sep 6, 2022
8240acf
removed gitVersionAtLeast method
Hrushi20 Sep 6, 2022
e21ea2b
Remove xstream dependency, not required
MarkEWaite Sep 7, 2022
db03e75
Annotate argument as NonNull
MarkEWaite Sep 7, 2022
c0d3e98
Remove reordering of imports
MarkEWaite Sep 7, 2022
2bd9213
Use correct case in middle of sentence
MarkEWaite Sep 7, 2022
f84a4ee
Use heading text consistent with Jenkins configuration
MarkEWaite Sep 7, 2022
c889e96
updated execution status using gitClient
Hrushi20 Sep 7, 2022
55ed559
storing size of cache in Bytes
Hrushi20 Sep 7, 2022
5aa22bc
displaying cacheSize in MB only
Hrushi20 Sep 7, 2022
01e27c3
update git-client incremental version
Hrushi20 Sep 8, 2022
83044e5
Require git client plugin 3.12.0
MarkEWaite Sep 19, 2022
3c1a64a
Skipping caches whose size less than 10 MB
Hrushi20 Oct 1, 2022
cc5c990
Minor formatting change
MarkEWaite Oct 22, 2022
215f75c
Only log cache reads at FINE level
MarkEWaite Oct 22, 2022
c949c45
Remove System.out.println calls
MarkEWaite Oct 22, 2022
5499217
Update test TaskExecutor
Jul 30, 2023
7245206
Update TaskSchedulerTest
Jul 30, 2023
f728ae1
updated GitMaintenanceSCMTest
Jul 30, 2023
8d72be3
Replace LinkedList with ArrayList
Jul 31, 2023
3c3a39c
attempt to fix incremental-repack test
Hrushi20 Oct 27, 2023
026aa92
extend lifetime of execution thread unit test
Hrushi20 Oct 27, 2023
e266d16
Fixed spotbug issue, update pom.xml
Hrushi20 Oct 28, 2023
c0ff128
Fix javadoc error
Hrushi20 Oct 28, 2023
b1bd121
Merge branch 'jenkinsci:master' into jenkins-13493
Hrushi20 Aug 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@
<artifactId>mailer</artifactId>
</dependency>

<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>data-tables-api</artifactId>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public Object readResolve() throws IOException, GitException {
if (remoteRepositories != null && userRemoteConfigs == null) {
userRemoteConfigs = new ArrayList<>();
for(RemoteConfig cfg : remoteRepositories) {
// converted as in config.jelly
// converted as in index.jelly
String url = "";
if (cfg.getURIs().size() > 0 && cfg.getURIs().get(0) != null)
url = cfg.getURIs().get(0).toPrivateString();
Expand Down
24 changes: 23 additions & 1 deletion src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@

private static final Logger LOGGER = Logger.getLogger(AbstractGitSCMSource.class.getName());

static Set<String> cacheEntries = new HashSet<>();

static {
Jenkins jenkins = Jenkins.getInstanceOrNull();
if(jenkins != null){
File[] caches = new File(jenkins.getRootDir(),"caches").listFiles();
if(caches != null) {

Check warning on line 177 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 177 is only partially covered, one branch is missing
for (File cache : caches) {
String cacheEntry = cache.getName();
cacheEntries.add(cacheEntry);

Check warning on line 180 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 178-180 are not covered by tests
}
}
LOGGER.log(Level.FINE,"Caches on Jenkins controller " + cacheEntries);
}
}

public AbstractGitSCMSource() {
}

Expand Down Expand Up @@ -1284,6 +1300,10 @@
return getCacheEntry(getRemote());
}

protected static Set<String> getCacheEntries(){
return cacheEntries;
}

protected static File getCacheDir(String cacheEntry) {
return getCacheDir(cacheEntry, true);
}
Expand All @@ -1298,7 +1318,9 @@
if (!cacheDir.isDirectory()) {
if (createDirectory) {
boolean ok = cacheDir.mkdirs();
if (!ok) {
if(ok) {

Check warning on line 1321 in src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1321 is only partially covered, one branch is missing
cacheEntries.add(cacheEntry);
}else{
LOGGER.log(Level.WARNING, "Failed mkdirs of {0}", cacheDir);
}
} else {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/jenkins/plugins/git/maintenance/Cron.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jenkins.plugins.git.maintenance;

import hudson.Extension;
import hudson.model.PeriodicWork;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

@Extension
public class Cron extends PeriodicWork {

private TaskScheduler taskScheduler;

@Override
public long getInitialDelay(){
return MIN - (Calendar.getInstance().get(Calendar.SECOND) * 1000L);
}

@Override
public long getRecurrencePeriod() {
return TimeUnit.MINUTES.toMillis(1);
}

@Override
protected void doRun(){
scheduleMaintenanceTask();
}

void terminateMaintenanceTaskExecution(){
if(taskScheduler != null){
taskScheduler.terminateMaintenanceTaskExecution();
}
}

Check warning on line 33 in src/main/java/jenkins/plugins/git/maintenance/Cron.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 30-33 are not covered by tests

private void scheduleMaintenanceTask(){
if(taskScheduler == null){

Check warning on line 36 in src/main/java/jenkins/plugins/git/maintenance/Cron.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 36 is only partially covered, one branch is missing
taskScheduler = new TaskScheduler();
}

taskScheduler.scheduleTasks();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package jenkins.plugins.git.maintenance;

import jenkins.model.Jenkins;
import jenkins.plugins.git.AbstractGitSCMSource;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* GitMaintenanceSCM is responsible for fetching all caches along with locks on Jenkins controller. It extends {@link AbstractGitSCMSource}.
*
* @author Hrushikesh Rao
*/
public class GitMaintenanceSCM extends AbstractGitSCMSource {

String remote;

private static Logger LOGGER = Logger.getLogger(GitMaintenanceSCM.class.getName());
protected GitMaintenanceSCM(String remote){
this.remote = remote;
}

Check warning on line 26 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 24-26 are not covered by tests

/**
* Stores the File object and lock for cache.
*/
static class Cache {

File cache;
Lock lock;
Cache(File cache, Lock lock){
this.cache = cache;
this.lock = lock;
}

/**
* Return the File object of a cache.
* @return File object of a cache.
*/
public File getCacheFile(){
return cache;
}

/**
* Returns the lock for a cache.
*
* @return lock for a cache.
*/
public Lock getLock(){
return lock;
}

}
@Override
public String getCredentialsId() {
return null;
}

@Override
public String getRemote() {
return remote;

Check warning on line 65 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 60-65 are not covered by tests
}

/**
* Returns a list of {@link Cache}.
* @return A list of {@link Cache}.
*/
public static List<Cache> getCaches(){
Jenkins jenkins = Jenkins.getInstanceOrNull();

List<Cache> caches = new ArrayList<>();
if (jenkins == null){

Check warning on line 76 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 76 is only partially covered, one branch is missing
LOGGER.log(Level.WARNING,"Internal error. Couldn't get Jenkins instance.");
return caches;

Check warning on line 78 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 77-78 are not covered by tests
}

for (String cacheEntry : getCacheEntries()) {

Check warning on line 81 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 81 is only partially covered, one branch is missing
File cacheDir = getCacheDir(cacheEntry,false);
Lock cacheLock = getCacheLock(cacheEntry);

// skip caches size less than 10 mb
if(FileUtils.sizeOfDirectory(cacheDir) < 10000000)continue;
LOGGER.log(Level.FINE,"Cache Entry " + cacheEntry);
caches.add(new Cache(cacheDir,cacheLock));
}

Check warning on line 89 in src/main/java/jenkins/plugins/git/maintenance/GitMaintenanceSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 82-89 are not covered by tests

return caches;
}
}
116 changes: 116 additions & 0 deletions src/main/java/jenkins/plugins/git/maintenance/Logs/CacheRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package jenkins.plugins.git.maintenance.Logs;

import jenkins.plugins.git.maintenance.TaskType;
import org.apache.commons.lang.time.DurationFormatUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CacheRecord {
String repoName;
long repoSize;
String maintenanceType;
long timeOfExecution;
boolean executionStatus;
long executionDuration;

Map<String,List<CacheRecord>> maintenanceData;


// This is to create a new Cache Record when cache is not present.
public CacheRecord(String repoName,String maintenanceType){
this.repoName = repoName;
this.maintenanceType = maintenanceType;
maintenanceData = new HashMap<>();

for(TaskType taskType : TaskType.values()){
maintenanceData.put(taskType.getTaskName(),new LinkedList<>());
}
}

// This is to add maintenance data to existing Cache Record
public CacheRecord(CacheRecord cacheRecord){
setExecutionDuration(cacheRecord.executionDuration);
setExecutionStatus(cacheRecord.getExecutionStatus());
setRepoSize(cacheRecord.getRepoSize());
setTimeOfExecution(cacheRecord.timeOfExecution);
setMaintenanceType(cacheRecord.getMaintenanceType());
}


public String getRepoName() {
return repoName;
}

public long getRepoSize() {
return repoSize;
}
public void setRepoSize(long repoSize) {
this.repoSize = repoSize;
}

public String getMaintenanceType() {
return maintenanceType;
}

public void setMaintenanceType(String maintenanceType){
this.maintenanceType = maintenanceType;
}

public String getTimeOfExecution() {
Date date = new Date(timeOfExecution * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
return sdf.format(date);
}

public void setTimeOfExecution(long timeOfExecution) {
this.timeOfExecution = timeOfExecution;
}

public boolean getExecutionStatus() {
return executionStatus;
}

public void setExecutionStatus(boolean executionStatus) {
this.executionStatus = executionStatus;
}

public String getExecutionDuration() {
return DurationFormatUtils.formatDuration(executionDuration,"mmm:ss:SSS");
}

public void setExecutionDuration(long executionDuration) {
this.executionDuration = executionDuration;
}

public void insertMaintenanceData(CacheRecord record){
if(record != null && maintenanceData != null) {

Check warning on line 93 in src/main/java/jenkins/plugins/git/maintenance/Logs/CacheRecord.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 93 is only partially covered, 2 branches are missing
List<CacheRecord> list = maintenanceData.get(record.getMaintenanceType());
if(list != null) {

Check warning on line 95 in src/main/java/jenkins/plugins/git/maintenance/Logs/CacheRecord.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 95 is only partially covered, one branch is missing
list.add(0,record);
// Maximum storage of 5 Maintenance Records per Cache.
if (list.size() > 5)

Check warning on line 98 in src/main/java/jenkins/plugins/git/maintenance/Logs/CacheRecord.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 98 is only partially covered, one branch is missing
list.remove(list.size()-1);

Check warning on line 99 in src/main/java/jenkins/plugins/git/maintenance/Logs/CacheRecord.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 99 is not covered by tests
}
}
}

public List<CacheRecord> getAllMaintenanceRecordsForSingleCache(){
List<CacheRecord> maintenanceData = new ArrayList<>();

for(Map.Entry<String,List<CacheRecord>> entry : this.maintenanceData.entrySet()){
maintenanceData.addAll(entry.getValue());
}

Collections.sort(maintenanceData,(o1,o2) -> (int) (o2.timeOfExecution - o1.timeOfExecution));

return maintenanceData;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package jenkins.plugins.git.maintenance.Logs;

import java.util.*;

public class RecordList {
List<CacheRecord> maintenanceRecords;
Set<String> cacheSet;

public RecordList(){
maintenanceRecords = new ArrayList<>();
cacheSet = new HashSet<>();
}

List<CacheRecord> getMaintenanceRecords(){
return new ArrayList<>(maintenanceRecords);
}

void addRecord(CacheRecord cacheRecord){
String repoName = cacheRecord.getRepoName();
Set<String> cacheSet = getCacheSet();
if(cacheSet.contains(repoName)){
// adding record to existing cache list
Iterator<CacheRecord> itr = maintenanceRecords.iterator();

CacheRecord record;
while(itr.hasNext()){

Check warning on line 26 in src/main/java/jenkins/plugins/git/maintenance/Logs/RecordList.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 26 is only partially covered, one branch is missing
record = itr.next();
if(record.getRepoName().equals(repoName)){

Check warning on line 28 in src/main/java/jenkins/plugins/git/maintenance/Logs/RecordList.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 28 is only partially covered, one branch is missing

// To not lose data of the first maintenance task
if(record.getAllMaintenanceRecordsForSingleCache().size() == 0){
CacheRecord oldCacheRecord = new CacheRecord(record);
record.insertMaintenanceData(oldCacheRecord);
}
CacheRecord childCacheRecord = new CacheRecord(cacheRecord);

record.insertMaintenanceData(childCacheRecord);

// Updates the Top most Cache with fresh data
record.setTimeOfExecution(childCacheRecord.timeOfExecution);
record.setExecutionStatus(childCacheRecord.getExecutionStatus());
record.setRepoSize(childCacheRecord.getRepoSize());
record.setMaintenanceType(childCacheRecord.getMaintenanceType());
record.setExecutionDuration(childCacheRecord.executionDuration);

break;
}
}
return;
}

// Creates a new Cache Entry and adds the data.
maintenanceRecords.add(0,cacheRecord);
cacheSet.add(repoName);
}

List<CacheRecord> getAllMaintenanceRecordsForSingleCache(String cacheName) {
List<CacheRecord> allRecords = null;

Iterator<CacheRecord> itr = maintenanceRecords.iterator();

CacheRecord record;
while(itr.hasNext()){

Check warning on line 63 in src/main/java/jenkins/plugins/git/maintenance/Logs/RecordList.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 63 is only partially covered, one branch is missing
record = itr.next();
if(record.getRepoName().equals(cacheName)){

Check warning on line 65 in src/main/java/jenkins/plugins/git/maintenance/Logs/RecordList.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 65 is only partially covered, one branch is missing
allRecords = record.getAllMaintenanceRecordsForSingleCache();
break;
}
}

return allRecords;
}

Set<String> getCacheSet(){
return this.cacheSet;
}
}
Loading
Loading