upsource

Is there a way to close all open reviews in Upsource?


There are lots of open reviews and i dont want to close manuelly. I want to close old open reviews. How can i close all open reviews as bulk in upsource?


Solution

  • I write a simple code with upsource api. Here is the example code to close all open reviews.

     public class Rootobject
    {
        public Result result { get; set; }
    }
    
    public class Result
    {
        public Review[] reviews { get; set; }
        public bool hasMore { get; set; }
        public int totalCount { get; set; }
    }
    
    public class Review
    {
        public Reviewid reviewId { get; set; }
        public string title { get; set; }
        public Participant[] participants { get; set; }
        public int state { get; set; }
        public bool isUnread { get; set; }
        public bool isReadyToClose { get; set; }
        public bool isRemoved { get; set; }
        public long createdAt { get; set; }
        public string createdBy { get; set; }
        public long updatedAt { get; set; }
        public Completionrate completionRate { get; set; }
        public Discussioncounter discussionCounter { get; set; }
        public bool isMuted { get; set; }
        public string description { get; set; }
    }
    
    public class Reviewid
    {
        public string projectId { get; set; }
        public string reviewId { get; set; }
    }
    
    public class Completionrate
    {
        public int completedCount { get; set; }
        public int reviewersCount { get; set; }
        public bool hasConcern { get; set; }
    }
    
    public class Discussioncounter
    {
        public int count { get; set; }
        public bool hasUnresolved { get; set; }
        public int unresolvedCount { get; set; }
        public int resolvedCount { get; set; }
    }
    
    public class Participant
    {
        public string userId { get; set; }
        public int role { get; set; }
        public int state { get; set; }
    }
    
     class Program
    {
        static void Main(string[] args)
        {
            string token="Authorization: Basic username:password";//username:password with base64 encoded
            var s = HttpPOST("http://your-upsource-address/~rpc/getReviews",
                 "{\"projectId\":\"projectId\\", \"limit\":2000,\"query\":\"state: open\"}", token);
            var reviews = JsonConvert.DeserializeObject<Rootobject>(s);
            foreach (Review resultReview in reviews.result.reviews)
            {
                string review = $"{{\"reviewId\":{{\"projectId\":\"projectId\",\"reviewId\":\"{resultReview.reviewId.reviewId}\"}},\"isFlagged\":true}}";
                HttpPOST("http://your-upsource-addres/~rpc/closeReview", review, token);
            }
        }
    
        public static string HttpPOST(string url, string querystring, string token)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.Headers.Add(token);
            request.ContentType = "application/x-www-form-urlencoded"; 
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
    
            try
            {
                requestWriter.Write(querystring);
            }
            catch
            {
                throw;
            }
            finally
            {
                requestWriter.Close();
                requestWriter = null;
            }
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }
    }