|
| 1 | +# For duplicate issues, ensure the close type is right (not planned), update it if not |
| 2 | +# For all closed (completed) issues, cascade the closure onto any referenced rageshakes |
| 3 | +# For all closed (not planned) issues, comment on rageshakes to move them into the canonical issue if one exists |
| 4 | +on: |
| 5 | + issues: |
| 6 | + types: [ closed ] |
| 7 | +jobs: |
| 8 | + tidy: |
| 9 | + name: Tidy closed issues |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/github-script@v5 |
| 13 | + with: |
| 14 | + # PAT needed as the GITHUB_TOKEN won't be able to see cross-references from other orgs (matrix-org) |
| 15 | + github-token: ${{ secrets.ELEMENT_BOT_TOKEN }} |
| 16 | + script: | |
| 17 | + const variables = { |
| 18 | + owner: context.repo.owner, |
| 19 | + name: context.repo.repo, |
| 20 | + number: context.issue.number, |
| 21 | + }; |
| 22 | +
|
| 23 | + const query = `query($owner:String!, $name:String!, $number:Int!) { |
| 24 | + repository(owner: $owner, name: $name) { |
| 25 | + issue(number: $number) { |
| 26 | + stateReason, |
| 27 | + timelineItems(first: 100, itemTypes: [MARKED_AS_DUPLICATE_EVENT, UNMARKED_AS_DUPLICATE_EVENT, CROSS_REFERENCED_EVENT]) { |
| 28 | + edges { |
| 29 | + node { |
| 30 | + __typename |
| 31 | + ... on MarkedAsDuplicateEvent { |
| 32 | + canonical { |
| 33 | + ... on Issue { |
| 34 | + repository { |
| 35 | + nameWithOwner |
| 36 | + } |
| 37 | + number |
| 38 | + } |
| 39 | + ... on PullRequest { |
| 40 | + repository { |
| 41 | + nameWithOwner |
| 42 | + } |
| 43 | + number |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + ... on UnmarkedAsDuplicateEvent { |
| 48 | + canonical { |
| 49 | + ... on Issue { |
| 50 | + repository { |
| 51 | + nameWithOwner |
| 52 | + } |
| 53 | + number |
| 54 | + } |
| 55 | + ... on PullRequest { |
| 56 | + repository { |
| 57 | + nameWithOwner |
| 58 | + } |
| 59 | + number |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + ... on CrossReferencedEvent { |
| 64 | + source { |
| 65 | + ... on Issue { |
| 66 | + repository { |
| 67 | + nameWithOwner |
| 68 | + } |
| 69 | + number |
| 70 | + } |
| 71 | + ... on PullRequest { |
| 72 | + repository { |
| 73 | + nameWithOwner |
| 74 | + } |
| 75 | + number |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + }`; |
| 85 | +
|
| 86 | + const result = await github.graphql(query, variables); |
| 87 | + const { stateReason, timelineItems: { edges } } = result.repository.issue; |
| 88 | +
|
| 89 | + const RAGESHAKE_OWNER = "matrix-org"; |
| 90 | + const RAGESHAKE_REPO = "element-web-rageshakes"; |
| 91 | + const rageshakes = new Set(); |
| 92 | + const duplicateOf = new Set(); |
| 93 | +
|
| 94 | + console.log("Edges: ", JSON.stringify(edges)); |
| 95 | +
|
| 96 | + for (const { node } of edges) { |
| 97 | + switch(node.__typename) { |
| 98 | + case "MarkedAsDuplicateEvent": |
| 99 | + duplicateOf.add(node.canonical.repository.nameWithOwner + "#" + node.canonical.number); |
| 100 | + break; |
| 101 | + case "UnmarkedAsDuplicateEvent": |
| 102 | + duplicateOf.remove(node.canonical.repository.nameWithOwner + "#" + node.canonical.number); |
| 103 | + break; |
| 104 | + case "CrossReferencedEvent": |
| 105 | + if (node.source.repository.nameWithOwner === (RAGESHAKE_OWNER + "/" + RAGESHAKE_REPO)) { |
| 106 | + rageshakes.add(node.source.number); |
| 107 | + } |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | +
|
| 112 | + console.log("Duplicate of: ", duplicateOf); |
| 113 | + console.log("Found rageshakes: ", rageshakes); |
| 114 | +
|
| 115 | + if (duplicateOf.size) { |
| 116 | + const body = Array.from(duplicateOf).join("\n"); |
| 117 | +
|
| 118 | + // Comment on all rageshakes to create relationship to the issue this was closed as duplicate of |
| 119 | + for (const rageshake of rageshakes) { |
| 120 | + github.rest.issues.createComment({ |
| 121 | + owner: RAGESHAKE_OWNER, |
| 122 | + repo: RAGESHAKE_REPO, |
| 123 | + issue_number: rageshake, |
| 124 | + body, |
| 125 | + }); |
| 126 | + } |
| 127 | +
|
| 128 | + // Duplicate was closed with wrong reason, fix it |
| 129 | + if (stateReason === "COMPLETED") { |
| 130 | + await github.graphql(`mutation($id:ID!) { |
| 131 | + closeIssue(input: { issueId:$id, stateReason:NOT_PLANNED }) { |
| 132 | + clientMutationId |
| 133 | + } |
| 134 | + }`, { |
| 135 | + id: context.payload.issue.node_id, |
| 136 | + }); |
| 137 | + } |
| 138 | + } else { |
| 139 | + // This issue was closed, close all related rageshakes |
| 140 | + for (const rageshake of rageshakes) { |
| 141 | + github.rest.issues.update({ |
| 142 | + owner: RAGESHAKE_OWNER, |
| 143 | + repo: RAGESHAKE_REPO, |
| 144 | + issue_number: rageshake, |
| 145 | + state: "closed", |
| 146 | + }); |
| 147 | + } |
| 148 | + } |
0 commit comments