~haakan/do-plugins/sshfix

« back to all changes in this revision

Viewing changes to JIRA/src/Remote/JIRARssClient.cs

  • Committer: Jason Smith
  • Date: 2008-12-24 04:37:17 UTC
  • mto: This revision was merged to the branch mainline in revision 337.
  • Revision ID: jassmith@gmail.com-20081224043717-9yq3uhajlmnyyg5k
Merge community into official

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// This program is free software; you can redistribute it and/or modify
 
3
// it under the terms of the GNU General Public License as published by
 
4
// the Free Software Foundation; either version 3 of the License, or
 
5
// (at your option) any later version.
 
6
//
 
7
// This program is distributed in the hope that it will be useful,
 
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
10
// GNU General Public License for more details.
 
11
//
 
12
// You should have received a copy of the GNU General Public License
 
13
// along with this program; if not, write to the Free Software
 
14
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
15
 
 
16
using System;
 
17
using System.Net;
 
18
using System.Net.Security;
 
19
using System.Text;
 
20
using System.Xml;
 
21
using System.Web;
 
22
using System.Collections.Generic;
 
23
using Atlassian;
 
24
 
 
25
namespace JIRA.Remote
 
26
{
 
27
        /// <summary>
 
28
        /// Class which makes queries to JIRA via the RSS interface
 
29
        /// </summary>
 
30
        class JIRARssClient
 
31
        {
 
32
                /// <summary>
 
33
                /// The JIRA base url
 
34
                /// ie. http://issues.apache.org/jira
 
35
                /// </summary>
 
36
                private string _url;
 
37
                
 
38
                private string _username;
 
39
                private string _password;
 
40
                
 
41
                /// <summary>
 
42
                /// Maximum results we can retrieve in a query
 
43
                /// </summary>
 
44
                private int _maxResults= 10000;
 
45
 
 
46
                
 
47
                public JIRARssClient( string url )
 
48
                {
 
49
                        _url= url;
 
50
                }
 
51
                
 
52
                public string Username
 
53
                {
 
54
                        set { _username= value; }
 
55
                }
 
56
                
 
57
                public string Password
 
58
                {
 
59
                        set { _password= value; }
 
60
                }
 
61
                
 
62
                /// <summary>
 
63
                /// Get a list of all issues, open or closed
 
64
                /// </summary>
 
65
                public IList<RemoteIssue> GetAllIssues( IList<long> projectIds )
 
66
                {
 
67
                        return DoQuery( GetBaseQueryUrl( projectIds ) );
 
68
                }
 
69
                
 
70
                /// <summary>
 
71
                /// Get a list of all issues that have the given status ids
 
72
                /// </summary>
 
73
                public IList<RemoteIssue> GetAllIssuesWithStatus( IList<long> projectIds, IList<int> statusIds )
 
74
                {
 
75
                        StringBuilder sb= new StringBuilder();
 
76
                        sb.Append( GetBaseQueryUrl( projectIds ) );
 
77
                        
 
78
                        foreach( int statusId in statusIds )
 
79
                        {
 
80
                                sb.Append( "&status=" );
 
81
                                sb.Append( statusId );
 
82
                        }
 
83
                        return DoQuery( sb.ToString() );
 
84
                }
 
85
                
 
86
                /// <summary>
 
87
                /// Get a list of all issues (open and closed), that have been updated in the last hour
 
88
                /// </summary>
 
89
                public IList<RemoteIssue> GetRecentlyUpdatedIssues( IList<long> projectIds )
 
90
                {
 
91
                        return DoQuery( GetBaseQueryUrl( projectIds )+"&updated%3Aprevious=-1h" );
 
92
                }
 
93
                
 
94
                /// <summary>
 
95
                /// Do the actual query by converting the rss into a list of issue items
 
96
                /// </summary>
 
97
                private IList<RemoteIssue> DoQuery( String queryUrl )
 
98
                {
 
99
                        List<RemoteIssue> result= new List<RemoteIssue>();                      
 
100
                        
 
101
                        // Make the request
 
102
                        HttpWebRequest request= WebRequest.Create( queryUrl ) as HttpWebRequest;
 
103
                        
 
104
                        // Handle credentials
 
105
                        if( _username!=null && _password!=null )
 
106
                        {
 
107
                                CredentialCache creds= new CredentialCache();
 
108
                                creds.Add( new Uri( queryUrl ), "Basic", new NetworkCredential( _username, _password ) );
 
109
                                
 
110
                                request.Credentials= creds;
 
111
                        }
 
112
                        
 
113
                        // Console.WriteLine( "Query: "+queryUrl );
 
114
                        
 
115
                        HttpWebResponse response= request.GetResponse() as HttpWebResponse;
 
116
                        
 
117
                        // Parse the xml from the get all open issues query
 
118
                        XmlDocument doc= new XmlDocument();
 
119
                        doc.Load( response.GetResponseStream() );
 
120
                                                
 
121
                        foreach( XmlNode itemNode in doc.SelectNodes( "//rss/channel/item" ) )
 
122
                        {
 
123
                                RemoteIssue issue= new RemoteIssue();
 
124
                                issue.key= itemNode.SelectSingleNode( "key" ).InnerText;
 
125
                                issue.summary= itemNode.SelectSingleNode( "title" ).InnerText;
 
126
                                issue.status= itemNode.SelectSingleNode( "status" ).Attributes[ "id" ].InnerText;
 
127
                                
 
128
                                result.Add( issue );
 
129
                        }
 
130
                        
 
131
                        return result;
 
132
                }
 
133
                
 
134
                /// <summary>
 
135
                /// Use the RSS End-point to find issues that are resolved/closed for the given project ids 
 
136
                /// </summary>
 
137
                private string GetBaseQueryUrl( IList<long> projectIds )
 
138
                {
 
139
                        StringBuilder sb= new StringBuilder();
 
140
                        sb.Append( _url );
 
141
                        sb.Append( "/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?" );
 
142
 
 
143
                        // Append the request to authenticate
 
144
                        if( _username!=null && _password!=null )
 
145
                        {
 
146
                                sb.Append( "os_authType=basic" );
 
147
                        }
 
148
                        
 
149
                        // Add each of the project ids to the query
 
150
                        foreach( long projectId in projectIds )
 
151
                        {
 
152
                                sb.Append( "&pid=" );
 
153
                                sb.Append( projectId );
 
154
                        }
 
155
                        
 
156
                        // Descending sort by issue id
 
157
                        sb.Append( "&sorter/field=issuekey" );
 
158
                        sb.Append( "&sorter/order=DESC" );
 
159
                        
 
160
                        // Cap on the max results...
 
161
                        sb.Append( "&tempMax=" );
 
162
                        sb.Append( _maxResults );
 
163
                        
 
164
                        return sb.ToString();
 
165
                }
 
166
        }
 
167
}