Skip to content

Commit

Permalink
Migrate to Go (#32)
Browse files Browse the repository at this point in the history
* Migrate gh-pr-comment to Go
* Migrate gh-pr-upload to Go
* Add install script
* Update README
  • Loading branch information
at-wat authored Aug 3, 2020
1 parent f7c09d6 commit 6f14a9d
Show file tree
Hide file tree
Showing 22 changed files with 713 additions and 327 deletions.
6 changes: 4 additions & 2 deletions tests/exec-test.sh → .ci/upload-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ file=$(mktemp)
echo "Test file" > ${file}
ALLOW_PUBLIC_UPLOADER=true IMAGE_UPLOADER=test gh-pr-upload ${file} \
&& upload=OK || upload=Failed
echo

gh-pr-comment "Test post (python ${TRAVIS_PYTHON_VERSION})" \
"testing comment post on python ${TRAVIS_PYTHON_VERSION}
gh-pr-comment "Test post" \
"testing comment post
- UTF-8 text: \"bœuf/牛\"
- upload test: ${upload}
"

[ ${upload} == "OK" ]
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
*.egg-info
*.pyc
build
gh-pr-comment
gh-pr-upload
!cmd/gh-pr-comment
!cmd/gh-pr-upload
gh-pr-comment.upx
gh-pr-upload.upx
dist
45 changes: 45 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
builds:
- id: gh-pr-comment
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
main: ./cmd/gh-pr-comment/main.go
binary: gh-pr-comment
ldflags:
- "-s -w"
hooks:
post:
- upx "{{ .Path }}"
- id: gh-pr-upload
env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
main: ./cmd/gh-pr-upload/main.go
binary: gh-pr-upload
ldflags:
- "-s -w"
hooks:
post:
- upx "{{ .Path }}"
archives:
- builds:
- gh-pr-comment
- gh-pr-upload
format: tar.gz
format_overrides:
- goos: windows
format: zip
checksum:
name_template: 'checksums.txt'
50 changes: 34 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
dist: xenial
language: python
python:
- 2.7
- 3.6
dist: focal
language: go
go: 1.14
branches:
only:
- master
- "/^[0-9]+\\.[0-9]+\\.[0-9]+/"
- "/^deploy-test/"

addons:
apt:
packages:
- upx-ucl

env:
global:
- CGO_ENABLED=0

install:
- pip install pycodestyle pyflakes
- python setup.py install
- ./tests/coding-style.sh
- go build -ldflags="-s -w" ./cmd/gh-pr-comment
- go build -ldflags="-s -w" ./cmd/gh-pr-upload
- upx ./gh-pr-comment
- upx ./gh-pr-upload
- sudo cp ./gh-pr-comment /usr/local/bin/
- sudo cp ./gh-pr-upload /usr/local/bin/

script:
- ./tests/exec-test.sh
- go vet ./...
- go test ./...
- .ci/upload-test.sh
- curl -sfL https://git.io/goreleaser | sh -s -- check # check goreleaser config

before_deploy:
- export GITHUB_TOKEN=${GITHUB_TOKEN_DEPLOY}

deploy:
provider: pypi
user: at-wat
password:
secure: Abg3hVOGYXSXDEqyQarJjgLbtoIrNLsaald56fA9h0c88h/cmvUKwVHkEGkyZnfJZlT4CHiNHH3vsqC8cHTmkQLlp6kehrGzpUEagaoWCUkE7TR+z8c2HUwYNFU2mDs7TxlvRjOikluFsywjE6GBh0l4ueXMEiM4KBu/KwEpW5O9axvFjzF49o2znT/zPjhEmNHeLEF+pronm/QuR/91q/TIErHFV5ilV4a/zhp66VBUAyQ31yxC1bEV7iSsLHsiwJJIlJTovlgEHVq+j3pD7MoElmdkAAw74BMUXjO3+Ua44qbxfmfQayT6dpeyEPqipHF3Ej262eGuLyaie57oW/tZ6S3MDBEewIp5JC3i79+bZcJ06Btyf+iT75Gac4CSAiT2GCQ7G9O3dQwUFWpNeDDB1+GEZ/R+FWH4Uvwo+BPJi4DNf+UG5IObkGN6YPDxZabzbQxlbHO7vrwNCIiFKebep1bjv7VnMlXu2fqi7TZq9fgtw1N0ignuRcd0ZLr73YcWIF1EP1MfXvxirI1SL5P8GplbnRExCecVCpGGbFmViwgi4r4nlcKzr5bXMnzXDQrSBgTHhIhJJ5qvnvGLZxWfVtNbe5pH0js1ht08vSsKVa0XzlpjdKiWIFdc3vy626xVxKy3wMSS0XOqyuPuJCkqsiAb3wPqwvoOOFH+tso=
on:
tags: true
python: 3.6
- provider: script
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ $ gh-pr-comment title comment

## Install

### Download compiled binary
```
$ pip install gh-pr-comment
$ curl -sL https://raw.githubusercontent.com/at-wat/gh-pr-comment/master/install.sh | sh -s
```

## Required environment variables

### Travis-CI

- ***TRAVIS***: true
- ***TRAVIS\_PULL\_REQUEST\_SLUG***: owner/repos
- ***TRAVIS\_PULL\_REQUEST***: pull request number
- ***TRAVIS\_BOT\_GITHUB\_TOKEN*** or ***GITHUB\_TOKEN***: token with comment write permission

Two of first variables are automatically set by Travis-CI on pull-request builds.
95 changes: 95 additions & 0 deletions cmd/gh-pr-comment/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"context"
"fmt"
"os"
"time"

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

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

func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "usage: %s title comment\n", os.Args[0])
fmt.Fprint(os.Stderr, `env:
GITHUB_TOKEN (or TRAVIS_BOT_GITHUB_TOKEN):
token with comment write permission
env for Travis-CI:
TRAVIS:
must be true
TRAVIS_PULL_REQUEST_SLUG:
owner/repos (TRAVIS_REPO_SLUG is used if not set)
TRAVIS_PULL_REQUEST:
pull request number
`)
os.Exit(1)
}
title := os.Args[1]
body := os.Args[2]

env, err := cienv.Detect()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

if !env.IsPullRequest {
fmt.Fprint(os.Stderr, "info: not on a pull request\n")
os.Exit(0)
}

var ghToken string

if t, ok := os.LookupEnv("TRAVIS_BOT_GITHUB_TOKEN"); ok {
ghToken = t
} else if t, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
ghToken = t
}

if ghToken == "" {
fmt.Fprint(os.Stderr, "error: neither TRAVIS_BOT_GITHUB_TOKEN nor GITHUB_TOKEN is not set.\n")
os.Exit(1)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

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

var gh *github.Client
if ep, customEP := os.LookupEnv("GITHUB_API_URL_BASE"); customEP {
var err error
gh, err = github.NewEnterpriseClient(ep, ep, tc)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to init GitHub client: %v\n", err)
os.Exit(1)
}
} else {
gh = github.NewClient(tc)
}

bodyStr := fmt.Sprintf("## %s\n\n%s", title, body)
_, resp, err := gh.Issues.CreateComment(
ctx,
env.PullRequestSlug.Owner, env.PullRequestSlug.Repo,
env.PullRequest,
&github.IssueComment{Body: &bodyStr},
)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to call CreateComment API: %v\n", err)
os.Exit(1)
}
if resp.StatusCode < 200 || 300 <= resp.StatusCode {
fmt.Fprintf(os.Stderr, "error: failed to CreateComment: %s\n", resp.Status)
os.Exit(1)
}
}
54 changes: 54 additions & 0 deletions cmd/gh-pr-upload/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"os"

"github.com/at-wat/gh-pr-comment/cmd/gh-pr-upload/uploader"
)

func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "usage: %s filename\n", os.Args[0])
fmt.Fprint(os.Stderr, `env:
IMAGE_UPLOADER: imgur(default), s3
ALLOW_PUBLIC_UPLOADER: set it to enable public uploader
env for imgur:
IMGUR_CLIENT_ID: custom-client-id
env for s3:
AWS_DEFAULT_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_S3_BUCKET
return: image url
`)
os.Exit(1)
}
filename := os.Args[1]

var ut = uploader.DefaultUploader
if t, ok := os.LookupEnv("IMAGE_UPLOADER"); ok {
var err error
ut, err = uploader.NewUploaderType(t)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to parse IMAGE_UPLOADER: %v\n", err)
os.Exit(1)
}
}

if ut.Public() {
if _, ok := os.LookupEnv("ALLOW_PUBLIC_UPLOADER"); !ok {
fmt.Fprintf(os.Stderr, "error: public uploader is not allowed, set ALLOW_PUBLIC_UPLOADER to enable\n")
os.Exit(1)
}
}

u := ut.Uploader()
if err := u.Upload(filename); err != nil {
fmt.Fprintf(os.Stderr, "error: failed to upload: %v\n", err)
os.Exit(1)
}
}
Loading

0 comments on commit 6f14a9d

Please sign in to comment.