Skip to content

Commit

Permalink
feat: Use Github graphql API to query issue closer
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcer committed Oct 25, 2024
1 parent 3c31703 commit 63680fb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
33 changes: 19 additions & 14 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 Expand Up @@ -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 },
}
}

Expand Down
45 changes: 33 additions & 12 deletions nu/query.nu
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,61 @@
# 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
token: string # Github access token
] {
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 }
}

0 comments on commit 63680fb

Please sign in to comment.