0

有没有办法检测项目中是否已经存在问题,例如if: ${{ !contains(github.event.issue.projects.*.name, 'Board') }}

这是我的情况:我有一个动作,它会自动将新问题移动到我的项目“Board”中。但是只有在新问题不在项目“Board”中时才应该运行此操作。

这是当前的操作:

name: new-issue-automation

on:
  issues:
    types: [opened]
    
env:
  BOARD_NAME: "Board"

jobs:
  move-new-issues-to-backlog:
    # We don't want to move P0 issues to the backlog column
    if: ${{ !contains(github.event.issue.labels.*.name, 'P0') }}
    runs-on: ubuntu-latest
    steps:
      - uses: alex-page/github-project-automation-plus@v0.8.0
        with:
          project: ${{ env.BOARD_NAME }}
          column: "Backlog"
          # The normal GITHUB_TOKEN should have enough permission to edit the board. However, it seems so, 
          # that there is a GitHub bug. As a workaround we add an Personal GitHub Token (Nils Token)
          repo-token: ${{ secrets.PROJECT_AUTOMATION_PLUS_TOKEN }}

但我查看了 GitHub 上下文的属性,没有“项目”属性:

{
  "token": "***",
  "job": "one",
  "ref": "refs/heads/main",
  "sha": "a53e94dd74818acf6dcc60a433488b4573008867",
  "repository": "nilsreichardt/playground",
  "repository_owner": "nilsreichardt",
  "repositoryUrl": "git://github.com/nilsreichardt/playground.git",
  "run_id": "1033281712",
  "run_number": "1",
  "retention_days": "90",
  "actor": "nilsreichardt",
  "workflow": ".github/workflows/issue_test.yml",
  "head_ref": "",
  "base_ref": "",
  "event_name": "issues",
  "event": {
    "action": "opened",
    "issue": {
      "active_lock_reason": null,
      "assignee": null,
      "assignees": [],
      "author_association": "OWNER",
      "body": "",
      "closed_at": null,
      "comments": 0,
      "comments_url": "https://api.github.com/repos/nilsreichardt/playground/issues/19/comments",
      "created_at": "2021-07-15T08:53:48Z",
      "events_url": "https://api.github.com/repos/nilsreichardt/playground/issues/19/events",
      "html_url": "https://github.com/nilsreichardt/playground/issues/19",
      "id": 945154403,
      "labels": [],
      "labels_url": "https://api.github.com/repos/nilsreichardt/playground/issues/19/labels{/name}",
      "locked": false,
      "milestone": null,
      "node_id": "MDU6SXNzdWU5NDUxNTQ0MDM=",
      "number": 19,
      "performed_via_github_app": null,
      "repository_url": "https://api.github.com/repos/nilsreichardt/playground",
      "state": "open",
      "title": "test",
      "updated_at": "2021-07-15T08:53:48Z",
      "url": "https://api.github.com/repos/nilsreichardt/playground/issues/19",
      "user": {
        "avatar_url": "https://avatars.githubusercontent.com/u/24459435?v=4",
        "events_url": "https://api.github.com/users/nilsreichardt/events{/privacy}",
        "followers_url": "https://api.github.com/users/nilsreichardt/followers",
        "following_url": "https://api.github.com/users/nilsreichardt/following{/other_user}",
        "gists_url": "https://api.github.com/users/nilsreichardt/gists{/gist_id}",
        "gravatar_id": "",
        "html_url": "https://github.com/nilsreichardt",
        "id": 24459435,
        "login": "nilsreichardt",
        "node_id": "MDQ6VXNlcjI0NDU5NDM1",
        "organizations_url": "https://api.github.com/users/nilsreichardt/orgs",
        "received_events_url": "https://api.github.com/users/nilsreichardt/received_events",
        "repos_url": "https://api.github.com/users/nilsreichardt/repos",
        "site_admin": false,
        "starred_url": "https://api.github.com/users/nilsreichardt/starred{/owner}{/repo}",
        "subscriptions_url": "https://api.github.com/users/nilsreichardt/subscriptions",
        "type": "User",
        "url": "https://api.github.com/users/nilsreichardt"
      }
    },
[...]

有没有办法在行动中检查项目?

4

3 回答 3

0

@Nils Reichardt的回答已经非常有帮助了。但由于我还不能评论,我想在这个答案中指出,存在@octokit/graphqloctokit/graphql-action@v2.x,这使得编程更加舒适(例如,该if curl ...行是变得非常冗长)。

于 2021-11-23T08:35:20.913 回答
0

自上次回复以来,此操作已更新。

根据:https ://github.com/alex-page/github-project-automation-plus/pull/71

当您想防止已经在板上的卡片移动到其他列时,请使用此选项。

name: new-issue-automation

on:
  issues:
    types: [opened]
    
env:
  BOARD_NAME: "Board"

jobs:
  move-new-issues-to-backlog:
    # We don't want to move P0 issues to the backlog column
    if: ${{ !contains(github.event.issue.labels.*.name, 'P0') }}
    runs-on: ubuntu-latest
    steps:
      - uses: alex-page/github-project-automation-plus@v0.8.1
        with:
          project: ${{ env.BOARD_NAME }}
          action: add
          column: "Backlog"
          # The normal GITHUB_TOKEN should have enough permission to edit the board. However, it seems so, 
          # that there is a GitHub bug. As a workaround we add an Personal GitHub Token (Nils Token)
          repo-token: ${{ secrets.PROJECT_AUTOMATION_PLUS_TOKEN }}
于 2022-01-04T22:03:13.207 回答
0

正如@GuiFalourd建议的那样,我可以使用GitHub API(我需要使用 GraphQL API,因为 REST API 不显示问题的链接项目)首先检查问题是否已经在董事会中。

这是我的完整工作流程:

name: new-issue-automation

on:
  issues:
    types: [opened]
    
jobs:
  move-new-issues-to-backlog:
    # We not want to move P0 issues to the backlog column
    if: ${{ !contains(github.event.issue.labels.*.name, 'P0') }}
    runs-on: ubuntu-latest
    env:
      BOARD_NAME: "Board"
      OWNER: ${{ github.repository_owner }}
      REPO: ${{ github.event.repository.name }}
      ISSUE: ${{ github.event.issue.number }}
    steps:
      - name: Check if issue is already in "${{ env.BOARD_NAME }}"
        run: |
          if curl -i -H 'Content-Type: application/json' -H "Authorization: bearer ${{ secrets.GITHUB_TOKEN }}" -X POST -d '{"query": "query($issue: Int!, $owner: String!, $repo: String!) { repository(owner: $owner, name: $repo) { issue(number: $issue) { projectCards { nodes { project { name } } } } } } ", "variables" : "{ \"issue\": '${ISSUE}', \"owner\": \"'${OWNER}'\", \"repo\": \"'${REPO}'\" }" }' https://api.github.com/graphql | grep "\b$BOARD_NAME\b"; then
            echo "Issue is already in Project '$BOARD_NAME', cancelling this workflow";
            echo "ALREADY_IN_BOARD=true" >> $GITHUB_ENV
          else
            echo "Issue is not in project '$BOARD_NAME', adding it to $BOARD_NAME."
            echo "ALREADY_IN_BOARD=false" >> $GITHUB_ENV
          fi
      - uses: alex-page/github-project-automation-plus@v0.8.0
        if: ${{ env.ALREADY_IN_BOARD == 'false' }}
        with:
          project: ${{ env.BOARD_NAME }}
          column: "Backlog"
          # The normal GITHUB_TOKEN can not be used inside a private repo. So we
          # have to use a Personal GitHub Token. See more information:
          # https://github.com/alex-page/github-project-automation-plus#personal-access-token
          repo-token: ${{ secrets.PROJECT_AUTOMATION_PLUS_TOKEN }}
于 2021-07-26T16:11:37.720 回答