Skip to content

Commit

Permalink
Support GitHub Actions (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
at-wat authored Oct 7, 2020
1 parent f029672 commit 62b7914
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test
on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
- name: Post comment
run: go run ./cmd/gh-pr-comment "Test from GitHub Actions" "Comment from GitHub Actions"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
13 changes: 12 additions & 1 deletion cmd/gh-pr-comment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"golang.org/x/oauth2"

"github.com/at-wat/gh-pr-comment/pkg/cienv"
_ "github.com/at-wat/gh-pr-comment/pkg/cienv/travis"
_ "github.com/at-wat/gh-pr-comment/pkg/cienv/githubactions"
_ "github.com/at-wat/gh-pr-comment/pkg/cienv/travisci"
)

func main() {
Expand All @@ -27,6 +28,16 @@ env for Travis-CI:
owner/repos (TRAVIS_REPO_SLUG is used if not set)
TRAVIS_PULL_REQUEST:
pull request number
env for GitHub Actions:
GITHUB_ACTIONS:
must be true
GITHUB_REPOSITORY:
owner/repos (TRAVIS_REPO_SLUG is used if not set)
GITHUB_EVENT_NAME:
action event name
GITHUB_EVENT_PATH:
path to webhook payload
`)
os.Exit(1)
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/cienv/githubactions/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package githubactions

import (
"encoding/json"
"errors"
"fmt"
"os"

"github.com/at-wat/gh-pr-comment/pkg/cienv"
)

func init() {
cienv.Register(&Actions{})
}

type Actions struct {
}

func (t *Actions) Name() string {
return "GitHub Actions"
}

func (t *Actions) Detect() (*cienv.CIEnv, error) {
env := &cienv.CIEnv{}

if isActions, ok := os.LookupEnv("GITHUB_ACTIONS"); !ok || isActions != "true" {
return nil, cienv.ErrNotDetected
}

eventName, ok := os.LookupEnv("GITHUB_EVENT_NAME")
if !ok {
return nil, errors.New("GITHUB_EVENT_NAME is not set")
}
env.IsPullRequest = eventName == "pull_request"

if env.IsPullRequest {
if prEvent, ok := os.LookupEnv("GITHUB_EVENT_PATH"); ok {
f, err := os.Open(prEvent)
if err != nil {
return nil, fmt.Errorf("failed to read event payload: %w", err)
}
defer f.Close()
dec := json.NewDecoder(f)

var event pullRequestEvent
if err := dec.Decode(&event); err != nil {
return nil, fmt.Errorf("failed to parse event payload: %w", err)
}

env.PullRequest = event.Number
env.PullRequestSlug, err = cienv.NewSlug(event.PullRequest.Head.Repo.FullName)
if err != nil {
return nil, fmt.Errorf("failed to parse head.repo.full_name: %w", err)
}
} else {
return nil, errors.New("GITHUB_EVENT_PATH is not set")
}
}

if slug, ok := os.LookupEnv("GITHUB_REPOSITORY"); ok {
var err error
env.RepoSlug, err = cienv.NewSlug(slug)
if err != nil {
return nil, fmt.Errorf("failed to parse GITHUB_REPOSITORY: %w", err)
}
}

return env, nil
}

type pullRequestEvent struct {
Action string `json:"action"`
Number int `json:"number"`
PullRequest struct {
Head struct {
Repo struct {
FullName string `json:"full_name"`
} `json:"repo"`
} `json:"head"`
} `json:"pull_request"`
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package travis
package travisci

import (
"errors"
Expand Down

0 comments on commit 62b7914

Please sign in to comment.