Closed1

Issue が特定の github project に入っているか確認する && label を付与する

tkttkt
name: Add label to issue/PR if it is in HOGE project

on:
  issues:
    types: [opened, closed, reopened]
  pull_request:
    types: [opened, closed, reopened]

env:
    GITHUB_TOKEN: ${{ secrets.PAT }}
    YOUR_PROJECT_ID: ${{ secrets.PROJECT_ID }}
    LABEL_TO_ADD: "LABEL_NAME"

jobs:
  check_project_existence:
    runs-on: ubuntu-latest
    outputs:
      exists_in_project: ${{ steps.check.outputs.result }}
    steps:
      - name: Check if item is in project using GraphQL
        id: check
        uses: actions/github-script@v6
        with:
          github-token: ${{ env.GITHUB_TOKEN }}
          script: |
            const issueOrPrNumber = context.issue ? context.issue.number : context.pull_request.number;
            const projectIdToCheck = ${{ env.YOUR_PROJECT_ID }};

            // GraphQL query to fetch associated project numbers for the issue/PR
            const query = `
              query($owner: String!, $repo: String!, $number: Int!) {
                repository(owner: $owner, name: $repo) {
                  issueOrPullRequest(number: $number) {
                    ... on Issue {
                      projectsV2(first: 100) {
                        nodes {
                          number
                        }
                      }
                    }
                    ... on PullRequest {
                      projectsV2(first: 100) {
                        nodes {
                          number
                        }
                      }
                    }
                  }
                }
              }
            `;

            // Fetch associated project numbers for the issue/PR
            const { repository: { issueOrPullRequest: { projectsV2: { nodes } } } } = await github.graphql(query, {
              owner: context.repo.owner,
              repo: context.repo.repo,
              number: issueOrPrNumber
            });

            // Check if the issue/PR is in the specified project
            const isInProject = nodes.some(project => project.number === projectIdToCheck);

            // debug
            console.log(`Is issue/PR in project? ${isInProject}`);

            // Output the result
            return isInProject;

  add_label:
    needs: check_project_existence
    runs-on: ubuntu-latest
    if: needs.check_project_existence.outputs.exists_in_project == 'true'
    steps:
      - name: Add label
        uses: actions/github-script@v6
        with:
          github-token: ${{ env.GITHUB_TOKEN }}
          script: |
            const itemNumber = context.issue ? context.issue.number : context.pull_request.number;
            const labelStrToAdd = `${{ env.LABEL_TO_ADD }}`;

            // get label id
            const query = `
              query($owner: String!, $repo: String!, $labelStrToAdd: String!, $issueOrPrNumber: Int!) {
                repository(owner: $owner, name: $repo) {
                  label(name: $labelStrToAdd) {
                    id
                  }
                  issueOrPullRequest(number: $issueOrPrNumber) {
                    ... on Issue {
                      id
                    }
                    ... on PullRequest {
                      id
                    }
                  }
                }
              }
            `;
            const { repository: { label: { id: labelId }, issueOrPullRequest: { id: issueOrPrId } } } = await github.graphql(query, {
              owner: context.repo.owner,
              repo: context.repo.repo,
              labelStrToAdd,
              issueOrPrNumber: itemNumber
            });

            // add label
            await github.graphql(`
              mutation($labelId: ID!, $issueOrPrId: ID!) {
                addLabelsToLabelable(input: { labelIds: [$labelId], labelableId: $issueOrPrId }) {
                  clientMutationId
                }
              }
            `, {
              labelId,
              issueOrPrId
            });
このスクラップは2023/10/20にクローズされました