Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from masutaka/user-owned-project
Browse files Browse the repository at this point in the history
Support user owned project
  • Loading branch information
masutaka authored Feb 16, 2019
2 parents 22a0aa0 + 19b9672 commit 697b4d6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 15 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,55 @@ action "Add a pull_request to project" {
}
```

### User owned project

1. Set the URL of User owned project to `PROJECT_URL`
1. Set column name you want issue/pull_request at the beginning to `INITIAL_COLUMN_NAME`
1. Set secrets `MY_GITHUB_TOKEN`
1. Create personal access token with `repo` scope on https://github.com/settings/tokens
1. Create secret `MY_GITHUB_TOKEN` on https://github.com/USER/REPO_NAME/settings/secrets. The value is same to personal access token you created the above
1. Set `MY_GITHUB_TOKEN` to `secrets` as follows:

#### For issues

```hcl
workflow "issues" {
on = "issues"
resolves = ["Add an issue to project"]
}
action "Add an issue to project" {
uses = "docker://masutaka/github-actions-all-in-one-project"
secrets = ["MY_GITHUB_TOKEN"]
args = ["issue"]
env = {
PROJECT_URL = "https://github.com/users/masutaka/projects/2"
INITIAL_COLUMN_NAME = "To do"
}
}
```

#### For pull requests

```hcl
workflow "pull_requests" {
on = "pull_request"
resolves = ["Add a pull_request to project"]
}
action "Add a pull_request to project" {
uses = "docker://masutaka/github-actions-all-in-one-project"
secrets = ["MY_GITHUB_TOKEN"]
args = ["pull_request"]
env = {
PROJECT_URL = "https://github.com/users/masutaka/projects/2"
INITIAL_COLUMN_NAME = "In progress"
}
}
```

### Organization-wide project

1. Set the URL of Organization-wide project to `PROJECT_URL`
Expand Down
52 changes: 37 additions & 15 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,57 @@ if [ "$ACTION" != opened ]; then
exit 0
fi

get_org_name() {
get_project_type() {
_PROJECT_URL="$1"

if echo "$_PROJECT_URL" | grep -qF 'https://github.com/orgs/'; then
echo "$_PROJECT_URL" | sed -e 's@https://github.com/orgs/\([^/]\+\)/projects/[0-9]\+@\1@'
fi
case "$_PROJECT_URL" in
https://github.com/orgs/*)
echo "org"
;;
https://github.com/users/*)
echo "user"
;;
https://github.com/*/projects/*)
echo "repo"
;;
*)
echo "Invalid PROJECT_URL: $_PROJECT_URL" >&2
false
;;
esac

unset _PROJECT_URL
}

find_project_id() {
_ORG_NAME="$1"
_PROJECT_TYPE="$1"
_PROJECT_URL="$2"

if [ "$_ORG_NAME" ]; then
_ENDPOINT="https://api.github.com/orgs/$_ORG_NAME/projects"
else
_ENDPOINT="https://api.github.com/repos/$GITHUB_REPOSITORY/projects"
fi
case "$_PROJECT_TYPE" in
org)
_ORG_NAME=$(echo "$_PROJECT_URL" | sed -e 's@https://github.com/orgs/\([^/]\+\)/projects/[0-9]\+@\1@')
_ENDPOINT="https://api.github.com/orgs/$_ORG_NAME/projects"
;;
user)
_USER_NAME=$(echo "$_PROJECT_URL" | sed -e 's@https://github.com/users/\([^/]\+\)/projects/[0-9]\+@\1@')
_ENDPOINT="https://api.github.com/users/$_USER_NAME/projects"
;;
repo)
_ENDPOINT="https://api.github.com/repos/$GITHUB_REPOSITORY/projects"
;;
esac

_PROJECTS=$(curl -s -X GET -u "$GITHUB_ACTOR:$TOKEN" --retry 3 \
-H 'Accept: application/vnd.github.inertia-preview+json' \
"$_ENDPOINT")

if [ "$(echo "$_PROJECTS" | jq '. | length == 0')" = true ]; then
echo "No project was found." >&2
return 1
false
fi

echo "$_PROJECTS" | jq -r ".[] | select(.html_url == \"$_PROJECT_URL\").id"
unset _PROJECT_URL _ORG_NAME _ENDPOINT _PROJECTS
unset _PROJECT_TYPE _PROJECT_URL _ORG_NAME _USER_NAME _ENDPOINT _PROJECTS
}

find_column_id() {
Expand All @@ -53,15 +75,15 @@ find_column_id() {
unset _PROJECT_ID _INITIAL_COLUMN_NAME _COLUMNS
}

ORG_NAME=$(get_org_name "${PROJECT_URL:?<Error> required this environment variable}")
PROJECT_TYPE=$(get_project_type "${PROJECT_URL:?<Error> required this environment variable}")

if [ "$ORG_NAME" ]; then
if [ "$PROJECT_TYPE" = org ] || [ "$PROJECT_TYPE" = repo ]; then
TOKEN="$MY_GITHUB_TOKEN" # It's User's personal access token. It should be secret.
else
TOKEN="$GITHUB_TOKEN" # GitHub sets. The scope in only the repository containing the workflow file.
fi

PROJECT_ID=$(find_project_id "$ORG_NAME" "$PROJECT_URL")
PROJECT_ID=$(find_project_id "$PROJECT_TYPE" "$PROJECT_URL")
INITIAL_COLUMN_ID=$(find_column_id "$PROJECT_ID" "${INITIAL_COLUMN_NAME:?<Error> required this environment variable}")

if [ -z "$INITIAL_COLUMN_ID" ]; then
Expand Down

0 comments on commit 697b4d6

Please sign in to comment.