Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
superbrothers committed Feb 28, 2019
1 parent ded33b4 commit 6484755
Show file tree
Hide file tree
Showing 176 changed files with 54,876 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM golang:1.11-alpine

LABEL "com.github.actions.name"="Auto Close"
LABEL "com.github.actions.description"="Auto close pull requests"
LABEL "com.github.actions.icon"="slash"
LABEL "com.github.actions.color"="red"

WORKDIR $GOPATH/src/github.com/superbrothers/auto-close-action
COPY . .
RUN set -x && go build -o /action

ENTRYPOINT ["/action"]
74 changes: 74 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true

[[constraint]]
name = "github.com/google/go-github"
version ="v24.0.0"

[[constraint]]
name = "golang.org/x/oauth2"
branch = "master"

[prune]
go-tests = true
unused-packages = true
22 changes: 22 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2019 Kazuki Suda <kazuki.suda@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
REPO := superbrothers/auto-close-action

.PHONY: image
image:
docker build -t $(REPO) .
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# :no_entry_sign: Auto close pull requests GitHub Action

A GitHub Action to automatically close pull requests.

## Usage

This Action subscribes to `pull_request` events. When receiving a `pull_request` event, this action close the pull request triggered by the event immediately.

```workflow
workflow "New workflow" {
on = "pull_request"
resolves = ["Auto Close"]
}
action "Auto Close" {
uses = "superbrothers/auto-close-action"
env = {
# Optional. Post a issue comment just before closing a pull request.
COMMENT = "We do not accept PRs. If you have any questions, please feel free to connect us."
}
secrets = ["GITHUB_TOKEN"]
}
```

<img src="./docs/auto-close-action.png" width="400px">

## Environment variables

- `COMMENT` - *Optional*. Post an issue comment just before closing a pull request.

## LICENSE

This software is released under the MIT License.
Binary file added docs/auto-close-action.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

func main() {
eventName := os.Getenv("GITHUB_EVENT_NAME")
if eventName != "pull_request" {
fmt.Printf("Ignoring event %s (only listens for 'pull_request')\n", eventName)
return
}

ctx := context.Background()
client := newClient(ctx)

f, err := os.Open(os.Getenv("GITHUB_EVENT_PATH"))
if err != nil {
log.Fatal(err)
}

decorder := json.NewDecoder(f)
var triggerEvent github.PullRequestEvent
if err := decorder.Decode(&triggerEvent); err != nil {
log.Fatal(err)
}

fullRepo := os.Getenv("GITHUB_REPOSITORY")
repo := strings.SplitN(fullRepo, "/", 2)
if len(repo) != 2 {
log.Fatalf("Invalid repo name: %s\n", fullRepo)
}

number := *triggerEvent.Number

body := os.Getenv("COMMENT")
if len(body) > 0 {
issueComment := &github.IssueComment{Body: &body}
_, _, err = client.Issues.CreateComment(ctx, repo[0], repo[1], number, issueComment)
if err != nil {
log.Fatal(err)
}
}

state := "closed"
pr := &github.PullRequest{State: &state}
_, _, err = client.PullRequests.Edit(ctx, repo[0], repo[1], number, pr)
if err != nil {
log.Fatal(err)
}

log.Printf("Closed PR %d\n", number)
}

func newClient(ctx context.Context) *github.Client {
token := os.Getenv("GITHUB_TOKEN")

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

return github.NewClient(tc)
}
3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions vendor/github.com/golang/protobuf/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions vendor/github.com/golang/protobuf/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6484755

Please sign in to comment.