|
| 1 | +# 测试脚本:验证修复后的GitHub API功能 |
| 2 | +# 使用gh auth token进行测试 |
| 3 | + |
| 4 | +$ErrorActionPreference = "Stop" |
| 5 | + |
| 6 | +# 设置环境变量 |
| 7 | +$env:GITHUB_TOKEN = gh auth token |
| 8 | +$env:REPOSITORY = "MiyakoMeow/ScoopGithubActions" |
| 9 | + |
| 10 | +Write-Host "=== 测试开始 ===" -ForegroundColor Green |
| 11 | +Write-Host "" |
| 12 | + |
| 13 | +# 测试1: 验证token是否有效 |
| 14 | +Write-Host "测试1: 验证GitHub Token" -ForegroundColor Cyan |
| 15 | +try { |
| 16 | + $user = gh api /user --jq '.login' |
| 17 | + Write-Host "✓ Token有效,用户: $user" -ForegroundColor Green |
| 18 | +} catch { |
| 19 | + Write-Host "✗ Token无效: $($_.Exception.Message)" -ForegroundColor Red |
| 20 | + exit 1 |
| 21 | +} |
| 22 | +Write-Host "" |
| 23 | + |
| 24 | +# 测试2: GraphQL API测试 |
| 25 | +Write-Host "测试2: 测试GraphQL API" -ForegroundColor Cyan |
| 26 | +try { |
| 27 | + $graphqlQuery = @" |
| 28 | +query(`$owner:String!, `$repo:String!) { |
| 29 | + repository(owner:`$owner, name:`$repo) { |
| 30 | + defaultBranchRef { |
| 31 | + name |
| 32 | + } |
| 33 | + pullRequests(states:OPEN, first:5) { |
| 34 | + nodes { |
| 35 | + number |
| 36 | + title |
| 37 | + baseRefName |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | +"@ |
| 43 | + |
| 44 | + $response = gh api graphql -f query="$graphqlQuery" -f owner=MiyakoMeow -f repo=ScoopGithubActions --jq '.data.repository' |
| 45 | + $defaultBranch = $response.defaultBranchRef.name |
| 46 | + $prCount = $response.pullRequests.nodes.Count |
| 47 | + |
| 48 | + Write-Host "✓ GraphQL API成功" -ForegroundColor Green |
| 49 | + Write-Host " 默认分支: $defaultBranch" -ForegroundColor Gray |
| 50 | + Write-Host " PR数量: $prCount" -ForegroundColor Gray |
| 51 | +} catch { |
| 52 | + Write-Host "✗ GraphQL API失败: $($_.Exception.Message)" -ForegroundColor Red |
| 53 | +} |
| 54 | +Write-Host "" |
| 55 | + |
| 56 | +# 测试3: REST API测试(分支保护检查) |
| 57 | +Write-Host "测试3: 测试REST API(分支保护检查)" -ForegroundColor Cyan |
| 58 | +try { |
| 59 | + $branchInfo = gh api /repos/MiyakoMeow/ScoopGithubActions/branches/main --json 'name,protected' |
| 60 | + $isProtected = $branchInfo.protected |
| 61 | + |
| 62 | + Write-Host "✓ REST API成功" -ForegroundColor Green |
| 63 | + Write-Host " 分支名: $($branchInfo.name)" -ForegroundColor Gray |
| 64 | + Write-Host " 受保护: $isProtected" -ForegroundColor Gray |
| 65 | +} catch { |
| 66 | + Write-Host "✗ REST API失败: $($_.Exception.Message)" -ForegroundColor Red |
| 67 | +} |
| 68 | +Write-Host "" |
| 69 | + |
| 70 | +# 测试4: 模拟PR筛选逻辑(GraphQL格式) |
| 71 | +Write-Host "测试4: 测试修复后的PR筛选逻辑(GraphQL格式)" -ForegroundColor Cyan |
| 72 | +try { |
| 73 | + # 模拟GraphQL返回的PR对象 |
| 74 | + $mockPrs = @( |
| 75 | + @{ title = "test@1.0.0: Fix hash"; baseRefName = "main"; number = 1 }, |
| 76 | + @{ title = "test@1.0.0: Fix hash"; baseRefName = "develop"; number = 2 }, |
| 77 | + @{ title = "other@1.0.0: Fix hash"; baseRefName = "main"; number = 3 } |
| 78 | + ) |
| 79 | + |
| 80 | + $masterBranch = "main" |
| 81 | + $titleToBePosted = "test@1.0.0: Fix hash" |
| 82 | + |
| 83 | + # 使用修复后的筛选逻辑 |
| 84 | + $filteredPrs = $mockPrs | Where-Object { |
| 85 | + $_.title -eq $titleToBePosted -and ( |
| 86 | + $_.baseRefName -eq $masterBranch -or $_.base.ref -eq $masterBranch |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + if ($filteredPrs.Count -eq 1 -and $filteredPrs[0].number -eq 1) { |
| 91 | + Write-Host "✓ GraphQL格式PR筛选正确" -ForegroundColor Green |
| 92 | + Write-Host " 筛选结果: PR#$($filteredPrs[0].number)" -ForegroundColor Gray |
| 93 | + } else { |
| 94 | + Write-Host "✗ GraphQL格式PR筛选失败" -ForegroundColor Red |
| 95 | + } |
| 96 | +} catch { |
| 97 | + Write-Host "✗ 测试失败: $($_.Exception.Message)" -ForegroundColor Red |
| 98 | +} |
| 99 | +Write-Host "" |
| 100 | + |
| 101 | +# 测试5: 模拟PR筛选逻辑(REST格式) |
| 102 | +Write-Host "测试5: 测试修复后的PR筛选逻辑(REST格式)" -ForegroundColor Cyan |
| 103 | +try { |
| 104 | + # 模拟REST返回的PR对象 |
| 105 | + $mockPrs = @( |
| 106 | + @{ title = "test@1.0.0: Fix hash"; base = @{ ref = "main" }; number = 1 }, |
| 107 | + @{ title = "test@1.0.0: Fix hash"; base = @{ ref = "develop" }; number = 2 }, |
| 108 | + @{ title = "other@1.0.0: Fix hash"; base = @{ ref = "main" }; number = 3 } |
| 109 | + ) |
| 110 | + |
| 111 | + $masterBranch = "main" |
| 112 | + $titleToBePosted = "test@1.0.0: Fix hash" |
| 113 | + |
| 114 | + # 使用修复后的筛选逻辑 |
| 115 | + $filteredPrs = $mockPrs | Where-Object { |
| 116 | + $_.title -eq $titleToBePosted -and ( |
| 117 | + $_.baseRefName -eq $masterBranch -or $_.base.ref -eq $masterBranch |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + if ($filteredPrs.Count -eq 1 -and $filteredPrs[0].number -eq 1) { |
| 122 | + Write-Host "✓ REST格式PR筛选正确" -ForegroundColor Green |
| 123 | + Write-Host " 筛选结果: PR#$($filteredPrs[0].number)" -ForegroundColor Gray |
| 124 | + } else { |
| 125 | + Write-Host "✗ REST格式PR筛选失败" -ForegroundColor Red |
| 126 | + } |
| 127 | +} catch { |
| 128 | + Write-Host "✗ 测试失败: $($_.Exception.Message)" -ForegroundColor Red |
| 129 | +} |
| 130 | +Write-Host "" |
| 131 | + |
| 132 | +Write-Host "=== 测试完成 ===" -ForegroundColor Green |
0 commit comments