【问题标题】:How to add repository filter when searching code in azure devops projects with REST API?使用 REST API 在 azure devops 项目中搜索代码时如何添加存储库过滤器?
【发布时间】:2021-12-30 03:05:42
【问题描述】:

我无法为 Azure DevOps REST API https://almsearch.dev.azure.com 添加存储库过滤器。

string[] parameterNames = new string[] { "MyRepo" };

JArray jarrayObj = new JArray();

foreach (string parameterName in parameterNames)
{
    jarrayObj.Add(parameterName);
}


string[] parameterNames1 = new string[] { "master" };

JArray jarrayObj1 = new JArray();

foreach (string parameterName in parameterNames1)
{
    jarrayObj1.Add(parameterName);
}

JObject payLoad =   new JObject(
                                 new JProperty("searchText", item),
                                 new JProperty("$top", 133),
                            //   new JProperty("filters", new JObject(new JProperty("Branch", jarrayObj1))
                                 new JProperty("filters", new JObject(new JProperty("Repository", jarrayObj)

                             )));

var newcontent = new StringContent(payLoad.ToString(), Encoding.UTF8, "application/json");

using (HttpResponseMessage response = client.PostAsync("https://almsearch.dev.azure.com/myorganization/_apis/search/codesearchresults?api-version=5.1-preview.1", newcontent).Result)

【问题讨论】:

  • 这需要点击一个链接来获取代码的上下文。请将链接中的任何相关信息添加到您的问题正文中。

标签: c# rest azure-devops


【解决方案1】:

在 API 的 Request Body 中,您可以在 filters 对象中设置过滤器。

filters对象中,可以设置Project过滤器、Repository过滤器、Branch过滤器, 路径过滤器等...

更多详情请见this example

[更新]

filters 对象中使用 Repository 过滤器时,您还必须添加 Project 过滤器。否则,您将获得 400 Bad Request 状态代码。

【讨论】:

  • 在我原来的问题中,我已经粘贴了有效负载创建的代码,但这似乎不适用于存储库过滤器。你能告诉我payload代码中需要修复的地方吗
  • 您好@user2386486,我注意到当您使用 Repository 过滤器时,您没有使用 Project 过滤器。这可能是您收到 400 错误的原因。我已经用更多细节更新了我的上述答案。请检查一下。
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 2021-01-26
  • 2021-06-19
  • 2021-05-22
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 2021-02-18
相关资源
最近更新 更多