|
| 1 | +name: Sync solving label to project status |
| 2 | +# 仅对issue增删的solving标签生效: |
| 3 | +# 添加solving标签->设置Project状态为In Progress |
| 4 | +# 移除solving标签-↓ |
| 5 | +# issue为close状态->设置Project状态为Done |
| 6 | +# issue为open状态->设置Project状态为Todo |
| 7 | +# 这里GPT死活解决不了,最后分步命令之,成功了 |
| 8 | + |
| 9 | +on: |
| 10 | + issues: |
| 11 | + types: [labeled, unlabeled] |
| 12 | + |
| 13 | +permissions: |
| 14 | + issues: read |
| 15 | + repository-projects: write |
| 16 | + |
| 17 | +env: |
| 18 | + PROJECT_ID: ${{ secrets.ONE_PROBLEM_ONE_DAY_PROJECT_ID }} |
| 19 | + STATUS_FIELD_ID: ${{ secrets.ONE_PROBLEM_ONE_DAY_STATUS_FIELD_ID }} |
| 20 | + |
| 21 | +jobs: |
| 22 | + sync: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + steps: |
| 25 | + - name: Update project status by solving label |
| 26 | + env: |
| 27 | + GH_TOKEN: ${{ secrets.ONE_PROBLEM_ONE_DAY_PAT }} |
| 28 | + run: | |
| 29 | + echo "===== Event Debug =====" |
| 30 | + echo "action = ${{ github.event.action }}" |
| 31 | + echo "issue number = ${{ github.event.issue.number }}" |
| 32 | + echo "issue node_id = ${{ github.event.issue.node_id }}" |
| 33 | + echo "issue state = ${{ github.event.issue.state }}" |
| 34 | + echo "label(event) = ${{ github.event.label.name }}" |
| 35 | + echo "=======================" |
| 36 | +
|
| 37 | + issue_node_id="${{ github.event.issue.node_id }}" |
| 38 | + issue_state="${{ github.event.issue.state }}" |
| 39 | +
|
| 40 | + labels="${{ join(github.event.issue.labels.*.name, ' ') }}" |
| 41 | + echo "Current labels: $labels" |
| 42 | +
|
| 43 | + has_solving=false |
| 44 | + for l in $labels; do |
| 45 | + [[ "$l" == "solving" ]] && has_solving=true |
| 46 | + done |
| 47 | + echo "has_solving = $has_solving" |
| 48 | +
|
| 49 | + echo "Query project item_id via Issue.projectItems..." |
| 50 | + item_id=$(gh api graphql -f query=' |
| 51 | + query($issue:ID!) { |
| 52 | + node(id:$issue) { |
| 53 | + ... on Issue { |
| 54 | + projectItems(first:20) { |
| 55 | + nodes { |
| 56 | + id |
| 57 | + project { id } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + ' -f issue="$issue_node_id" \ |
| 64 | + --jq ".data.node.projectItems.nodes[] |
| 65 | + | select(.project.id==\"$PROJECT_ID\") |
| 66 | + | .id") |
| 67 | + if [[ -z "$item_id" ]]; then |
| 68 | + echo "Issue not in project, skip" |
| 69 | + exit 0 |
| 70 | + fi |
| 71 | + echo "Found item_id = $item_id" |
| 72 | +
|
| 73 | + if [[ "$has_solving" == "true" ]]; then |
| 74 | + status="In Progress" |
| 75 | + else |
| 76 | + if [[ "$issue_state" == "closed" ]]; then |
| 77 | + status="Done" |
| 78 | + else |
| 79 | + status="Todo" |
| 80 | + fi |
| 81 | + fi |
| 82 | + echo "Target status name = $status" |
| 83 | +
|
| 84 | + echo "Resolve status option ID..." |
| 85 | + options_json=$(gh api graphql -f query=' |
| 86 | + query($field:ID!) { |
| 87 | + node(id:$field) { |
| 88 | + ... on ProjectV2SingleSelectField { |
| 89 | + options { id name } |
| 90 | + } |
| 91 | + } |
| 92 | + }' -F field="$STATUS_FIELD_ID") |
| 93 | + option_id=$(echo "$options_json" | jq -r --arg status "$status" '.data.node.options[] | select(.name==$status) | .id') |
| 94 | + if [[ -z "$option_id" ]]; then |
| 95 | + echo "❌ Cannot find option id for status=$status" |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + echo "Resolved option_id = $option_id" |
| 99 | +
|
| 100 | + echo "Update project status..." |
| 101 | + if [[ "$option_id" =~ ^[0-9]+$ ]]; then |
| 102 | + echo "纯数字 -> ID!" |
| 103 | + gql_type="ID!" |
| 104 | + # 不知道为什么,这里Done的option_id为纯数字,不论ID!还是String!都不行,折腾了好久好久 |
| 105 | + # 未验证这个能否用在In Progress和Todo上,感觉是可以的。保留if-else是为了保留这个神奇的Debug经历 |
| 106 | + gh api graphql -f query=' |
| 107 | + mutation UpdateProjectItemStatus($projectId: ID!, $itemId: ID!, $fieldId: ID!, $optionId: String!) { |
| 108 | + updateProjectV2ItemFieldValue( |
| 109 | + input: { |
| 110 | + projectId: $projectId |
| 111 | + itemId: $itemId |
| 112 | + fieldId: $fieldId |
| 113 | + value: { singleSelectOptionId: $optionId } |
| 114 | + } |
| 115 | + ) { |
| 116 | + projectV2Item { |
| 117 | + id |
| 118 | + fieldValues(first: 10) { |
| 119 | + nodes { |
| 120 | + ... on ProjectV2ItemFieldSingleSelectValue { |
| 121 | + id |
| 122 | + name |
| 123 | + optionId |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + ' -f projectId="$PROJECT_ID" -f itemId="$item_id" -f fieldId="$STATUS_FIELD_ID" -f optionId="$option_id" |
| 131 | + else |
| 132 | + echo "字符串 -> String!" |
| 133 | + gql_type="String!" |
| 134 | + gh api graphql -f query=' |
| 135 | + mutation($project:ID!, $item:ID!, $field:ID!, $option:String!) { |
| 136 | + updateProjectV2ItemFieldValue(input:{ |
| 137 | + projectId:$project |
| 138 | + itemId:$item |
| 139 | + fieldId:$field |
| 140 | + value:{ singleSelectOptionId:$option } |
| 141 | + }) { |
| 142 | + projectV2Item { id } |
| 143 | + } |
| 144 | + } |
| 145 | + ' \ |
| 146 | + -F project="$PROJECT_ID" \ |
| 147 | + -F item="$item_id" \ |
| 148 | + -F field="$STATUS_FIELD_ID" \ |
| 149 | + -F option="$option_id" |
| 150 | + fi |
| 151 | + echo "Status updated successfully ✅" |
0 commit comments