Skip to content

Instantly share code, notes, and snippets.

@SteveL-MSFT
Last active May 10, 2024 20:54
Show Gist options
  • Save SteveL-MSFT/bc122c2ab3e56e0315fe4bf8e9e97391 to your computer and use it in GitHub Desktop.
Save SteveL-MSFT/bc122c2ab3e56e0315fe4bf8e9e97391 to your computer and use it in GitHub Desktop.
param(
[parameter(Mandatory)]
$user,
[System.Management.Automation.SwitchParameter]
$delete,
[string]
$yourUsername,
[parameter(Mandatory)]
[string]
$repository
)
$repositoryOwner, $repository = $repository -split '/'
if ($null -eq $repository) {
throw "Please provide the repository in the format owner/repository"
}
$repositoryQuery = @"
query {
repository(owner: "$repositoryOwner", name: "$repository") {
id
}
}
"@
$body = @{query=$repositoryQuery} | ConvertTo-Json
$repositoryData = Invoke-RestMethod https://api.github.com/graphql -Authentication OAuth -Token (Get-Secret GitHub) -Body $body -Method Post
if ($null -ne $repositoryData.errors) {
$repositoryData.errors
throw $repositoryData.errors.message
}
$repositoryId = $repositoryData.data.repository.id
Write-Verbose -Verbose "Repository ID: $repositoryId"
$query = @"
query {
user(login: "$user") {
repositoryDiscussions(first: 100, repositoryId: "$repositoryId") {
nodes {
title
id
repository {
name
}
}
}
}
}
"@
$body = @{query=$query} | ConvertTo-Json
$data = Invoke-RestMethod https://api.github.com/graphql -Authentication OAuth -Token (Get-Secret GitHub) -Body $body -Method Post
if ($null -ne $data.errors) {
$data.errors
throw $data.errors.message
}
write-verbose -verbose "Found $($data.data.user.repositoryDiscussions.nodes.Count) discussions"
$data.data.user.repositoryDiscussions.nodes
if ($delete) {
if ($yourUsername -eq '') {
throw "Please provide your username to delete discussions"
}
if ($repository -eq '') {
throw "Please provide the repository name to delete discussions"
}
foreach ($discussion in $data.data.user.repositoryDiscussions.nodes) {
if ($discussion.repository.name -ne $repository) {
continue
}
Write-Verbose -Verbose "Deleting $($discussion.title)"
$deleteQuery = @"
mutation {
deleteDiscussion(input: {id: "$($discussion.id)", clientMutationId: "$yourUsername"}) {
clientMutationId
}
}
"@
$body = @{query=$deleteQuery} | ConvertTo-Json
$out = Invoke-RestMethod https://api.github.com/graphql -Authentication OAuth -Token (Get-Secret GitHub) -Body $body -Method Post
if ($null -ne $out.errors) {
$out.errors
throw $out.errors.message
}
$out | ConvertTo-Json -Depth 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment