public async Task <IEnumerable <string> > GetSuggestionsAsync(IComponentConfiguration config) { var credentialName = config["CredentialName"]; var jobName = config["JobName"]; if (string.IsNullOrEmpty(credentialName) || string.IsNullOrEmpty(jobName)) { return(Enumerable.Empty <string>()); } var branchName = config["BranchName"]; string buildNumber = AH.CoalesceString(config["BuildNumber"], "lastSuccessfulBuild"); int?projectId = AH.ParseInt(AH.CoalesceString(config["ProjectId"], config["ApplicationId"])); int?environmentId = AH.ParseInt(config["EnvironmentId"]); var credentials = (JenkinsLegacyCredentials)ResourceCredentials.TryCreate(JenkinsLegacyCredentials.TypeName, credentialName, environmentId: environmentId, applicationId: projectId, inheritFromParent: false); if (credentials == null) { return(Enumerable.Empty <string>()); } using (var cts = new CancellationTokenSource(new TimeSpan(0, 0, 30))) { var client = new JenkinsClient(credentials.UserName, credentials.Password, credentials.ServerUrl, false, null, cts.Token); return((await client.GetBuildArtifactsAsync(jobName, branchName, buildNumber)) .Select(a => a.RelativePath) .ToList()); } }
private Task <string> ResolveJenkinsBuildNumber() { if (AH.ParseInt(this.BuildNumber) != null) { return(Task.FromResult(this.BuildNumber)); } this.Logger.LogDebug($"Build number is not an integer, resolving special build number \"{this.BuildNumber}\"..."); var client = new JenkinsClient(this.ConnectionInfo, this.Logger); return(client.GetSpecialBuildNumberAsync(this.JobName, this.BuildNumber)); }
public async Task <IEnumerable <string> > GetSuggestionsAsync(IComponentConfiguration config) { var credentialName = config["CredentialName"]; if (string.IsNullOrEmpty(credentialName)) { return(Enumerable.Empty <string>()); } var credentials = ResourceCredentials.Create <JenkinsCredentials>(credentialName); var client = new JenkinsClient(credentials); var jobs = await client.GetJobNamesAsync().ConfigureAwait(false); return(jobs); }
public async Task <IEnumerable <string> > GetSuggestionsAsync(IComponentConfiguration config) { var credentialName = config["CredentialName"]; var jobName = config["JobName"]; if (string.IsNullOrEmpty(credentialName) || string.IsNullOrEmpty(jobName)) { return(Enumerable.Empty <string>()); } string buildNumber = AH.CoalesceString(config["BuildNumber"], "lastSuccessfulBuild"); var credentials = ResourceCredentials.Create <JenkinsCredentials>(credentialName); var client = new JenkinsClient(credentials); var artifacts = await client.GetBuildArtifactsAsync(jobName, buildNumber).ConfigureAwait(false); return(artifacts.Select(a => a.RelativePath)); }
public static JenkinsClient CreateClient(this ISuggestionProvider s, IComponentConfiguration config, CancellationToken token) { if (s == null) { return(null); } var(c, r) = config.GetCredentialsAndResource(); var up = c as Extensions.Credentials.UsernamePasswordCredentials; var api = c as Extensions.Credentials.TokenCredentials; var client = new JenkinsClient( up?.UserName, up?.Password ?? api?.Token, r?.ServerUrl, csrfProtectionEnabled: false, null, token ); return(client); }
public async Task <string> ImportAsync() { this.Logger.LogInformation($"Importing artifact \"{this.ArtifactName}\" from Jenkins..."); string zipFileName = null; string jenkinsBuildNumber = await this.ResolveJenkinsBuildNumber().ConfigureAwait(false); if (string.IsNullOrEmpty(jenkinsBuildNumber)) { this.Logger.LogError($"An error occurred attempting to resolve Jenkins build number \"{this.BuildNumber}\". " + $"This can mean that the special build type was not found, there are no builds for job \"{this.JobName}\", " + "or that the job was not found or is disabled." ); return(null); } try { this.Logger.LogInformation($"Importing {this.ArtifactName} from {this.JobName}..."); var client = new JenkinsClient(this.ConnectionInfo, this.Logger); zipFileName = Path.GetTempFileName(); this.Logger.LogDebug("Temp file: " + zipFileName); this.Logger.LogDebug("Downloading artifact..."); await client.DownloadArtifactAsync(this.JobName, jenkinsBuildNumber, zipFileName).ConfigureAwait(false); this.Logger.LogInformation("Artifact downloaded."); using (var file = FileEx.Open(zipFileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { await SDK.CreateArtifactAsync( (int)this.Context.ApplicationId, this.Context.ReleaseNumber, this.Context.BuildNumber, this.Context.DeployableId, this.Context.ExecutionId, TrimWhitespaceAndZipExtension(this.ArtifactName), file, true ).ConfigureAwait(false); } } finally { try { if (zipFileName != null) { this.Logger.LogDebug("Removing temp file..."); FileEx.Delete(zipFileName); } } catch (Exception ex) { this.Logger.LogWarning("Error deleting temp file:" + ex.Message); } } this.Logger.LogInformation(this.ArtifactName + " artifact imported."); return(jenkinsBuildNumber); }