Beispiel #1
0
        internal void Publish(Plugin plugin, PluginBuilder pluginBuilder)
        {
            if (!HasOAuthToken)
            {
                throw new ArgumentException("Need oAuthToken");
            }

            CreateRelease release = new CreateRelease(plugin.version, pluginBuilder.release);

            string zipFileName;

            if (pluginBuilder.zipFileName != null)
            {
                zipFileName = Path.GetFileName(pluginBuilder.zipFileName);
            }
            else
            {
                zipFileName = plugin.name.repository;
            }

            if (!zipFileName.EndsWith(".zip"))
            {
                zipFileName = zipFileName + ".zip";
            }

            string zipFilePath = PathExtensions.Combine(plugin.directory, zipFileName);

            try
            {
                plugin.Zip(zipFilePath);
                if (PublishRelease(release, zipFilePath))
                {
                    MessageBox.Show("Release posted");
                }
            }
            finally
            {
                if (File.Exists(zipFilePath))
                {
                    File.Delete(zipFilePath);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Publish a new release.
        /// </summary>
        /// <param name="create">Release information.</param>
        /// <param name="assetsPaths">Assets to be included, do not include folders. It is recommended that you compress the files and pass the zip file path to this method.</param>
        public bool PublishRelease(CreateRelease create, params string[] assetsPaths)
        {
            if (_oAuthToken == null)
            {
                throw new NullReferenceException("Cannot publish if authentication token is null");
            }

            if (assetsPaths == null || assetsPaths.Length == 0)
            {
                throw new ArgumentException("No Assets, cannot publish");
            }
            foreach (string path in assetsPaths)
            {
                if (!File.Exists(path))
                {
                    throw new ArgumentException("File does not exist: " + path);
                }
            }

            string fail;

            if (!CanCreateRelease(create.version, out fail))
            {
                Logger.WriteLine(fail);
                return(false);
            }

            // release needs to be draft while it is being created, in case of failure
            bool draft = create.draft;

            create.draft = true;

            HttpWebRequest request = WebRequest.CreateHttp(name.releases_site);

            request.UserAgent = _userAgent;
            request.Method    = "POST";
            request.Headers.Add("Authorization", "token " + _oAuthToken);

            Logger.WriteLine("POST release");

            using (Stream requestStream = request.GetRequestStream())
                create.WriteCreateJson(requestStream);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Release));
            Release release;

            using (WebResponse response = request.GetResponse())
                using (Stream responseStream = response.GetResponseStream())
                    release = (Release)serializer.ReadObject(responseStream);

            Logger.WriteLine("Release id: " + release.id);

            try
            {
                foreach (string asset in assetsPaths)
                {
                    string fileName = Path.GetFileName(asset);
                    request             = WebRequest.CreateHttp(name.uploads_site + '/' + release.id + "/assets?name=" + fileName);
                    request.UserAgent   = _userAgent;
                    request.Method      = "POST";
                    request.ContentType = "application/" + Path.GetExtension(fileName).Substring(1);
                    request.Headers.Add("Authorization", "token " + _oAuthToken);

                    using (Stream upStream = request.GetRequestStream())
                        using (FileStream fileRead = new FileStream(asset, FileMode.Open))
                            fileRead.CopyTo(upStream);

                    Logger.WriteLine("Posting: " + fileName);
                    request.GetResponse().Dispose();
                }
            }
            catch (WebException ex)
            {
                Logger.WriteLine("Failed to post asset(s)\n" + ex);
                DeleteRelease(release);
                throw;
            }

            if (!draft)
            {
                release.draft = draft;
                EditRelease(ref release);
            }

            _releases = null;             // needs to be updated

            Logger.WriteLine("Release published");
            return(true);
        }