Skip to content

Instantly share code, notes, and snippets.

@lukestanley
Last active January 5, 2024 12:34
Show Gist options
  • Save lukestanley/bd35dc0d9e7d5413ec7de1e9afa83723 to your computer and use it in GitHub Desktop.
Save lukestanley/bd35dc0d9e7d5413ec7de1e9afa83723 to your computer and use it in GitHub Desktop.
Archives done Linear Sync tasks via GraphQL API
# Set API_KEY!
import requests
headers = {
'Authorization': API_KEY,
'Content-Type': 'application/json'
}
# Search for issues that have been synced from LinearSync:
query = """
query Search(
$term: String!,
$filter: IssueFilter,
$first: Int,
$last: Int,
$after: String,
$before: String,
$orderBy: PaginationOrderBy,
$includeArchived: Boolean,
$includeComments: Boolean,
$teamId: String
) {
searchIssues(
filter: $filter,
first: $first,
last: $last,
term: $term,
after: $after,
before: $before,
orderBy: $orderBy,
includeArchived: $includeArchived,
includeComments: $includeComments,
teamId: $teamId
) {
nodes {
id
metadata
title
description
state {
name
}
}
totalCount
}
}
"""
variables = {
"term": "via LinearSync",
"includeArchived": False,
"includeComments": True,
}
response = requests.post('https://api.linear.app/graphql', json={'query': query, 'variables': variables, 'operationName': "Search"}, headers=headers)
issues = response.json().get("data", {}).get("searchIssues", {}).get("nodes", [])
# If they are in the Done state, archive them:
may_archive= []
for issue in issues:
state = issue.get("state", {}).get("name")
if state == "Done":
print(issue['id'], issue['title'], issue['description'])
may_archive.append(issue)
print(len(may_archive), 'issues were in the Done state and may be archived.')
def archive_issue(issueId):
"""Uses a GraphQL query request to archive a specific issue, prints the result and returns the status"""
query = """
mutation ArchiveIssue($issueId: String!) {
issueArchive(id: $issueId) {
success
}
}
"""
variables = {
"issueId": issueId
}
response = requests.post('https://api.linear.app/graphql', json={'query': query, 'variables': variables}, headers=headers)
response_json = response.json()
was_archive_success = response_json.get("data", {}).get("issueArchive", {}).get("success", False)
print('Was archive a success?', was_archive_success, 'for issue', issueId, 'response', response_json)
return was_archive_success
# Archives all the identified done LinearSync matched issues:
for issue in may_archive:
archive_issue(issue['id'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment