diff --git a/nu/milestone.nu b/nu/milestone.nu index fac52ae..18d7953 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 @@ -203,23 +205,26 @@ def check-gh [] { # Milestone action entry point. export def milestone-action [ - action: string, # Action to perform, could be create, close, or bind-pr. - repo: string, # Github repository name - --gh-token(-t): string, # Github access token - --milestone(-m): string, # Milestone name - --title: string, # Milestone title to create or close - --due-on(-d): string, # Milestone due date, format: yyyy/mm/dd - --description(-D): string,# Milestone description - --pr: string, # The PR number/url/branch of the PR that we want to add milestone. - --issue: string, # 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. + action: string, # Action to perform, could be create, close, or bind-pr. + repo: string, # Github repository name + --gh-token(-t): string, # Github access token + --milestone(-m): string, # Milestone name + --title: string, # Milestone title to create or close + --due-on(-d): string, # Milestone due date, format: yyyy/mm/dd + --description(-D): string, # Milestone description + --pr: string, # The PR number/url/branch of the PR that we want to add milestone. + --issue: string, # 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. ] { + print $env.FILE_PWD? + print $env.CURRENT_FILE? + $env | table -e | print match $action { close => { close-milestone $repo $milestone --gh-token $gh_token }, create => { create-milestone $repo $title --due-on $due_on -D $description -t $gh_token }, bind-pr => { milestone-bind-for-pr $repo -t $gh_token -m $milestone --pr $pr --force=$force --dry-run=$dry_run }, - bind-issue => { milestone-bind-for-issue $repo -t $gh_token -m $milestone --issue $issue --force=$force --dry-run=$dry_run }, + bind-issue => { milestone-bind-for-issue $repo -t $gh_token -m $milestone --issue ($issue | into int) --force=$force --dry-run=$dry_run }, } } diff --git a/nu/query.nu b/nu/query.nu index 50cf643..b2eeb5e 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 @@ -14,33 +16,52 @@ export def query-issue-closer-by-graphql [ ] { let owner = $repo | split row / | first let name = $repo | split row / | last - let query = open -r nu/issue.gql + let pwd = $env.FILE_PWD? | default 'nu' + let query = open -r $'($pwd)/issue.gql' let variables = { repo_name: $name, repo_owner: $owner, 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 ...' + $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 } + $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 + $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 } }