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

Added "Build With Parameters" #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public static class JenkinsDataLoader
private const short MAX_JOB_BUILDS = 5;

private const string VIEW_QUERY = "api/json?pretty=true&tree=views[name,url]";
private const string JOBS_QUERY_WITH_RANGE = "api/json?pretty=true&tree=jobs[name,url,inQueue,buildable,builds[number,result,building,estimatedDuration,timestamp]{0,5},queueItem[why,id]]";
private const string JOBS_QUERY = "api/json?pretty=true&tree=jobs[name,url,inQueue,buildable,builds[number,result,building,estimatedDuration,timestamp],queueItem[why,id]]";
private const string JOBS_QUERY_BASE = "api/json?pretty=true&tree=jobs[name,url,inQueue,buildable,queueItem[why,id],property[parameterDefinitions[name,defaultParameterValue[value]]],builds[number,result,building,estimatedDuration,timestamp]";
private const string JOBS_QUERY_WITH_RANGE = JOBS_QUERY_BASE + "{0,5}]";
private const string JOBS_QUERY = JOBS_QUERY_BASE + "]";

public static async Task<IEnumerable<JenkinsView>> GetViews(JenkinsServer server)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

Expand Down Expand Up @@ -50,6 +51,25 @@ public async static Task ScheduleJob(string jobUrl, string jenkinsServerUrl)
}
}

public static async Task BuildJobWithDefaultParameters(JenkinsJob job, string jenkinsServerUrl)
{
using (var client = JenkinsDataLoader.CreateJenkinsWebClient(jenkinsServerUrl))
{
Uri buildUri = CreateBuildWithParametersUri(job.Url);
var parametersNameValueCollection = new NameValueCollection();
job.Property.Aggregate(parametersNameValueCollection, (collection, property) =>
{
if (property.ParameterDefinitions != null && property.ParameterDefinitions.Any())
{
property.ParameterDefinitions.ToList().ForEach(pd => collection.Add(pd.Name, pd.DefaultParameterValue.Value));
}
return collection;
});
parametersNameValueCollection.Add("delay", "0sec");
byte[] response = await client.UploadValuesTaskAsync(buildUri, parametersNameValueCollection);
}
}

public async static Task DequeueJob(JenkinsQueueItem queueItem, string jenkinsServerUrl)
{
if (queueItem == null)
Expand Down Expand Up @@ -110,5 +130,11 @@ private static Uri CreateLatestLogUri(string jobUrl)
var jobUri = new Uri(jobUrl);
return new Uri(jobUri, "lastBuild/consoleText");
}

private static Uri CreateBuildWithParametersUri(string jobUrl)
{
var joubUri = new Uri(jobUrl);
return new Uri(joubUri, "buildWithParameters");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@
<Compile Include="Contract\IVisualStudioWindowHandler.cs" />
<Compile Include="Schema\JenkinsBuild.cs" />
<Compile Include="Schema\JenkinsBuildresultConverter.cs" />
<Compile Include="Schema\JenkinsDefaultParameterValue.cs" />
<Compile Include="Schema\JenkinsOverview.cs" />
<Compile Include="Schema\JenkinsParameterDefinition.cs" />
<Compile Include="Schema\JenkinsProperty.cs" />
<Compile Include="Schema\JenkinsQueueItem.cs" />
<Compile Include="Schema\JenkinsServer.cs" />
<Compile Include="Schema\JenkinsServerList.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Devkoes.JenkinsManager.Model.Schema
{
public class JenkinsDefaultParameterValue
{
[JsonProperty("Value")]
public string Value { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class JenkinsJob : ObservableObject
private bool _isQueued;
private JenkinsQueueItem _queueItem;
private string _name;
private IEnumerable<JenkinsProperty> _property;

[JsonProperty("InQueue")]
public bool IsQueued
Expand Down Expand Up @@ -94,6 +95,21 @@ public string Name
[JsonProperty("Url")]
public string Url { get; set; }

[JsonProperty("Property")]
public IEnumerable<JenkinsProperty> Property
{
get { return _property; }
set
{
if (value != _property)
{
_property = value;
RaisePropertyChanged(() => Property);
RaisePropertyChanged(() => HasParameters);
}
}
}

[JsonIgnore]
public JenkinsBuild LatestBuild
{
Expand Down Expand Up @@ -162,6 +178,12 @@ public short BuildProgress
}
}

[JsonIgnore]
public bool HasParameters
{
get { return Property != null && Property.Any(p => p?.ParameterDefinitions != null && p.ParameterDefinitions.Any()); }
}

public override bool Equals(object obj)
{
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json;

namespace Devkoes.JenkinsManager.Model.Schema
{
public class JenkinsParameterDefinition
{
[JsonProperty("Name")]
public string Name { get; set; }

[JsonProperty("DefaultParameterValue")]
public JenkinsDefaultParameterValue DefaultParameterValue { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Devkoes.JenkinsManager.Model.Schema
{
public class JenkinsProperty
{
[JsonProperty("ParameterDefinitions")]
public IEnumerable<JenkinsParameterDefinition> ParameterDefinitions { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\BuildWithParameters.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class JenkinsManagerViewModel : ViewModelBase
public RelayCommand<JenkinsJob> ScheduleBuildCommand { get; private set; }
public RelayCommand<JenkinsJob> CancelBuildCommand { get; private set; }
public RelayCommand<JenkinsJob> DequeueJobCommand { get; private set; }
public RelayCommand<JenkinsJob> BuildJobWithDefaultParametersCommand { get; private set; }

public RelayCommand<JenkinsJob> ShowJobsWebsite { get; private set; }
public RelayCommand<JenkinsJob> LinkJobToCurrentSolution { get; private set; }
Expand Down Expand Up @@ -81,6 +82,8 @@ private void InitializeCommands()
ScheduleBuildCommand = new RelayCommand<JenkinsJob>(ScheduleJob, CanDoJobAction);
CancelBuildCommand = new RelayCommand<JenkinsJob>(CancelBuild, CanDoJobAction);
DequeueJobCommand = new RelayCommand<JenkinsJob>(DequeueBuild, CanDoJobAction);
BuildJobWithDefaultParametersCommand = new RelayCommand<JenkinsJob>(BuildJobWithDefaultParameters, CanDoJobAction);


ShowJobsWebsite = new RelayCommand<JenkinsJob>(ShowWebsite, CanDoJobAction);
ShowLatestLog = new RelayCommand<JenkinsJob>(HandleShowLatestLog, CanDoJobAction);
Expand Down Expand Up @@ -313,6 +316,19 @@ private async void BuildJob(JenkinsJob j)
}
}

private async void BuildJobWithDefaultParameters(JenkinsJob job)
{
try
{
await JenkinsJobManager.BuildJobWithDefaultParameters(job, SelectedJenkinsServer.Url);
ForceReload(false);
}
catch (Exception ex)
{
Logger.Log(ex);
}
}

private async void ScheduleJob(JenkinsJob j)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@
Height="16" />
</MenuItem.Icon>
</MenuItem>
<MenuItem
Header="Build with _Parameters"
Command="{Binding Path=BuildJobWithDefaultParametersCommand}"
CommandParameter="{Binding SelectedJob}"
IsEnabled="{Binding SelectedJob.HasParameters}">
<MenuItem.Icon>
<Image Source="..\Resources\BuildWithParameters.png" Height="16" />
</MenuItem.Icon>
</MenuItem>
<MenuItem
Header="_Cancel build"
Command="{Binding Path=CancelBuildCommand}"
Expand Down