pr-link-issue

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Link a GitHub Issue + Linear Ticket on a PR

为PR关联GitHub Issue + Linear工单

Appends a Sentry-style
#### Issues
block to a PR description, referencing both the GitHub issue and the Linear ticket pulled from the issue's
linear-linkback
comment.
在PR描述中添加Sentry风格的
#### Issues
区块,同时引用GitHub issue以及从该issue的
linear-linkback
评论中提取的Linear工单。

Inputs

输入参数

  • <issue-url>
    — GitHub issue URL like
    https://github.com/<owner>/<repo>/issues/<n>
    . Issue number alone is fine if the PR is in the same repo.
  • (optional)
    <pr-number>
    — defaults to the open PR for the current branch.
  • <issue-url>
    — GitHub issue的URL,格式如
    https://github.com/<owner>/<repo>/issues/<n>
    。如果PR与issue在同一个仓库中,仅提供issue编号即可。
  • (可选)
    <pr-number>
    — 默认值为当前分支对应的已打开PR的编号。

Steps

操作步骤

  1. Resolve the PR number — skip if user supplied one:
    bash
    gh pr view --json number,body -q '.number'
    If no PR exists on the branch, stop and tell the user.
  2. Extract issue number + repo from the input URL, or accept a bare
    #1234
    for current repo.
  3. Fetch the Linear ticket from the issue's linear-linkback comment:
    bash
    gh issue view <n> --repo <owner>/<repo> --json comments \
      -q '.comments[] | select(.author.login=="linear-code") | .body' \
      | grep -Eioe '[a-z]+-[0-9]+' | head -1
    If no match, fall back to asking the user for the Linear key, or omit it.
  4. Read the existing PR body so you can append rather than overwrite:
    bash
    gh pr view <pr-number> --json body -q '.body'
  5. Construct the new body. If the body is empty, use just the
    #### Issues
    block. Otherwise, append it after a blank line. Don't duplicate — if
    #### Issues
    is already present, replace that section instead of adding a second one.
    Format:
    markdown
    #### Issues
    
    * Resolves: #<n>
    * Resolves: <linear-key>
  6. Update the PR with a heredoc to preserve newlines:
    bash
    gh pr edit <pr-number> --body "$(cat <<'EOF'
    <new body>
    EOF
    )"
  7. Confirm by echoing the resulting PR URL:
    bash
    gh pr view <pr-number> --json url -q '.url'
  1. 确定PR编号 — 如果用户已提供则跳过此步骤:
    bash
    gh pr view --json number,body -q '.number'
    如果当前分支没有对应的PR,请停止操作并告知用户。
  2. 从输入的URL中提取issue编号和仓库信息,或者接受当前仓库中的纯编号格式(如
    #1234
    )。
  3. 从issue的linear-linkback评论中获取Linear工单:
    bash
    gh issue view <n> --repo <owner>/<repo> --json comments \
      -q '.comments[] | select(.author.login=="linear-code") | .body' \
      | grep -Eioe '[a-z]+-[0-9]+' | head -1
    如果未匹配到结果,则询问用户提供Linear密钥,或者省略该信息。
  4. 读取PR的现有描述内容,以便进行追加而非覆盖:
    bash
    gh pr view <pr-number> --json body -q '.body'
  5. 构建新的PR描述内容。如果原描述为空,则仅使用
    #### Issues
    区块;否则,在空行后追加该区块。注意避免重复——如果
    #### Issues
    区块已存在,则替换该部分而非添加第二个。
    格式如下:
    markdown
    #### Issues
    
    * Resolves: #<n>
    * Resolves: <linear-key>
  6. 使用here-doc更新PR以保留换行符:
    bash
    gh pr edit <pr-number> --body "$(cat <<'EOF'
    <new body>
    EOF
    )"
  7. 输出PR的URL以确认操作完成:
    bash
    gh pr view <pr-number> --json url -q '.url'

Notes

注意事项

  • Linear linkback comments are posted by the GitHub user
    linear-code
    . The body contains a markdown link whose text is the Linear key, e.g.
    PY-2357
    .
  • Project keys vary per repo (
    PY-…
    for sentry-python,
    JS-…
    for sentry-javascript, etc.) — the regex
    [a-z]+-[0-9]+
    covers them.
  • Don't strip existing PR content. Always read first, append/replace second.
  • If the issue doesn't have a Linear linkback yet (newly filed), proceed with just the GitHub issue reference and tell the user the Linear key is missing.
  • Linear linkback评论由GitHub用户
    linear-code
    发布。评论内容包含一个markdown链接,链接文本即为Linear密钥,例如
    PY-2357
  • 不同仓库的项目密钥格式不同(如sentry-python仓库为
    PY-…
    ,sentry-javascript仓库为
    JS-…
    等)——正则表达式
    [a-z]+-[0-9]+
    可覆盖这些格式。
  • 不要删除PR的现有内容。务必先读取原内容,再进行追加或替换操作。
  • 如果issue尚未添加Linear linkback评论(例如新创建的issue),则仅添加GitHub issue引用,并告知用户缺少Linear密钥。