From a2ea700f58cdc3987519dc34e182c96338e25c46 Mon Sep 17 00:00:00 2001 From: James Pace Date: Mon, 20 Mar 2023 23:31:47 +0000 Subject: [PATCH] Add program to file an issue. --- cmd/j7s-gitea-issue/main.go | 76 +++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 cmd/j7s-gitea-issue/main.go diff --git a/cmd/j7s-gitea-issue/main.go b/cmd/j7s-gitea-issue/main.go new file mode 100644 index 0000000..77cb5ca --- /dev/null +++ b/cmd/j7s-gitea-issue/main.go @@ -0,0 +1,76 @@ +// 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.") + } + + statusFlag := flag.String("status", "", "Status for the commit.") + title := flag.String("title", "", "Title for issue.") + body := flag.String("body", "", "Body for issue.") + flag.Parse() + + if *title == "" { + log.Fatal("Must provide a title.") + } + + isIssue, ok := shouldIssueIssue(*statusFlag) + if !ok { + log.Fatal("Status must be a valid string.") + } + + if !isIssue { + log.Println("Status is fine, don't need to issue an issue.") + os.Exit(0) + } + + clientOpt := gitea.SetToken(token) + client, err := gitea.NewClient(url, clientOpt) + if err != nil { + log.Fatal(err) + } + + issueOption := gitea.CreateIssueOption{Title: *title, Body: *body} + issue, _, err := client.CreateIssue(owner, repo, issueOption) + if err != nil { + log.Fatal(err) + } + log.Println("Filed issue at", issue.HTMLURL) +} + +func shouldIssueIssue(inStatus string) (bool, bool) { + switch inStatus { + // First two cases are valid from Tekton as a pipeline run status. + case "Succeeded", "Completed": + return false, true + case "Failed", "None": + return true, true + default: + return false, false + } +}