Skip to content

Commit

Permalink
[Tools] [ConfluenceKB] Added filter logic: label, lastModified, created
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromonaco committed May 10, 2022
1 parent 14adfa9 commit 8e9c60c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,14 @@ public async Task UpdatePage(string pageId, string htmlContent, string comment)
var result = await PutAsync($"{_url}/wiki/rest/api/content/{pageId}", requestJson);
}

public async Task<List<ConfluencePageSearchResult>> SearchContentByLabelAsync(string label)
{
//&expand=history
//expand=metadata.labels
var response = await GetAsync($"{_url}/wiki/rest/api/content/search?limit=10000&cql=type=page%20AND%20label='{label}'&expand=metadata.labels");
var wikiPageSearchResults = JsonSerializer.Deserialize<ConfluencePageSearchResults>(response, _jsonSerializerOptions);
return wikiPageSearchResults.Results.Where(r => r.Type.Equals("page")).ToList();
}

public async Task<List<ConfluencePageSearchResult>> SearchContentAsync(string contributorAccountId, string spaceKey)
public async Task<List<ConfluencePageSearchResult>> SearchContentAsync(string contributorAccountId, string spaceKey, string label, string lastModifiedYear, string createdYear)
{
string contributorClause = string.Empty;
string spaceClause = string.Empty;
string labelClause = string.Empty;
string lastModifiedClause = string.Empty;
string createdYearClause = string.Empty;

if (!string.IsNullOrEmpty(contributorAccountId))
{
Expand All @@ -72,8 +67,23 @@ public async Task<List<ConfluencePageSearchResult>> SearchContentAsync(string co
spaceClause = $" and space.key='{spaceKey}'";
}

if (!string.IsNullOrEmpty(label))
{
spaceClause = $" and label='{label}'";
}

if (!string.IsNullOrEmpty(lastModifiedYear))
{
lastModifiedClause = $" and lastModified <= '{lastModifiedYear}-12-31'";
}

if (!string.IsNullOrEmpty(createdYear))
{
createdYearClause = $" and created <= '{createdYear}-12-31'";
}

var results = new List<ConfluencePageSearchResult>();
var response = await GetAsync($"{_url}/wiki/rest/api/content/search?limit=10000&cql=type=page{contributorClause}{spaceClause}&expand=history,history.lastUpdated");
var response = await GetAsync($"{_url}/wiki/rest/api/content/search?limit=10000&cql=type=page{contributorClause}{spaceClause}{labelClause}{lastModifiedClause}{createdYearClause}&expand=history,history.lastUpdated,metadata.labels");
var wikiPageSearchResults = JsonSerializer.Deserialize<ConfluencePageSearchResults>(response, _jsonSerializerOptions);
results.AddRange(wikiPageSearchResults.Results);
while (wikiPageSearchResults.Links.Next != null)
Expand Down
9 changes: 9 additions & 0 deletions Tools/src/ConfluenceKB/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public class Options
[Option("SpaceKey", Required = false, HelpText = "Space Key")]
public string SpaceKey { get; set; }

[Option("Label", Required = false, HelpText = "Labels")]
public string Label { get; set; }

[Option("LastModifiedYear", Required = false, HelpText = "Last Modified Year")]
public string LastModifiedYear { get; set; }

[Option("CreatedYear", Required = false, HelpText = "Created Year")]
public string CreatedYear { get; set; }

}
}
8 changes: 4 additions & 4 deletions Tools/src/ConfluenceKB/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o =>
{
ExecuteJob(o.ConfluenceEndpoint, o.ConfluenceUsername, o.ConfluenceAuthenticationToken, o.UserAccountId, o.SpaceKey);
ExecuteJob(o.ConfluenceEndpoint, o.ConfluenceUsername, o.ConfluenceAuthenticationToken, o.UserAccountId, o.SpaceKey, o.Label, o.LastModifiedYear, o.CreatedYear);
});

void ExecuteJob(string confluenceEndpoint, string confluenceUsername, string confluenceAuthenticationToken, string userAccountId, string spaceKey)
void ExecuteJob(string confluenceEndpoint, string confluenceUsername, string confluenceAuthenticationToken, string userAccountId, string spaceKey, string label, string lastModifiedYear, string createdYear)
{
var key = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{confluenceUsername}:{confluenceAuthenticationToken}"));
var confluenceConnector = new ConfluenceConnector(confluenceEndpoint, key, AuthenticationType.Basic);
var results = confluenceConnector.SearchContentAsync(userAccountId, spaceKey).Result;
var results = confluenceConnector.SearchContentAsync(userAccountId, spaceKey, label, lastModifiedYear, createdYear).Result;
foreach (var result in results)
{
Console.WriteLine($"https://{confluenceEndpoint}/wiki{result.Links.WebUi}");
Console.WriteLine($"{confluenceEndpoint}/wiki{result.Links.WebUi}");
}
}

0 comments on commit 8e9c60c

Please sign in to comment.