Skip to content

Commit 9e6a26a

Browse files
authored
Merge pull request #16 from takanabe/pull_request_support
Pull request support
2 parents 7978a96 + 1b36586 commit 9e6a26a

File tree

10 files changed

+679
-104
lines changed

10 files changed

+679
-104
lines changed
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
name: Add a new GitHub issue to a designate project column
2-
on: issues
1+
name: Add a new GitHub Project card linked to a GitHub issue to a specified project column
2+
on: [issues, pull_request]
33
jobs:
44
add-new-issues-to-project-column:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@master
8-
- name: add-new-issues-to-project-column
9-
uses: ./
7+
- name: add-new-issues-to-repository-based-project-column
8+
uses: docker://takanabe/github-actions-automate-projects:sandbox
9+
if: github.event_name == 'issues' && github.event.action == 'opened'
1010
env:
1111
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12-
GITHUB_PROJECT_URL: https://github.com/takanabe/add-new-issues-to-project-column/projects/1
12+
GITHUB_PROJECT_URL: https://github.com/takanabe/github-actions-automate-projects/projects/1
1313
GITHUB_PROJECT_COLUMN_NAME: To do
14-
DEBUG: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313

1414
main
1515
add-new-issues-to-project-column
16+
github-actions-automate-projects
1617
.vscode/launch.json

Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
FROM takanabe/add-new-issues-to-project-column:0.1
1+
FROM takanabe/github-actions-automate-projects:sandbox
22

33
LABEL "com.github.actions.name"="Add new issues to a designate project column"
44
LABEL "com.github.actions.description"="GitHub Actions adding new issues to a specified project column automatically"
55
LABEL "com.github.actions.icon"="terminal"
66
LABEL "com.github.actions.color"="purple"
77

8-
LABEL "repository"="https://github.com/takanabe/add-new-issues-to-project-column"
9-
LABEL "homepage"="https://github.com/takanabe/add-new-issues-to-project-column"
8+
LABEL "repository"="https://github.com/takanabe/github-actions-automate-projects"
9+
LABEL "homepage"="https://github.com/takanabe/github-actions-automate-projects"
1010
LABEL "maintainer"="Takayuki Watanabe <[email protected]>"
1111

12-
CMD ["/app/main"]
12+
CMD ["/app/main"]

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
IMAGE_NAME=add-new-issues-to-project-column
2-
DOCKER_REPO=takanabe/add-new-issues-to-project-column
1+
IMAGE_NAME=github-actions-automate-projects
2+
DOCKER_REPO=takanabe/github-actions-automate-projects
33
TAG_NAME=sandbox
44

55
#
@@ -13,7 +13,7 @@ all: docker-build docker-tag docker-publish
1313
#
1414
.PHONY: docker-build
1515
docker-build:
16-
docker build -f Dockerfile.build . -t $(IMAGE_NAME):$(TAG_NAME)
16+
docker build --no-cache -f Dockerfile.build . -t $(IMAGE_NAME):$(TAG_NAME)
1717

1818
#
1919
# Build Docker image with DockerHub repository

README.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
# add-issues-to-project-column
1+
# github-actions-automate-projects
22

3-
![Docker Automated build](https://img.shields.io/docker/automated/takanabe/add-new-issues-to-project-column.svg?logo=docker)
4-
![License](https://img.shields.io/github/license/takanabe/add-new-issues-to-project-column.svg)
3+
[dockerhub]: https://hub.docker.com/r/takanabe/github-actions-automate-projects
4+
[license]: https://github.com/takanabe/github-actions-automate-projects/blob/master/LICENSE
55

6-
GitHub Actions adding new GitHub Issues to a specified GitHub Project column automatically :recycle:. This GitHub Action is inspired by https://github.com/masutaka/github-actions-all-in-one-project
6+
[![Docker Automated build](https://img.shields.io/docker/automated/takanabe/github-actions-automate-projects.svg?logo=docker)][dockerhub]
7+
[![License](https://img.shields.io/github/license/takanabe/github-actions-automate-projects.svg)][license]
8+
9+
GitHub Actions adding GitHub Issues & Pull requests to a specified GitHub Project column automatically :recycle:. This GitHub Action is inspired by https://github.com/masutaka/github-actions-all-in-one-project
710

811
## Usage
912

@@ -15,41 +18,50 @@ For any type of GitHub Projects, you need to change `GITHUB_PROJECT_URL` and `GI
1518

1619
```yml
1720
name: Add a new GitHub Project card linked to a GitHub issue to a specified project column
18-
on: issues
21+
on: [issues, pull_request]
1922
jobs:
20-
add-new-issues-to-project-column:
23+
github-actions-automate-projects:
2124
runs-on: ubuntu-latest
2225
steps:
2326
- name: add-new-issues-to-repository-based-project-column
24-
uses: docker://takanabe/add-new-issues-to-project-column:v0.0.1
27+
uses: docker://takanabe/github-actions-automate-projects:v0.0.1
28+
if: github.event_name == 'issues' && github.event.action == 'opened'
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
GITHUB_PROJECT_URL: https://github.com/takanabe/github-actions-automate-projects/projects/1
32+
GITHUB_PROJECT_COLUMN_NAME: To do
33+
- name: add-new-prs-to-repository-based-project-column
34+
uses: docker://takanabe/github-actions-automate-projects:v0.0.1
35+
if: github.event_name == 'pull_request' && github.event.action == 'opened'
2536
env:
2637
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27-
GITHUB_PROJECT_URL: https://github.com/takanabe/add-new-issues-to-project-column/projects/1
38+
GITHUB_PROJECT_URL: https://github.com/takanabe/github-actions-automate-projects/projects/1
2839
GITHUB_PROJECT_COLUMN_NAME: To do
2940
```
3041
31-
1. Replace the URL set on `GITHUB_PROJECT_URL` to the URL of your repository project to place issues
32-
1. Replace the URL set on `GITHUB_PROJECT_COLUMN_NAME` to the string which your repository project has and want to place issues
42+
1. Replace the URL set on `GITHUB_PROJECT_URL` to the URL of your repository project to place issues/pull-requests
43+
1. Replace the URL set on `GITHUB_PROJECT_COLUMN_NAME` to the string which your repository project has and want to place issues/pull-requests
3344

3445
### Organization-based project
3546

3647
```yml
3748
name: Add a new GitHub issue to a designate project column
38-
on: issues
49+
on: [issues, pull_request]
3950
jobs:
4051
add-new-issues-to-project-column:
4152
runs-on: ubuntu-latest
4253
steps:
4354
- name: add-new-issues-to-organization-based-project-column
44-
uses: docker://takanabe/add-new-issues-to-project-column:v0.0.1
55+
uses: docker://takanabe/github-actions-automate-projects:v0.0.1
56+
if: github.event_name == 'issues' && github.event.action == 'opened'
4557
env:
4658
GITHUB_TOKEN: ${{ secrets.GITHUB_PERSONAL_TOKEN_TO_ADD_PROJECT }}
4759
GITHUB_PROJECT_URL: https://github.com/orgs/organization_name/projects/1
48-
GITHUB_PROJECT_COLUMN_NAME: test
60+
GITHUB_PROJECT_COLUMN_NAME: To Do
4961
```
5062

51-
1. Replace the URL set on `GITHUB_PROJECT_URL` to the URL of your repository project to place issues
52-
1. Replace the URL set on `GITHUB_PROJECT_COLUMN_NAME` to the string which your repository project has and want to place issues
63+
1. Replace the URL set on `GITHUB_PROJECT_URL` to the URL of your repository project to place issues/pull-requests
64+
1. Replace the URL set on `GITHUB_PROJECT_COLUMN_NAME` to the string which your repository project has and want to place issues/pull-requests
5365
1. Replace the secret set on ${{ secrets.GITHUB_PERSONAL_TOKEN_TO_ADD_PROJECT }} to your personal GitHub token
5466
1. Create a new personal access token from https://github.com/settings/tokens
5567
1. Create a new personal access token from https://github.com/organization_name/repository_name/settings/secrets with the value of personal access token you created above
@@ -59,14 +71,25 @@ jobs:
5971

6072
User-based project is not supported yet
6173

74+
## Configurations
75+
6276
### Environment variables
6377

64-
| Environment variable | Value | Description |
65-
| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
66-
| GITHUB_TOKEN | ${{ secrets.GITHUB_TOKEN }}, ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }} | An Access token to access your issues and projects. if you use repository-based projects, ${{ secrets.GITHUB_TOKEN }} provides appropriate access privileges to this GitHub action ([See](https://help.github.com/en/articles/virtual-environments-for-github-actions#github_token-secret)). If that is not enough, you need to pass ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }} by issuing personal access token with appropriate grants. |
67-
| GITHUB_PROJECT_URL | https://github.com/username/reponame/projects/1, https://github.com/orgname/reponame/projects/1, https://github.com/orgs/orgname/projects/1 | A GitHub Project URL you want to use |
68-
| GITHUB_PROJECT_COLUMN_NAME | Anything (e.g: To Do) | A GitHub Project column name you want to place new issues |
69-
| DEBUG | Anything (e.g: true) | A flag to produce debug messages for this GitHub Actions if this environment variable exists |
78+
| Environment variable | Value | Description |
79+
| :------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
80+
| GITHUB_TOKEN | ${{ secrets.GITHUB_TOKEN }}, ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }} | An Access token to access your repository and projects. if you use repository-based projects, ${{ secrets.GITHUB_TOKEN }} provides appropriate access privileges to this GitHub action ([See](https://help.github.com/en/articles/virtual-environments-for-github-actions#github_token-secret)). If that is not enough, you need to pass ${{ secrets.GITHUB_PERSONAL_ACCESS_TOKEN }} by issuing personal access token with appropriate grants. |
81+
| GITHUB_PROJECT_URL | https://github.com/username/reponame/projects/1, https://github.com/orgname/reponame/projects/1, https://github.com/orgs/orgname/projects/1 | A GitHub Project URL you want to use |
82+
| GITHUB_PROJECT_COLUMN_NAME | Anything (e.g: To Do) | A GitHub Project column name you want to place issues/pull-requests |
83+
| DEBUG | Anything (e.g: true) | A flag to produce debug messages for this GitHub Actions if this environment variable exists |
84+
85+
### Condition with contexts
86+
87+
You can easily detect [event contexts](https://help.github.com/en/articles/contexts-and-expression-syntax-for-github-actions#github-context) and use them in if statements. Here are some lists of the useful contexts for this GitHub action.
88+
89+
| Property name | Values | Description |
90+
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
91+
| github.event.action | opened, closed, edited, and so on | The name of actions (references for [issues](https://developer.github.com/v3/activity/events/types/#issuesevent) and for [pull_request](https://developer.github.com/v3/activity/events/types/#pullrequestevent) |
92+
| github.event_name | [issues](https://developer.github.com/v3/activity/events/types/#webhook-event-name-19), [pull_quests](https://developer.github.com/v3/activity/events/types/#webhook-event-name-33) | The name of the event that triggered the workflow run |
7093

7194
## Development
7295

@@ -78,8 +101,8 @@ Change `IMAGE_NAME`, `DOCKER_REPO` and `TAG_NAME` in `Makefile` based on your Do
78101
make
79102
```
80103

81-
Except for `sandbox` tag, [`takanabe/add-new-issues-to-project-column`](https://hub.docker.com/r/takanabe/add-new-issues-to-project-column/tags) lists production ready Docker images matching [GitHub release tag](https://github.com/takanabe/add-new-issues-to-project-column/releases).
104+
Except for `sandbox` tag, [`takanabe/github-actions-automate-projects`](https://hub.docker.com/r/takanabe/github-actions-automate-projects/tags) lists production ready Docker images matching [GitHub release tag](https://github.com/takanabe/github-actions-automate-projects/releases).
82105

83106
## License
84107

85-
[Apache 2.0](https://github.com/takanabe/add-new-issues-to-project-column/blob/master/LICENSE)
108+
[Apache 2.0](https://github.com/takanabe/github-actions-automate-projects/blob/master/LICENSE)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/takanabe/add-new-issues-to-project-column
1+
module github.com/takanabe/github-actions-automate-projects
22

33
require (
44
github.com/google/go-github/v25 v25.0.4

issues.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/google/go-github/v25/github"
10+
"github.com/pkg/errors"
11+
)
12+
13+
// issueEventPayload returns GitHub issue event payload kept in the payload file .
14+
// GITHUB_EVENT_PATH keeps the path to the file that contains the payload of the event that triggered the workflow.
15+
// See: https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables
16+
func issueEventPayload() github.IssuesEvent {
17+
var jsonFilePath string
18+
_, ok := os.LookupEnv("GITHUB_ACTION_LOCAL")
19+
if ok {
20+
// Use local test payload
21+
// https://developer.github.com/v3/activity/events/types/#issuesevent
22+
var err error
23+
jsonFilePath, err = filepath.Abs("./payload/issue_event.json")
24+
if err != nil {
25+
errorLog(err)
26+
}
27+
} else {
28+
jsonFilePath = os.Getenv("GITHUB_EVENT_PATH")
29+
}
30+
jsonFile, err := os.Open(jsonFilePath)
31+
if err != nil {
32+
errorLog(errors.Wrap(err, "Failed to open json"))
33+
}
34+
defer jsonFile.Close()
35+
36+
// read opened jsonFile as a byte array.
37+
jsonByte, err := ioutil.ReadAll(jsonFile)
38+
if err != nil {
39+
errorLog(errors.Wrap(err, "Failed to read json as a byte array"))
40+
}
41+
42+
payload := github.IssuesEvent{}
43+
err = json.Unmarshal(jsonByte, &payload)
44+
if err != nil {
45+
errorLog(errors.Wrap(err, "Failed to unmarshal JSON to Go Object"))
46+
}
47+
if payload.GetAction() != "opened" {
48+
infoLog("GitHub action interupts!!")
49+
infoLog("This issue is not new one :D")
50+
os.Exit(0)
51+
}
52+
53+
return payload
54+
}
55+
56+
// extractIssueID returns GitHub issue ID extracted from event payloads
57+
func extractIssueID(payload github.IssuesEvent) (int64, error) {
58+
59+
issueID := payload.GetIssue().GetID()
60+
61+
if issueID == 0 {
62+
return 0, errors.New("Issue ID is 0. Failed to get issue id properly")
63+
}
64+
65+
return issueID, nil
66+
}

0 commit comments

Comments
 (0)