0

Is there a way to detect, if an issue is already in a project, like if: ${{ !contains(github.event.issue.projects.*.name, 'Board') }}?

This is my situation: I have an action, which moves automatically new issues into my project "Board". But this action should only should run if the new issue isn't in the project "Board" already.

This is the currenct action:

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 }}

But I watched into the attributes of the GitHub Context and there is no "project" attribute:

{
  "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"
      }
    },
[...]

Is there a way to check the project in an action?

Nils Reichardt
  • 3,195
  • 2
  • 18
  • 28
  • The action documentation states that: _If the `pull_request` or `issue` card already exists it will be moved to the column provided. Otherwise the card will be created in the column._ So except if it's a duplicated ISSUE, it won't be added to the project again, it will just change the column. **Do you want to avoid duplicated issues on the project, or to avoid changing the column of the issue already inside the project?** – GuiFalourd Jul 16 '21 at 14:53
  • @GuiFalourd I want to avoid changing the column of the issue. We have a big project (called Board) with many columns. Normally, all items are added to the "backlog" column (this is the default case). But sometimes we know already when creating an issue, that we want to have this issue in "Design" or "Sprint" column. The current problem: The current workflow ignores the fact, that the issue is already in a column. So the action moves the created issue from "Design" column into "Backlog" and overrides the choice. So the goal is only run the workflow, if the isn't already in the Project "Board". – Nils Reichardt Jul 16 '21 at 19:23
  • 1
    Understood. A workaround could be to check if the issue card already exist in the project before using the action. Why not using 2 jobs in your workflow: The 1st that will always be executed and check this point (with a script consuming the github api for example), before setting a boolean as output according if the issue exists or not. Then a 2nd job that will only be executed according to the 1st job output (with an if expression) and that will use the `github-project-automation-plus` action? – GuiFalourd Jul 16 '21 at 22:14
  • Yes, this is a good idea! – Nils Reichardt Jul 26 '21 at 16:09

3 Answers3

0

As @GuiFalourd suggested, can I use the GitHub API (I needed to use the GraphQL API, because the REST API does not show the linked projects of an issue) to check first, if the issue is already in the board.

Here is my full workflow:

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 }}
Nils Reichardt
  • 3,195
  • 2
  • 18
  • 28
0

The answer by @Nils Reichardt was already very helpful. But as I can't yet comment, I want to point out in this answer, that there exists @octokit/graphql or octokit/graphql-action@v2.x, which makes it way more comfortable to program (e.g., the if curl ... line was getting really lengthy).

flipcc
  • 85
  • 6
0

There has been updates to this action since the last responses.

According to: https://github.com/alex-page/github-project-automation-plus/pull/71

Use this when you want to prevent cards already on the board from moving to other columns.

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 }}
Chris Mc
  • 445
  • 5
  • 9