Skip to content

Commit

Permalink
Rework configuration approach. Add project name label & arbitrary lab…
Browse files Browse the repository at this point in the history
…els to tickets
  • Loading branch information
Nicolas committed Jul 22, 2022
1 parent a565019 commit dbb13c4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ Automatically create Jira tickets when a newreleases.io release is detected usin
## Configuration:

The application requires the following environment variables to be set:
- `PORT`: The port the application is expecting traffic on
- `JIRA_URL`: The URL of your Jira instance
- `JIRA_USER`: Jira username to authenticate API requests
- `JIRA_TOKEN`: Jira API token, can also be a password in self-hosted instances
- `JIRA_PROJECT`: Jira Project key the tickets will be created in
- `JELEASE_PORT`: The port the application is expecting traffic on
- `JELEASE_ADDLABELS`: Add additional labels to the created jira ticket
- `JELEASE_JIRAURL`: The URL of your Jira instance
- `JELEASE_JIRAUSER`: Jira username to authenticate API requests
- `JELEASE_JIRATOKEN`: Jira API token, can also be a password in self-hosted instances
- `JELEASE_JIRAPROJECT`: Jira Project key the tickets will be created in

## Usage

Send newreleases.io webhooks to the `host:port/webhook` route.
Direct newreleases.io webhooks to the `host:port/webhook` route.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module github.2rioffice.com/nicolas-mohr/jilease
module github.2rioffice.com/nicolas-mohr/jelease

go 1.18

require github.com/andygrunwald/go-jira v1.15.1
require (
github.com/andygrunwald/go-jira v1.15.1
github.com/joho/godotenv v1.4.0
github.com/kelseyhightower/envconfig v1.4.0
)

require (
github.com/fatih/structs v1.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM=
Expand Down
70 changes: 39 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"

jira "github.com/andygrunwald/go-jira"
)

const (
defaultPort = "8080"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
)

var (
jiraClient *jira.Client
jiraProject string
jiraClient *jira.Client
config Config
)

// Configuration read from environment or .env file
type Config struct {
// Consider: add split_words tag for nicer readability of env vars
Port string `default:"8080"`
JiraUrl string `required:"true"`
JiraUser string `required:"true"`
JiraToken string `required:"true"`
JiraProject string `required:"true"`
AddLabels []string
}

// A new release object unmarshaled from the newreleases.io webhook.
// Some fields omitted for simplicity, check out the documentation at https://newreleases.io/webhooks
type NewRelease struct {
Provider string `json:"provider"`
Expand All @@ -29,15 +40,18 @@ type NewRelease struct {
}

func ReleaseToJiraIssue(newRelease NewRelease) jira.Issue {
labels := append(config.AddLabels, newRelease.Project)
return jira.Issue{
Fields: &jira.IssueFields{
Description: "Update issue generated by newreleases.io",
Project: jira.Project{
Key: jiraProject,
Key: config.JiraProject,
},
Type: jira.IssueType{
Name: "Task",
},
// Status: ,
Labels: labels,
Summary: fmt.Sprintf("Update %v to version %v", newRelease.Project, newRelease.Version),
},
}
Expand All @@ -62,6 +76,8 @@ func postWebhook(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(newRelease)

// look for existing releases

// create jira ticket
i := ReleaseToJiraIssue(newRelease)
issue, response, err := jiraClient.Issue.Create(&i)
Expand All @@ -76,41 +92,33 @@ func postWebhook(w http.ResponseWriter, r *http.Request) {
}

func main() {
// jira setup
jiraUrl, jiraUrlPresent := os.LookupEnv("JIRA_URL")
jiraUser, jiraUserPresent := os.LookupEnv("JIRA_USER")
jiraToken, jiraTokenPresent := os.LookupEnv("JIRA_TOKEN")
var jiraProjectPresent bool
jiraProject, jiraProjectPresent = os.LookupEnv("JIRA_PROJECT")
if !(jiraUserPresent && jiraTokenPresent && jiraUrlPresent && jiraProjectPresent) {
fmt.Printf("Missing jira environment variables.\nJIRA_URL present: %v\nJIRA_USER present: %v\nJIRA_TOKEN present: %v\nJIRA_PROJECT present: %v\n",
jiraUrlPresent, jiraTokenPresent, jiraUserPresent, jiraProjectPresent)
return
// configuration setup
err := godotenv.Load()
if err != nil {
fmt.Println("No .env file found.")
}

fmt.Printf("Jira URL: %v\n", jiraUrl)
err = envconfig.Process("jelease", &config)
if err != nil {
log.Fatal(err.Error())
}

fmt.Printf("Jira URL: %v\n", config.JiraUrl)
tp := jira.BasicAuthTransport{
Username: jiraUser,
Password: jiraToken,
Username: config.JiraUser,
Password: config.JiraToken,
}

var err error
jiraClient, err = jira.NewClient(tp.Client(), jiraUrl)
jiraClient, err = jira.NewClient(tp.Client(), config.JiraUrl)
if err != nil {
panic(err)
}

// http server setup
port, portPresent := os.LookupEnv("PORT")
if !portPresent {
port = defaultPort
fmt.Printf("No port specified, defaulting to %v\n", defaultPort)
}

// http serversetup
http.HandleFunc("/webhook", postWebhook)
http.HandleFunc("/", getRoot)
fmt.Printf("Listening on port %v\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%v", port), nil)
fmt.Printf("Listening on port %v\n", config.Port)
err = http.ListenAndServe(fmt.Sprintf(":%v", config.Port), nil)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
Expand Down

0 comments on commit dbb13c4

Please sign in to comment.