<commit_before>﻿using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using VstsSyncMigrator.Engine.Configuration.Processing;

namespace VstsSyncMigrator.Engine
{
    public class AttachementImportMigrationContext : AttachementMigrationContextBase
    {
        public override string Name
        {
            get
            {
                return "AttachementImportMigrationContext";
            }
        }
        public AttachementImportMigrationContext(MigrationEngine me, AttachementImportMigrationConfig config) : base(me, config)
        {

        }

        internal override void InternalExecute()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //////////////////////////////////////////////////
            WorkItemStoreContext targetStore = new WorkItemStoreContext(me.Target, WorkItemStoreFlags.BypassRules);
            Project destProject = targetStore.GetProject();

            Trace.WriteLine(string.Format("Found target project as {0}", destProject.Name));

            List<string> files = System.IO.Directory.EnumerateFiles(exportPath).ToList<string>();
            WorkItem targetWI = null;
            int current = files.Count;
            int failures = 0;
            foreach (string file in files)
            {
                string fileName = System.IO.Path.GetFileName(file);
                try
                {
                    string reflectedID = fileName.Split('#')[0].Replace('+', ':').Replace("--", "/");
                    targetWI = targetStore.FindReflectedWorkItemByReflectedWorkItemId(reflectedID, me.ReflectedWorkItemIdFieldName);
                    if (targetWI != null)
                    {
                        Trace.WriteLine(string.Format("{0} of {1} - Import {2} to {3}", current, files.Count, fileName, targetWI.Id));
                        Attachment a = new Attachment(file);
                        targetWI.Attachments.Add(a);

                        targetWI.Save();
                        System.IO.File.Delete(file);
                    }
                    else
                    {
                        Trace.WriteLine(string.Format("{0} of {1} - Skipping {2} to {3}", current, files.Count, fileName, 0));
                    }
                } catch (FileAttachmentException ex)
                {
                    // Probably due to attachment being over size limit
                    Trace.WriteLine(ex.Message) ;
                    failures++;
                }
                current--;
            }
            //////////////////////////////////////////////////
            stopwatch.Stop();
            Console.WriteLine(@"IMPORT DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds - {1} Files imported, {2} Failures", stopwatch.Elapsed, (files.Count- failures), failures);
        }

    }
}<commit_msg>Improving attachment import  (#61)<commit_after>﻿using Microsoft.TeamFoundation.WorkItemTracking.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using VstsSyncMigrator.Engine.Configuration.Processing;

namespace VstsSyncMigrator.Engine
{
    public class AttachementImportMigrationContext : AttachementMigrationContextBase
    {
        public override string Name
        {
            get
            {
                return "AttachementImportMigrationContext";
            }
        }
        public AttachementImportMigrationContext(MigrationEngine me, AttachementImportMigrationConfig config) : base(me, config)
        {

        }

        internal override void InternalExecute()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            //////////////////////////////////////////////////
            WorkItemStoreContext targetStore = new WorkItemStoreContext(me.Target, WorkItemStoreFlags.BypassRules);
            Project destProject = targetStore.GetProject();

            Trace.WriteLine(string.Format("Found target project as {0}", destProject.Name));

            List<string> files = System.IO.Directory.EnumerateFiles(exportPath).ToList<string>();
            WorkItem targetWI = null;
            int current = files.Count;
            int failures = 0;
            int skipped = 0;
            foreach (string file in files)
            {
                string fileName = System.IO.Path.GetFileName(file);
                try
                {
                    string reflectedID = fileName.Split('#')[0].Replace('+', ':').Replace("--", "/");
                    targetWI = targetStore.FindReflectedWorkItemByReflectedWorkItemId(reflectedID, me.ReflectedWorkItemIdFieldName);
                    if (targetWI != null)
                    {
                        Trace.WriteLine(string.Format("{0} of {1} - Import {2} to {3}", current, files.Count, fileName, targetWI.Id));
                        var attachments = targetWI.Attachments.Cast<Attachment>();
                        var attachment = attachments.Where(a => a.Name == fileName).FirstOrDefault();
                        if (attachment == null)
                        {
                            Attachment a = new Attachment(file);
                            targetWI.Attachments.Add(a);
                            targetWI.Save();
                        }
                        else
                        {
                            Trace.WriteLine(string.Format(" [SKIP] WorkItem {0} already contains attachment {1}", targetWI.Id, fileName));
                            skipped++;
                        }
                        System.IO.File.Delete(file);
                    }
                    else
                    {
                        Trace.WriteLine(string.Format("{0} of {1} - Skipping {2} to {3}", current, files.Count, fileName, 0));
                        skipped++;
                    }
                } catch (FileAttachmentException ex)
                {
                    // Probably due to attachment being over size limit
                    Trace.WriteLine(ex.Message) ;
                    failures++;
                }
                current--;
            }
            //////////////////////////////////////////////////
            stopwatch.Stop();
            Console.WriteLine(@"IMPORT DONE in {0:%h} hours {0:%m} minutes {0:s\:fff} seconds - {1} Files imported, {2} Failures, {3} Skipped", stopwatch.Elapsed, (files.Count- failures), failures, skipped);
        }

    }
}
{'fraction_non_alphanumeric': 0.12488715016551309, 'fraction_numerical': 0.006018657839301836, 'mean_word_length': 1.4710037174721189, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4631860', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1542, 'n_words': 339}