Skip to content

Instantly share code, notes, and snippets.

@topicus
Created November 13, 2024 13:25
Show Gist options
  • Save topicus/12ee07a8eb78ef9323e9c38f7e5d948f to your computer and use it in GitHub Desktop.
Save topicus/12ee07a8eb78ef9323e9c38f7e5d948f to your computer and use it in GitHub Desktop.
Reveal the issue title from the current branch

Installation

  • Go to linear -> Settings -> My Account -> API -> Personal API Keys -> Create New API Key
  • Paste the API key in the script
  • Download the script and store it in /usr/bin. I recommend to rename it from issue.py to issues
  • Give executable permissions chmod +x /usr/bin/
  • Go to a repository with a branch name that follows the Linear convention and run issue.
  • The title of the issue should be printed.
#!/usr/bin/env python3
import subprocess
import requests
# Configuration
LINEAR_API_TOKEN = "" # Replace with your actual Linear API token
LINEAR_API_URL = "https://api.linear.app/graphql"
def get_current_branch():
try:
# Get the current Git branch name
branch = (
subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
.strip()
.decode("utf-8")
)
return branch
except subprocess.CalledProcessError:
print("No Git repository found in the current workspace.")
return None
def extract_linear_issue_id(branch_name: str) -> str:
# Linear issues typically have a format like "ENG-123" or similar
# We use the last part of the branch name as the issue ID
return branch_name.split("/")[-1]
def get_linear_issue_title(issue_id):
# GraphQL query to get the Linear issue title
query = """
query($id: String!) {
issue(id: $id) {
title
}
}
"""
headers = {
"Authorization": f"{LINEAR_API_TOKEN}",
"Content-Type": "application/json",
}
variables = {"id": issue_id}
response = requests.post(
LINEAR_API_URL, json={"query": query, "variables": variables}, headers=headers
)
if response.status_code == 200:
data = response.json()
return data["data"]["issue"]["title"] if data["data"]["issue"] else None
else:
raise Exception(
f"Linear API request failed with status code {response.status_code}"
)
def main():
branch_name = get_current_branch()
if branch_name is None:
return
issue_id = extract_linear_issue_id(branch_name)
print(f"Branch name: {branch_name}")
print(f"Issue ID: {issue_id}")
if not issue_id:
print("No Linear issue ID found in the current branch name.")
return
try:
title = get_linear_issue_title(issue_id)
if title:
print(f"Linear Issue Title: {title}")
else:
print("No title found for the given Linear issue ID.")
except Exception as e:
print(f"Error fetching Linear issue title: {e}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment