diff --git a/nu/milestone.nu b/nu/milestone.nu index fac52ae..cbac877 100644 --- a/nu/milestone.nu +++ b/nu/milestone.nu @@ -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' @@ -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. ] { @@ -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 diff --git a/nu/query.nu b/nu/query.nu index 50cf643..1430d4b 100644 --- a/nu/query.nu +++ b/nu/query.nu @@ -7,6 +7,8 @@ # query-issue-closer-by-graphql nushell/nushell 13966 # query-issue-closer-by-graphql web-infra-dev/rsbuild 3780 +use common.nu [hr-line] + export def query-issue-closer-by-graphql [ repo: string, # Github repository name issueNO: int, # Issue number @@ -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 } }