Implemented initial version based on google/go-github

This commit is contained in:
Thomas Boerger 2015-11-23 01:07:57 +01:00
parent 2929ff833e
commit 70ca6255f4
15 changed files with 392 additions and 2 deletions

0
.drone.sec Normal file
View File

31
.drone.yml Normal file
View File

@ -0,0 +1,31 @@
build:
image: golang:1.5
environment:
- GO15VENDOREXPERIMENT=1
- GOOS=linux
- GOARCH=amd64
- CGO_ENABLED=0
commands:
- go get
- go build
- go test
publish:
docker:
username: drone
password: $$DOCKER_PASS
email: $$DOCKER_EMAIL
repo: plugins/drone-github-release
when:
branch: master
plugin:
name: GitHub Release
desc: Publish files and artifacts to GitHub releases
type: publish
image: plugins/drone-github-release
labels:
- publish
- github
- release
- dpl

2
.gitignore vendored
View File

@ -22,3 +22,5 @@ _testmain.go
*.exe *.exe
*.test *.test
*.prof *.prof
drone-github-release

17
DOCS.md Normal file
View File

@ -0,0 +1,17 @@
Use this plugin for publishing files and artifacts to GitHub releases. You
can override the default configuration with the following parameters:
* `api_key` - GitHub oauth token with public_repo or repo permission
* `files` - Files to upload to GitHub Release; globs are allowed
* `base_url` - GitHub base URL; only required for GHE
* `upload_url` - GitHub upload URL; only required for GHE
Sample configuration:
```yaml
publish:
github_release:
api_key: my_github_api_key
files:
- dist/*
```

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
# Docker image for the Drone GitHub Release plugin
#
# cd $GOPATH/src/github.com/drone-plugins/drone-github-release
# GO15VENDOREXPERIMENT=1 CGO_ENABLED=0 go build -a -tags netgo
# docker build --rm=true -t plugins/drone-github-release .
FROM alpine:3.2
RUN apk add -U ca-certificates git && rm -rf /var/cache/apk/*
ADD drone-github-release /bin/
ENTRYPOINT ["/bin/drone-github-release"]

View File

@ -199,4 +199,3 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.

View File

@ -1,2 +1,47 @@
# drone-github-release # drone-github-release
https://github.com/travis-ci/dpl/blob/master/README.md?#github-releases
Drone plugin for publishing GitHub releases
## Usage
Publish a release:
```
./drone-github-release <<EOF
{
"repo": {
"clone_url": "git://github.com/drone/drone",
"full_name": "drone/drone",
"owner": "drone",
"name": "drone"
},
"build": {
"event": "tag",
"branch": "refs/heads/v0.0.1",
"commit": "8f5d3b2ce38562bedb48b798328f5bb2e4077a2f",
"ref": "refs/heads/v0.0.1"
},
"workspace": {
"root": "/drone/src",
"path": "drone/src/github.com/drone/drone"
},
"vargs": {
"api_key": "your_api_key",
"files": [
"dist/*.txt",
"dist/other-file"
]
}
}
EOF
```
## Docker
Build the Docker container using the `netgo` build tag to eliminate the CGO
dependency:
```
GO15VENDOREXPERIMENT=1 CGO_ENABLED=0 go build -a -tags netgo
docker build --rm=true -t plugins/drone-github-release .
```

13
glide.yaml Normal file
View File

@ -0,0 +1,13 @@
package: github.com/drone-plugins/drone-github-release
import:
- package: github.com/drone/drone-go
ref: 085fdbd71700fb1a27fdc60b09689ab92cee8ceb
- package: github.com/google/go-github
ref: 47f2593dad1971eec2f0a0971aa007fef5edbc50
- package: github.com/google/go-querystring
ref: 2a60fc2ba6c19de80291203597d752e9ba58e4c0
- package: golang.org/x/net
ref: 4f2fc6c1e69d41baf187332ee08fbd2b296f21ed
- package: golang.org/x/oauth2
ref: 442624c9ec9243441e83b374a9e22ac549b5c51d

6
logo.svg Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="256px" height="250px" viewBox="0 0 256 250" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
<g>
<path d="M128.00106,0 C57.3172926,0 0,57.3066942 0,128.00106 C0,184.555281 36.6761997,232.535542 87.534937,249.460899 C93.9320223,250.645779 96.280588,246.684165 96.280588,243.303333 C96.280588,240.251045 96.1618878,230.167899 96.106777,219.472176 C60.4967585,227.215235 52.9826207,204.369712 52.9826207,204.369712 C47.1599584,189.574598 38.770408,185.640538 38.770408,185.640538 C27.1568785,177.696113 39.6458206,177.859325 39.6458206,177.859325 C52.4993419,178.762293 59.267365,191.04987 59.267365,191.04987 C70.6837675,210.618423 89.2115753,204.961093 96.5158685,201.690482 C97.6647155,193.417512 100.981959,187.77078 104.642583,184.574357 C76.211799,181.33766 46.324819,170.362144 46.324819,121.315702 C46.324819,107.340889 51.3250588,95.9223682 59.5132437,86.9583937 C58.1842268,83.7344152 53.8029229,70.715562 60.7532354,53.0843636 C60.7532354,53.0843636 71.5019501,49.6441813 95.9626412,66.2049595 C106.172967,63.368876 117.123047,61.9465949 128.00106,61.8978432 C138.879073,61.9465949 149.837632,63.368876 160.067033,66.2049595 C184.49805,49.6441813 195.231926,53.0843636 195.231926,53.0843636 C202.199197,70.715562 197.815773,83.7344152 196.486756,86.9583937 C204.694018,95.9223682 209.660343,107.340889 209.660343,121.315702 C209.660343,170.478725 179.716133,181.303747 151.213281,184.472614 C155.80443,188.444828 159.895342,196.234518 159.895342,208.176593 C159.895342,225.303317 159.746968,239.087361 159.746968,243.303333 C159.746968,246.709601 162.05102,250.70089 168.53925,249.443941 C219.370432,232.499507 256,184.536204 256,128.00106 C256,57.3066942 198.691187,0 128.00106,0 Z M47.9405593,182.340212 C47.6586465,182.976105 46.6581745,183.166873 45.7467277,182.730227 C44.8183235,182.312656 44.2968914,181.445722 44.5978808,180.80771 C44.8734344,180.152739 45.876026,179.97045 46.8023103,180.409216 C47.7328342,180.826786 48.2627451,181.702199 47.9405593,182.340212 Z M54.2367892,187.958254 C53.6263318,188.524199 52.4329723,188.261363 51.6232682,187.366874 C50.7860088,186.474504 50.6291553,185.281144 51.2480912,184.70672 C51.8776254,184.140775 53.0349512,184.405731 53.8743302,185.298101 C54.7115892,186.201069 54.8748019,187.38595 54.2367892,187.958254 Z M58.5562413,195.146347 C57.7719732,195.691096 56.4895886,195.180261 55.6968417,194.042013 C54.9125733,192.903764 54.9125733,191.538713 55.713799,190.991845 C56.5086651,190.444977 57.7719732,190.936735 58.5753181,192.066505 C59.3574669,193.22383 59.3574669,194.58888 58.5562413,195.146347 Z M65.8613592,203.471174 C65.1597571,204.244846 63.6654083,204.03712 62.5716717,202.981538 C61.4524999,201.94927 61.1409122,200.484596 61.8446341,199.710926 C62.5547146,198.935137 64.0575422,199.15346 65.1597571,200.200564 C66.2704506,201.230712 66.6095936,202.705984 65.8613592,203.471174 Z M75.3025151,206.281542 C74.9930474,207.284134 73.553809,207.739857 72.1039724,207.313809 C70.6562556,206.875043 69.7087748,205.700761 70.0012857,204.687571 C70.302275,203.678621 71.7478721,203.20382 73.2083069,203.659543 C74.6539041,204.09619 75.6035048,205.261994 75.3025151,206.281542 Z M86.046947,207.473627 C86.0829806,208.529209 84.8535871,209.404622 83.3316829,209.4237 C81.8013,209.457614 80.563428,208.603398 80.5464708,207.564772 C80.5464708,206.498591 81.7483088,205.631657 83.2786917,205.606221 C84.8005962,205.576546 86.046947,206.424403 86.046947,207.473627 Z M96.6021471,207.069023 C96.7844366,208.099171 95.7267341,209.156872 94.215428,209.438785 C92.7295577,209.710099 91.3539086,209.074206 91.1652603,208.052538 C90.9808515,206.996955 92.0576306,205.939253 93.5413813,205.66582 C95.054807,205.402984 96.4092596,206.021919 96.6021471,207.069023 Z" fill="#161614"></path>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.8 KiB

262
main.go Normal file
View File

@ -0,0 +1,262 @@
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"github.com/drone/drone-go/drone"
"github.com/drone/drone-go/plugin"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Params struct {
BaseUrl string `json:"base_url"`
UploadUrl string `json:"upload_url"`
APIKey string `json:"api_key"`
Files []string `json:"files"`
}
func main() {
r := drone.Repo{}
b := drone.Build{}
w := drone.Workspace{}
v := Params{}
plugin.Param("repo", &r)
plugin.Param("build", &b)
plugin.Param("workspace", &w)
plugin.Param("vargs", &v)
plugin.MustParse()
if b.Event != "tag" {
fmt.Printf("The GitHub Release plugin is only available for tags\n")
os.Exit(0)
return
}
if len(v.BaseUrl) == 0 {
v.BaseUrl = "https://api.github.com/"
} else {
if !strings.HasSuffix(v.BaseUrl, "/") {
v.BaseUrl = v.BaseUrl + "/"
}
}
if len(v.UploadUrl) == 0 {
v.UploadUrl = "https://uploads.github.com/"
} else {
if !strings.HasSuffix(v.UploadUrl, "/") {
v.UploadUrl = v.UploadUrl + "/"
}
}
if len(v.APIKey) == 0 {
fmt.Printf("You must provide an API key\n")
os.Exit(1)
return
}
files := make([]string, 0)
for _, glob := range v.Files {
globed, err := filepath.Glob(glob)
if err != nil {
fmt.Printf("Failed to glob %s\n", glob)
os.Exit(1)
return
}
if globed != nil {
files = append(files, globed...)
}
}
baseUrl, err := url.Parse(v.BaseUrl)
if err != nil {
fmt.Printf("Failed to parse base URL\n")
os.Exit(1)
return
}
uploadUrl, err := url.Parse(v.UploadUrl)
if err != nil {
fmt.Printf("Failed to parse upload URL\n")
os.Exit(1)
return
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: v.APIKey,
})
tc := oauth2.NewClient(
oauth2.NoContext,
ts)
client := github.NewClient(tc)
client.BaseURL = baseUrl
client.UploadURL = uploadUrl
release, releaseErr := retrieveRelease(
client,
r.Owner,
r.Name,
filepath.Base(b.Ref))
if releaseErr != nil {
fmt.Println(releaseErr)
os.Exit(1)
return
}
uploadErr := appendFiles(
client,
r.Owner,
r.Name,
*release.ID,
files)
if uploadErr != nil {
fmt.Println(uploadErr)
os.Exit(1)
return
}
}
func prepareRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
var release *github.RepositoryRelease
var releaseErr error
release, releaseErr = retrieveRelease(
client,
owner,
repo,
tag)
if releaseErr != nil {
return nil, releaseErr
}
if release != nil {
return release, nil
}
release, releaseErr = createRelease(
client,
owner,
repo,
tag)
if releaseErr != nil {
return nil, releaseErr
}
if release != nil {
return release, nil
}
return nil, errors.New(
"Failed to retrieve or create a release")
}
func retrieveRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
release, _, err := client.Repositories.GetReleaseByTag(
owner,
repo,
tag)
if err != nil {
return nil, errors.New(
"Failed to retrieve release")
}
fmt.Printf("Successfully retrieved %s release\n", tag)
return release, nil
}
func createRelease(client *github.Client, owner string, repo string, tag string) (*github.RepositoryRelease, error) {
release, _, err := client.Repositories.CreateRelease(
owner,
repo,
&github.RepositoryRelease{TagName: github.String(tag)})
if err != nil {
return nil, errors.New(
"Failed to create release")
}
fmt.Printf("Successfully created %s release\n", tag)
return release, nil
}
func appendFiles(client *github.Client, owner string, repo string, id int, files []string) error {
assets, _, err := client.Repositories.ListReleaseAssets(
owner,
repo,
id,
&github.ListOptions{})
if err != nil {
return errors.New(
"Failed to fetch existing assets")
}
for _, file := range files {
handle, err := os.Open(file)
if err != nil {
return errors.New(
fmt.Sprintf("Failed to read %s artifact", file))
}
for _, asset := range assets {
if *asset.Name == path.Base(file) {
_, deleteErr := client.Repositories.DeleteReleaseAsset(
owner,
repo,
*asset.ID)
if deleteErr != nil {
return errors.New(
fmt.Sprintf("Failed to delete %s artifact", file))
} else {
fmt.Printf("Successfully deleted old %s artifact\n", *asset.Name)
}
}
}
_, _, uploadErr := client.Repositories.UploadReleaseAsset(
owner,
repo,
id,
&github.UploadOptions{Name: path.Base(file)},
handle)
if uploadErr != nil {
return errors.New(
fmt.Sprintf("Failed to upload %s artifact", file))
} else {
fmt.Printf("Successfully uploaded %s artifact\n", file)
}
}
return nil
}

1
vendor/github.com/drone/drone-go generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 085fdbd71700fb1a27fdc60b09689ab92cee8ceb

1
vendor/github.com/google/go-github generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 47f2593dad1971eec2f0a0971aa007fef5edbc50

1
vendor/github.com/google/go-querystring generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 2a60fc2ba6c19de80291203597d752e9ba58e4c0

1
vendor/golang.org/x/net generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 4f2fc6c1e69d41baf187332ee08fbd2b296f21ed

1
vendor/golang.org/x/oauth2 generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 442624c9ec9243441e83b374a9e22ac549b5c51d