Beispiel #1
0
        private bool UpdateResolvedTicketsToVersion(ProjectMeta projectMeta)
        {
            _logger.Info("Getting all versions which have the fix version of <{0}>", _jiraOptions.FixVersion);
            string allClosedTicketsWithoutAnAvailableVersion =
                $"project={projectMeta.key} and status in (Resolved, \"Under Test\", Closed, Done) and fixVersion = {_jiraOptions.FixVersion} order by key";

            var client = new JiraClient(Account);

            bool      moreExists = false;
            const int maxResults = 100;
            int       startAt    = 0;
            var       allIssues  = new List <Issue>();

            do
            {
                Issues issues = client.GetIssuesByJql(allClosedTicketsWithoutAnAvailableVersion, startAt, maxResults);
                allIssues.AddRange(issues.issues.Select(i => i));

                if (issues.issues.Count + startAt < issues.total)
                {
                    moreExists = true;
                    startAt   += issues.issues.Count;
                }
                else
                {
                    moreExists = false;
                }
            } while (moreExists);

            if (!allIssues.Any())
            {
                _logger.Info("No tickets found to update");
                return(true);
            }

            AnotherJiraRestClient.JiraModel.Version addedVersion = AddOrGetExistingVersion(projectMeta);

            _logger.Info(
                $"Found <{allIssues.Count}> issues for this release, will be updated to 'Available Version' <{addedVersion.name}>");

            var expando = new ExpandoObject();
            var asDict  = (IDictionary <string, object>)expando;

            asDict.Add(_jiraOptions.CustomFieldName, addedVersion);

            var updateIssue = new
            {
                fields = expando
            };

            foreach (var issue in allIssues)
            {
                _logger.Info($"Processing <{issue.key}>");

                var request = new RestRequest
                {
                    Resource = $"{ResourceUrls.IssueByKey(issue.key)}?fields={_jiraOptions.CustomFieldName}",
                    Method   = Method.GET
                };

                // TODO: custom logic to handle some version information specific to Promapp's needs
                var promappIssue = client.Execute <PromappReleaseIssue>(request, HttpStatusCode.OK);

                if (promappIssue.fields == null)
                {
                    throw new InvalidOperationException("Fields is empty, has the Jira API changed?");
                }

                bool updateVersion = false;
                AnotherJiraRestClient.JiraModel.Version customFieldVersion = promappIssue.fields.customfield_11520;
                if (customFieldVersion == null)
                {
                    updateVersion = true;
                }
                else
                {
                    // because versions can have an "_" now
                    string actualVersion = customFieldVersion.name;
                    if (!actualVersion.TrySeparateVersionAndProject(out Version stampedVersion, out string projectName))
                    {
                        throw new InvalidOperationException($"Couldn't parse custom field value for ticket <{issue.key}> of <{customFieldVersion.name}> to a version");
                    }

                    // e.g. we have moved from dev->staging
                    if (_jiraOptions.FixVersionObj >= Version.Parse("1.0.0.0") &&
                        stampedVersion < Version.Parse("1.0.0.0") &&
                        _jiraOptions.AvailableFromVersionObj >= Version.Parse("1.0.0"))
                    {
                        updateVersion = true;
                    }
                    else
                    {
                        _logger.Info($"Issue <{issue.key}> won't get updated as it is already stamped with version <{actualVersion}>");
                    }
                }

                if (updateVersion)
                {
                    _logger.Info($"Update issue <{issue.key}> with version <{_jiraOptions.AvailableFromVersion}>");
                    client.UpdateIssueFields(issue.key, updateIssue);
                }
            }
            return(true);
        }