├── .gitignore ├── Dockerfile ├── action.yml ├── LICENSE ├── .github └── workflows │ └── main.yml ├── entrypoint.sh └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.sublime* 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Container image that runs your code 2 | FROM alpine:3.10 3 | 4 | RUN apk add --no-cache --no-progress curl jq 5 | 6 | # Copies your code file from your action repository to the filesystem path `/` of the container 7 | COPY entrypoint.sh /entrypoint.sh 8 | # Code file to execute when the docker container starts up (`entrypoint.sh`) 9 | ENTRYPOINT ["/entrypoint.sh"] 10 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | # action.yml 2 | name: 'Assign to One Project' 3 | description: 'Assign new/labeled Issue or Pull Request to a specific project dashboard column' 4 | author: srggrs 5 | inputs: 6 | project: 7 | description: 'The url of the project to be assigned to.' 8 | required: true 9 | project_id: 10 | description: 'The id of the project to be assigned to.' 11 | required: true 12 | column_name: 13 | description: 'The column name of the project, defaults to "To do" for issues and "In progress" for pull requests.' 14 | required: false 15 | 16 | runs: 17 | using: 'docker' 18 | image: 'docker://alexanderwert/assign-one-project-github-action:1.2.2' 19 | args: 20 | - ${{ inputs.project }} 21 | - ${{ inputs.project_id }} 22 | - ${{ inputs.column_name }} 23 | 24 | branding: 25 | icon: 'box' 26 | color: 'red' 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Sergio Pintaldi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | # name: Auto Assign to Project(s) 2 | 3 | # on: 4 | # issues: 5 | # types: [opened, labeled] 6 | # pull_request: 7 | # types: [opened, labeled] 8 | # env: 9 | # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 10 | 11 | # jobs: 12 | # assign_one_project: 13 | # runs-on: ubuntu-latest 14 | # name: Assign to One Project 15 | # steps: 16 | # - name: Assign NEW issues and NEW pull requests to project 2 17 | # uses: srggrs/assign-one-project-github-action@1.2.0 18 | # if: github.event.action == 'opened' 19 | # with: 20 | # project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2' 21 | 22 | # - name: Assign issues and pull requests with `bug` label to project 3 23 | # uses: srggrs/assign-one-project-github-action@1.2.0 24 | # if: | 25 | # contains(github.event.issue.labels.*.name, 'bug') || 26 | # contains(github.event.pull_request.labels.*.name, 'bug') 27 | # with: 28 | # project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3' 29 | # column_name: 'Labeled' 30 | name: Debug JSON 31 | 32 | on: 33 | issues: 34 | types: [opened, labeled, unlabeled] 35 | pull_request: 36 | types: [opened, labeled, unlabeled] 37 | env: 38 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 39 | 40 | jobs: 41 | debugging: 42 | runs-on: ubuntu-latest 43 | name: Debugging 44 | steps: 45 | - name: Debugging JSON 46 | run: | 47 | echo "Event Type: ${{ github.event_name }}" 48 | env 49 | cat $GITHUB_EVENT_PATH 50 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -l 2 | 3 | PROJECT_URL="$INPUT_PROJECT" 4 | if [ -z "$PROJECT_URL" ]; then 5 | echo "PROJECT_URL is not defined." >&2 6 | exit 1 7 | fi 8 | 9 | get_project_type() { 10 | _PROJECT_URL="$1" 11 | 12 | case "$_PROJECT_URL" in 13 | https://github.com/orgs/*) 14 | echo "org" 15 | ;; 16 | https://github.com/users/*) 17 | echo "user" 18 | ;; 19 | https://github.com/*/projects/*) 20 | echo "repo" 21 | ;; 22 | *) 23 | echo "Invalid PROJECT_URL: $_PROJECT_URL" >&2 24 | exit 1 25 | ;; 26 | esac 27 | 28 | unset _PROJECT_URL 29 | } 30 | 31 | find_column_id() { 32 | _PROJECT_ID="$1" 33 | _INITIAL_COLUMN_NAME="$2" 34 | 35 | _COLUMNS=$(curl -s -X GET -u "$GITHUB_ACTOR:$TOKEN" --retry 3 \ 36 | -H 'Accept: application/vnd.github.inertia-preview+json' \ 37 | "https://api.github.com/projects/$_PROJECT_ID/columns") 38 | 39 | 40 | echo "$_COLUMNS" | jq -r ".[] | select(.name == \"$_INITIAL_COLUMN_NAME\").id" 41 | unset _PROJECT_ID _INITIAL_COLUMN_NAME _COLUMNS 42 | } 43 | 44 | PROJECT_TYPE=$(get_project_type "${PROJECT_URL:? required this environment variable}") 45 | 46 | if [ "$PROJECT_TYPE" = org ] || [ "$PROJECT_TYPE" = user ]; then 47 | if [ -z "$MY_GITHUB_TOKEN" ]; then 48 | echo "MY_GITHUB_TOKEN not defined" >&2 49 | exit 1 50 | fi 51 | 52 | TOKEN="$MY_GITHUB_TOKEN" # It's User's personal access token. It should be secret. 53 | else 54 | if [ -z "$GITHUB_TOKEN" ]; then 55 | echo "GITHUB_TOKEN not defined" >&2 56 | exit 1 57 | fi 58 | 59 | TOKEN="$GITHUB_TOKEN" # GitHub sets. The scope in only the repository containing the workflow file. 60 | fi 61 | 62 | INITIAL_COLUMN_NAME="$INPUT_COLUMN_NAME" 63 | if [ -z "$INITIAL_COLUMN_NAME" ]; then 64 | # assing the column name by default 65 | INITIAL_COLUMN_NAME='To do' 66 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then 67 | echo "changing col name for PR event" 68 | INITIAL_COLUMN_NAME='In progress' 69 | fi 70 | fi 71 | 72 | 73 | PROJECT_ID="$INPUT_PROJECT_ID" 74 | INITIAL_COLUMN_ID=$(find_column_id "$PROJECT_ID" "${INITIAL_COLUMN_NAME:? required this environment variable}") 75 | 76 | if [ -z "$INITIAL_COLUMN_ID" ]; then 77 | echo "INITIAL_COLUMN_ID is not found." >&2 78 | exit 1 79 | fi 80 | 81 | case "$GITHUB_EVENT_NAME" in 82 | issues) 83 | ISSUE_ID=$(jq -r '.issue.id' < "$GITHUB_EVENT_PATH") 84 | 85 | # Add this issue to the project column 86 | curl -s -X POST -u "$GITHUB_ACTOR:$TOKEN" --retry 3 \ 87 | -H 'Accept: application/vnd.github.inertia-preview+json' \ 88 | -d "{\"content_type\": \"Issue\", \"content_id\": $ISSUE_ID}" \ 89 | "https://api.github.com/projects/columns/$INITIAL_COLUMN_ID/cards" 90 | 91 | echo "Added issue to project" >&2 92 | ;; 93 | pull_request|pull_request_target) 94 | PULL_REQUEST_ID=$(jq -r '.pull_request.id' < "$GITHUB_EVENT_PATH") 95 | 96 | # Add this pull_request to the project column 97 | curl -s -X POST -u "$GITHUB_ACTOR:$TOKEN" --retry 3 \ 98 | -H 'Accept: application/vnd.github.inertia-preview+json' \ 99 | -d "{\"content_type\": \"PullRequest\", \"content_id\": $PULL_REQUEST_ID}" \ 100 | "https://api.github.com/projects/columns/$INITIAL_COLUMN_ID/cards" 101 | 102 | echo "Added pull request to project" >&2 103 | ;; 104 | *) 105 | echo "Nothing to be done on this action: $GITHUB_EVENT_NAME" >&2 106 | exit 1 107 | ;; 108 | esac 109 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitHub Action for Assign to One Project 2 | 3 | [![Docker Cloud Automated build](https://img.shields.io/docker/cloud/automated/srggrs/assign-one-project-github-action)][docker] 4 | [![Docker Cloud Build Status](https://img.shields.io/docker/cloud/build/srggrs/assign-one-project-github-action)][docker] 5 | [![Docker Pulls](https://img.shields.io/docker/pulls/srggrs/assign-one-project-github-action)][docker] 6 | [![Docker Stars](https://img.shields.io/docker/stars/srggrs/assign-one-project-github-action)][docker] 7 | [![GitHub license](https://img.shields.io/github/license/srggrs/assign-one-project-github-action.svg)][license] 8 | 9 | [docker]: https://hub.docker.com/r/srggrs/assign-one-project-github-action 10 | [license]: https://github.com/srggrs/assign-one-project-github-action/blob/master/LICENSE 11 | 12 | Automatically add an issue or pull request to specific [GitHub Project](https://help.github.com/articles/about-project-boards/) when you __create__ and/or __label__ them. By default, the issues are assigned to the `To do` column and the pull requests to the `In progress` one, so make sure you have those columns in your project dashboard. But the workflow allowed you to specify the column name as input, so you can assign the issues/PRs based on a set of conditions to a specific column of a specific project. 13 | 14 | ## Acknowledgment & Motivations 15 | 16 | This action has been modified from the original action from [masutaka](https://github.com/masutaka/github-actions-all-in-one-project). I needed to fix it as the original docker container would not build. Also I think the GitHub Action syntax changed a bit. 17 | 18 | I would like to thank @SunRunAway for adding the labelling functionality and custom column input. 19 | 20 | ## Inputs 21 | 22 | ### `project` 23 | 24 | **Required** The url of the project to be assigned to. 25 | 26 | ### `column_name` 27 | 28 | The column name of the project, defaults to `'To do'` for issues and `'In progress'` for pull requests. 29 | 30 | ## Example usage 31 | 32 | Examples of action: 33 | 34 | ### Repository project 35 | 36 | ```yaml 37 | name: Auto Assign to Project(s) 38 | 39 | on: 40 | issues: 41 | types: [opened, labeled] 42 | pull_request: 43 | types: [opened, labeled] 44 | env: 45 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 46 | 47 | jobs: 48 | assign_one_project: 49 | runs-on: ubuntu-latest 50 | name: Assign to One Project 51 | steps: 52 | - name: Assign NEW issues and NEW pull requests to project 2 53 | uses: srggrs/assign-one-project-github-action@1.2.0 54 | if: github.event.action == 'opened' 55 | with: 56 | project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2' 57 | 58 | - name: Assign issues and pull requests with `bug` label to project 3 59 | uses: srggrs/assign-one-project-github-action@1.2.0 60 | if: | 61 | contains(github.event.issue.labels.*.name, 'bug') || 62 | contains(github.event.pull_request.labels.*.name, 'bug') 63 | with: 64 | project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3' 65 | column_name: 'Labeled' 66 | ``` 67 | 68 | #### __Notes__ 69 | Be careful of using the coditions above (opened and labeled issues/PRs) because in such workflow, if the issue/PR is opened and labeled at the same time, it will be assigned to __both__ projects! 70 | 71 | 72 | You can use any combination of conditions. For example, to assign new issues or issues labeled with 'mylabel' to a project column, use: 73 | ```yaml 74 | ... 75 | 76 | if: | 77 | github.event == 'issue' && 78 | ( 79 | github.event.action == 'opened' || 80 | contains(github.event.issue.labels.*.name, 'mylabel') 81 | ) 82 | ... 83 | ``` 84 | 85 | ### Organisation or User project 86 | 87 | Generate a token from the Organisation settings or User Settings and add it as a secret in the repository secrets as `MY_GITHUB_TOKEN` 88 | 89 | ```yaml 90 | name: Auto Assign to Project(s) 91 | 92 | on: 93 | issues: 94 | types: [opened, labeled] 95 | pull_request: 96 | types: [opened, labeled] 97 | env: 98 | MY_GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }} 99 | 100 | jobs: 101 | assign_one_project: 102 | runs-on: ubuntu-latest 103 | name: Assign to One Project 104 | steps: 105 | - name: Assign NEW issues and NEW pull requests to project 2 106 | uses: srggrs/assign-one-project-github-action@1.2.0 107 | if: github.event.action == 'opened' 108 | with: 109 | project: 'https://github.com/srggrs/assign-one-project-github-action/projects/2' 110 | 111 | - name: Assign issues and pull requests with `bug` label to project 3 112 | uses: srggrs/assign-one-project-github-action@1.2.0 113 | if: | 114 | contains(github.event.issue.labels.*.name, 'bug') || 115 | contains(github.event.pull_request.labels.*.name, 'bug') 116 | with: 117 | project: 'https://github.com/srggrs/assign-one-project-github-action/projects/3' 118 | column_name: 'Labeled' 119 | ``` 120 | --------------------------------------------------------------------------------