Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Tip/Trick

JIRA ActivityTimeline REST API with .NET

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Jan 2020CPOL1 min read 3.5K  
JIRA ActivityTimeline REST API with .NET
This article shows to how to work with Activity Timeline using the REST API with C#.

Background

Activity Timeline JIRA add-on provides resource planning dashboard to schedule JIRA tasks, custom events, and bookings and visualize team tasks in an organized manner. In order to work with API, following have to be set:

  • Account with administrative permission
  • REST API has to be switched on (Dashboard > Configuration > Advance settings)

There are few options available via API.

  • Authorization
    • Login [POST] - /rest/api/1/session
    • Logout [DELETE] - /rest/api/1/session
  • Team
    • Get Team List [GET] - /rest/api/1/team/list
    • Get Team by ID [GET] - /rest/api/1/team/$teamId
  • Timeline schedule
    • Get Timeline [GET] - /rest/api/1/timeline
    • Get Workload [GET] - /rest/api/1/workload
  • Custom Events
    • Create an event [POST] - /rest/api/1/event
    • Delete an event by ID [DELETE] - DELETE /rest/api/1/event/$id
    • Get event type list [GET] - /rest/api/1/eventType/list

In this article, we cover how to login with REST API and get team and timeline data.

Using the Code

In order to work with API methods, first get the session ID:

C#
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 method is used to acquire the session ID. Here, we are making a POST with username and password. Then, we read the header information to get the ID.

C#
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 method is creating the actual web request. Make sure to send the above data correctly.

Once we receive the session ID, let's store in a variable.

C#
//// Get session ID
string jSessionID = GetActivityTimelineSessionID();

Let's see how to query team list from the API.

C#
//// 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;
}

Once we get the TeamID, we can the query Timeline data as follows:

C#
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 method gets the timeline information. Here, we are getting 30 information. 15 days onward and 15 days backward. We are capturing TeamName, Username, Involvement, Day, DayofWeek and allocated hours within the period.

C#
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}&amp;end={1}&amp;teamId={2}&amp;
            startOffset=0&amp;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;
}

Let's create a class to store the result.

C#
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; }
   }

For more information, please refer to the ActivityTimeline documentation here.

History

  • 3rd January, 2020: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Sri Lanka Sri Lanka
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --