Skip to content

Commit

Permalink
feat: Use Github graphql API to query issue closer (#66) (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcer authored Oct 25, 2024
1 parent 3c31703 commit 8eaf0fe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
6 changes: 4 additions & 2 deletions nu/milestone.nu
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Description: Scripts for Github milestone management.

use common.nu [ECODE, hr-line is-installed]
use query.nu [query-issue-closer-by-graphql]

export-env {
$env.config.table.mode = 'light'
Expand Down Expand Up @@ -59,7 +60,7 @@ export def 'milestone-bind-for-issue' [
repo: string, # Github repository name
--gh-token(-t): string, # Github access token
--milestone(-m): string, # Milestone name
--issue: string, # The Issue number that we want to add milestone.
--issue: int, # The Issue number that we want to add milestone.
--force(-f), # Force update milestone even if the milestone is already set.
--dry-run(-d), # Dry run, only print the milestone that would be set.
] {
Expand All @@ -74,7 +75,8 @@ export def 'milestone-bind-for-issue' [
print $'Issue (ansi p)($issue)(ansi reset) is Not (ansi p)COMPLETED(ansi reset), will be ignored.'
return
}
let selected = if ($milestone | is-empty) { guess-milestone-for-issue $repo $issue | get milestone } else { $milestone }
let token = $env.GH_TOKEN? | default $env.GITHUB_TOKEN?
let selected = if ($milestone | is-empty) { query-issue-closer-by-graphql $repo $issue $token | get closedBy?.milestone? } else { $milestone }
if $force {
let prevMilestone = gh issue view $issue --repo $repo --json 'milestone' | from json | get milestone?.title? | default '-'
let shouldRemove = $prevMilestone != $selected
Expand Down
42 changes: 31 additions & 11 deletions nu/query.nu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# query-issue-closer-by-graphql nushell/nushell 13966 <token>
# query-issue-closer-by-graphql web-infra-dev/rsbuild 3780 <token>

use common.nu [hr-line]

export def query-issue-closer-by-graphql [
repo: string, # Github repository name
issueNO: int, # Issue number
Expand All @@ -21,26 +23,44 @@ export def query-issue-closer-by-graphql [
issue_number: $issueNO
}

const QUERY_API = 'https://api.github.com/graphql'
let HEADERS = ['Authorization' $'bearer ($token)']
let payload = { query: $query, variables: $variables } | to json
let status = query-issue-status $issueNO $payload $token
print 'Issue Status:'; hr-line; $status | reject events | table -e | print

$status
}

def query-issue-status [issueNO: int, payload: string, token: string] {
let rename = {
'author.login': 'author',
'milestone.title': 'milestone',
'repository.nameWithOwner': 'repo',
'mergeCommit.abbreviatedOid': 'sha'
}
const QUERY_API = 'https://api.github.com/graphql'
let HEADERS = ['Authorization' $'bearer ($token)']

let payload = { query: $query, variables: $variables } | to json
let result = http post --content-type application/json -H $HEADERS $QUERY_API $payload
| get data.repository.issueOrPullRequest
mut tries = 1
mut result = {}
mut closer = {}
mut events = []
mut milestone = '-'
# Loop 5 times to find the milestone of the last closed PR
loop {
if $milestone != '-' or $tries > 5 { break }
print $'Try to query milestone for issue (ansi p)($issueNO)(ansi reset) the (ansi p)($tries)(ansi reset) time ...'
let result = http post --content-type application/json -H $HEADERS $QUERY_API $payload
| get data.repository.issueOrPullRequest

let events = $result.timeline.edges.node | filter {|it| $it.stateReason? | is-not-empty }
let events = $result.timeline.edges.node | filter {|it| $it.stateReason? | is-not-empty }

let closer = $events | filter {|it| $it.closer?.number? | is-not-empty }
| select closer | flatten
| select number milestone?.title? author.login repository.nameWithOwner mergeCommit.abbreviatedOid title
| rename -c $rename
| last
let closer = $events | filter {|it| $it.closer?.number? | is-not-empty }
| select closer | flatten
| select number milestone?.title? author.login repository.nameWithOwner mergeCommit.abbreviatedOid title
| rename -c $rename
| last
$milestone = $closer.milestone?
}

{ closed: $result.closed, closedAt: $result.closedAt, closedBy: $closer, events: $events }
}

0 comments on commit 8eaf0fe

Please sign in to comment.