-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArtifactoryActionBase.cs
149 lines (130 loc) · 4.97 KB
/
ArtifactoryActionBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
using System.Text;
using System.IO;
using System.Linq;
using Inedo.BuildMaster;
using Inedo.BuildMaster.Extensibility.Actions;
using Inedo.BuildMaster.Extensibility.Agents;
namespace Inedo.BuildMasterExtensions.Artifactory
{
public abstract class ArtifactoryActionBase : RemoteActionBase
{
internal protected enum RequestType { Get, Post, Delete, Put };
[Persistent]
public string ActionUsername { get; set; }
[Persistent]
public string ActionPassword { get; set; }
[Persistent]
public string ActionServer { get; set; }
[Persistent]
public string RepositoryKey { get; set; }
public bool UsesRepositoryKey { get; set; }
public bool ForceAuthorizationHeader { get; set; }
public ArtifactoryConfigurer TestConfigurer { get; set; }
protected ArtifactoryConfigurer Configurer
{
get
{
//return this.GetExtensionConfigurer() as ArtifactoryConfigurer;
// For Test Creation
if (null != TestConfigurer)
return TestConfigurer;
else
return this.GetExtensionConfigurer() as ArtifactoryConfigurer;
//return Util.Actions.GetConfigurer(GetType()) as ArtifactoryConfigurer;
}
}
protected Authentication Credentials
{
get
{
// use local first
if (null != ActionUsername )
return new Authentication() {Username = ActionUsername, Password = ActionPassword } ;
return new Authentication() { Username = Configurer.Username, Password = Configurer.Password };
}
}
protected string Server
{
get
{
// use local first
if (null != ActionServer)
return ActionServer;
return Configurer.Server;
}
}
protected string ResolveDirectory(string FilePath)
{
using (var sourceAgent2 = Util.Agents.CreateLocalAgent())
{
var sourceAgent = sourceAgent2.GetService<IFileOperationsExecuter>();
char srcSeparator = sourceAgent.GetDirectorySeparator();
var srcPath = sourceAgent.GetWorkingDirectory(this.Context.ApplicationId, this.Context.DeployableId ?? 0, FilePath);
LogInformation("Source Path: " + srcPath);
return srcPath;
}
}
internal Response Request(RequestType RequestType, string Payload, string UriFormat, params object[] args)
{
Response retval = new Response();
var uri = new Uri(string.Format(UriFormat, args));
var req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Credentials = new NetworkCredential(this.Credentials.Username, this.Credentials.Password);
switch (RequestType)
{
case ArtifactoryActionBase.RequestType.Get:
req.Method = "GET";
break;
case ArtifactoryActionBase.RequestType.Post:
req.Method = "POST";
break;
case ArtifactoryActionBase.RequestType.Delete:
req.Method = "DELETE";
break;
case ArtifactoryActionBase.RequestType.Put:
req.Method = "PUT";
break;
default:
req.Method = "GET";
break;
}
if (this.ForceAuthorizationHeader)
{
string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(this.Credentials.Username + ":" + this.Credentials.Password));
req.Headers.Add("Authorization", "Basic " + credentials);
}
req.ContentType = "application/json";
if (!string.IsNullOrEmpty(Payload))
{
var buffer = Encoding.UTF8.GetBytes(Payload);
req.ContentLength = buffer.Length;
Stream reqStream = req.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException ex)
{
resp = (HttpWebResponse)ex.Response;
}
retval.StatusCode = resp.StatusCode;
retval.Headers = resp.Headers;
if (resp.ContentLength > 0)
{
StreamReader reader = new StreamReader(resp.GetResponseStream());
retval.Document = reader.ReadToEnd();
}
return retval;
}
}
}