commit 3bc2c0f9be9eda40521b7ecbff978404d486fcdd Author: James Pace Date: Mon Mar 20 00:00:36 2023 +0000 Initial commit. diff --git a/cmd/j7s-gitea-status/main.go b/cmd/j7s-gitea-status/main.go new file mode 100644 index 0000000..9c09f2a --- /dev/null +++ b/cmd/j7s-gitea-status/main.go @@ -0,0 +1,82 @@ +// Copyright 2023 James Pace +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package main + +import ( + "code.gitea.io/sdk/gitea" + "flag" + "log" + "os" +) + +func main() { + url := os.Getenv("GIT_URL") + token := os.Getenv("GIT_TOKEN") + owner := os.Getenv("GIT_OWNER") + repo := os.Getenv("GIT_REPO") + + if url == "" || token == "" || owner == "" || repo == "" { + log.Fatal("Must set env variables GIT_URL, GIT_TOKEN, GIT_OWNER, and GIT_REPO.") + } + + sha := flag.String("sha", "", "Commit to mark with status.") + context := flag.String("name", "", "gitea context for this status, e.g. test/mytest") + statusFlag := flag.String("status", "", "status for the commit.") + description := flag.String("description", "", "Text description for the gitea user.") + targetUrl := flag.String("url", "", "URL to show the gitea user.") + flag.Parse() + + if *sha == "" { + log.Fatal("Must provide a commit.") + } + if *context == "" { + log.Fatal("Must provide a name.") + } + status, ok := convertToStatus(*statusFlag) + if !ok { + log.Fatal("Status must be a valid string.") + } + + clientOpt := gitea.SetToken(token) + client, err := gitea.NewClient(url, clientOpt) + if err != nil { + log.Fatal(err) + } + + statusOption := gitea.CreateStatusOption{State: status, TargetURL: *targetUrl, Description: *description, Context: *context} + _, _, err = client.CreateStatus(owner, repo, *sha, statusOption) + if err != nil { + log.Fatal(err) + } + log.Println("Set status for of", status, "for commit", *sha, ".") +} + +func convertToStatus(inStatus string) (gitea.StatusState, bool) { + switch inStatus { + // First two cases are valid from Tekton as a pipeline run status. + case "Succeeded", "Completed": + return gitea.StatusSuccess, true + case "Failed", "None": + return gitea.StatusFailure, true + // Not valid from Tekton as a pipeline run, but still useful. + case "Pending": + return gitea.StatusPending, true + case "Warning": + return gitea.StatusWarning, true + case "Error": + return gitea.StatusError, true + default: + return gitea.StatusError, false + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c0b629e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.jpace121.net/public/j7s-gitea-api-wrapper + +go 1.15 + +require code.gitea.io/sdk/gitea v0.15.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0918234 --- /dev/null +++ b/go.sum @@ -0,0 +1,29 @@ +code.gitea.io/gitea-vet v0.2.1/go.mod h1:zcNbT/aJEmivCAhfmkHOlT645KNOf9W2KnkLgFjGGfE= +code.gitea.io/sdk v0.11.0 h1:R3VdjBCxObyLKnv4Svd/TM6oGsXzN8JORbzgkEFb83w= +code.gitea.io/sdk/gitea v0.15.1 h1:WJreC7YYuxbn0UDaPuWIe/mtiNKTvLN8MLkaw71yx/M= +code.gitea.io/sdk/gitea v0.15.1/go.mod h1:klY2LVI3s3NChzIk/MzMn7G1FHrfU7qd63iSMVoHRBA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= +github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=