目录
介绍
本文介绍了如何使用带有.C#的REST API的活动时间线(ActivityTimeline)。
背景
活动时间线JIRA插件提供了资源计划仪表板,以安排JIRA任务,自定义事件和预订,并以有组织的方式可视化团队任务。为了使用API,必须设置以下内容。
- 具有管理权限的帐户。
- REST API必须启动。(控制台>配置>高级设置)
通过API可用的选项很少。
- 授权
- 登录[POST]——/rest/api/1/session
- 注销[DELETE]——/rest/api/1/session
- 团队
- 获取团队列表[GET]——/rest/api/1/team/list
- 通过ID [GET]获取团队——/rest /api/1/team/$teamId
- 时间表时间表
- 获取时间线[GET]——/rest/api/1/timeline
- 获取工作量[GET]——/rest/api/1/workload
- 自定义事件
- 创建事件[POST]——/rest/api /1/event
- 通过ID [DELETE]删除事件——DELETE/rest/api/1/event/$id
- 获取事件类型列表[GET]——/rest/api/1/eventType/list
在本文中,我们介绍了如何使用REST API登录并获取团队和时间线数据。
使用代码
为了使用API方法,首先是获取会话ID,
protected string GetActivityTimelineSessionID()
{
string uri = string.Empty;
string cookie = string.Empty;
String credentials = "{'username':'" + "Admin Username"+ "','password':'" + "Admin password" + "'}";
HttpWebResponse response = ActivityTimelinePOST("https://xxx.activitytimeline.com/rest/api/1/session", credentials, null, null);
if (response == null || ((int)response.StatusCode) != 200)
{
return "";
}
WebHeaderCollection headers = response.Headers;
if (headers["Set-Cookie"] != null)
{
string cookies = headers["Set-Cookie"];
String[] fields = Regex.Split(cookies, ";\\s*");
cookie = fields[0];
}
return cookie;
}
GetActivityTimelineSessionID方法用于获取会话ID。在这里,我们使用用户名和密码进行POST。然后,我们读取标题信息以获取ID。
private HttpWebResponse ActivityTimelinePOST(string url, string postData, string referer, string cookie)
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/json";
if (!string.IsNullOrEmpty(cookie))
{
request.Headers.Add(HttpRequestHeader.Cookie, cookie);
}
if (!string.IsNullOrEmpty(referer))
{
request.Referer = referer;
}
request.ContentLength = byteArray.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
return (HttpWebResponse)request.GetResponse();
}
在ActivityTimelinePOST方法中,创建实际的Web请求。确保正确发送以上数据。
收到会话ID后,我们将其存储在变量中。
//// Get session ID
string jSessionID = GetActivityTimelineSessionID();
让我们看看如何从API查询团队列表。
//// Get team list
Newtonsoft.Json.Linq.JArray teamList = GetTeamList(jSessionID);
protected virtual Newtonsoft.Json.Linq.JArray GetTeamList(string jSessionID)
{
HttpWebResponse response = ActivityTimelineGET("https://xxx.activitytimeline.com/rest/api/1/team/list", null, jSessionID);
StreamReader reader = new StreamReader(response.GetResponseStream());
string jsonString = reader.ReadToEnd();
var tr = Newtonsoft.Json.Linq.JArray.Parse(jsonString);
return tr;
}
获得TeamID后,我们可以查询时间线数据,如下所示:
if (teamList.Count > 0)
{
foreach (var team in teamList)
{
var currentTeamid = team["id"];
Newtonsoft.Json.Linq.JObject timeLineData = GetTimeLineData(jSessionID, currentTeamid.ToString());
//// Get team member for a team
var currentTeamMemberList = timeLineData["members"];
foreach (var teamMember in currentTeamMemberList)
{
//// Get issues for a member
var workLoadList = teamMember["workload"];
if (workLoadList != null)
{
try
{
if (teamMember["enabled"].ToString().ToLower() == "true")
{
foreach (var workload in workLoadList)
{
ActivityTimelineRecord activityTimelineRecord = new ActivityTimelineRecord();
activityTimelineRecord.MemberID = teamMember["id"].ToString();
activityTimelineRecord.TeamID = currentTeamid.ToString();
activityTimelineRecord.TeamName = team["name"].ToString();
activityTimelineRecord.JiraUserName = teamMember["username"].ToString();
activityTimelineRecord.Involvement = teamMember["involvement"].ToString();
activityTimelineRecord.Day = workload["day"].ToString();
activityTimelineRecord.DayOfWeek = workload["dayOfWeek"].ToString();
activityTimelineRecord.Hours = workload["hours"].ToString();
activityTimelineRecordList.Add(activityTimelineRecord);
}
}
}
catch (Exception ex)
{
string errMessage = string.Format(" - {0} <br> - {1}", currentTeamid.ToString(), ex.Message);
sbErrorLog.AppendLine(errMessage);
Logger.LogError(errMessage, ex);
continue;
}
}
}
}
}
GetTimeLineData方法获取时间线信息。在这里,我们获得30条信息。向前15天,向后15天。我们正在捕获期间内的TeamName,Username,Involvement,Day,DayofWeek和分配的小时数。
protected virtual Newtonsoft.Json.Linq.JObject GetTimeLineData(string jSessionID, string teamID)
{
string futureDate = DateTime.Today.AddDays(15).ToString("yyyy-MM-dd");
string pastDate = DateTime.Today.AddDays(-15).ToString("yyyy-MM-dd");
HttpWebResponse response = ActivityTimelineGET("https://xxx.activitytimeline.com"+ string.Format("/rest/api/1/workload?start={0}&end={1}&teamId={2}&startOffset=0&maxUsers=50", pastDate, futureDate, teamID), null, jSessionID);
StreamReader reader = new StreamReader(response.GetResponseStream());
string jsonString = reader.ReadToEnd();
var tr = Newtonsoft.Json.Linq.JObject.Parse(jsonString);
return tr;
}
让我们创建一个类来存储结果。
public class ActivityTimelineRecord
{
public string TeamID { get; set; }
public string TeamName { get; set; }
public string MemberID { get; set; }
public string JiraUserName { get; set; }
public string Involvement { get; set; }
public string Day { get; set; }
public string DayOfWeek { get; set; }
public string Hours { get; set; }
}
有关更多信息,请在此处参考ActiviltyTimeline文档。
来源:CSDN
作者:寒冰屋
链接:https://blog.csdn.net/mzl87/article/details/104011601