Skip to content

Commit

Permalink
refactor subsections of run into separate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas committed Aug 2, 2022
1 parent 75f7b88 commit 0ded5cd
Showing 1 changed file with 76 additions and 75 deletions.
151 changes: 76 additions & 75 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,82 @@ func handlePostWebhook(w http.ResponseWriter, r *http.Request) {
logger.Printf("Updated issue summary from %q to %q", previousSummary, release.IssueSummary())
}

func configSetup() error {
err := godotenv.Load()
if err != nil {
logger.Println("No .env file found.")
}

err = envconfig.Process("jelease", &config)
if err != nil {
return err
}

logger.Printf("Jira URL: %v\n", config.JiraUrl)
tp := jira.BasicAuthTransport{
Username: config.JiraUser,
Password: config.JiraToken,
}
jiraClient, err = jira.NewClient(tp.Client(), config.JiraUrl)
if err != nil {
return fmt.Errorf("failed to create jira client: %w", err)
}
return nil
}

func projectExists() error {
allProjects, response, err := jiraClient.Project.GetList()
if err != nil {
body, readErr := io.ReadAll(response.Body)
errCtx := errors.New("error response from Jira when retrieving project list")
if readErr != nil {
return fmt.Errorf("%v: %w. Failed to decode response body: %v", errCtx, err, readErr)
}
return fmt.Errorf("%v: %w. Response body: %v", errCtx, err, string(body))
}
var projectExists bool
for _, project := range *allProjects {
if project.Key == config.Project {
projectExists = true
break
}
}
if !projectExists {
return fmt.Errorf("project %v does not exist on your Jira server", config.Project)
}
return nil
}

func statusExists() error {
allStatuses, response, err := jiraClient.Status.GetAllStatuses()
if err != nil {
body, readErr := io.ReadAll(response.Body)
errCtx := errors.New("error response from Jira when retrieving status list: %+v")
if readErr != nil {
return fmt.Errorf("%v: %w. Failed to decode response body: %v", errCtx, err, readErr)
}
return fmt.Errorf("%v: %w. Response body: %v", errCtx, err, string(body))
}
var statusExists bool
for _, status := range allStatuses {
if status.Name == config.DefaultStatus {
statusExists = true
break
}
}
if !statusExists {
return fmt.Errorf("status %v does not exist on your Jira server", config.DefaultStatus)
}
return nil
}

func serveHTTP() error {
http.HandleFunc("/webhook", handlePostWebhook)
http.HandleFunc("/", handleGetRoot)
logger.Printf("Listening on port %v\n", config.Port)
return http.ListenAndServe(fmt.Sprintf(":%v", config.Port), nil)
}

func init() {
logger = log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile)
}
Expand All @@ -193,81 +269,6 @@ func main() {
}

func run() error {
configSetup := func() error {
err := godotenv.Load()
if err != nil {
logger.Println("No .env file found.")
}

err = envconfig.Process("jelease", &config)
if err != nil {
return err
}

logger.Printf("Jira URL: %v\n", config.JiraUrl)
tp := jira.BasicAuthTransport{
Username: config.JiraUser,
Password: config.JiraToken,
}
jiraClient, err = jira.NewClient(tp.Client(), config.JiraUrl)
if err != nil {
return fmt.Errorf("failed to create jira client: %w", err)
}
return nil
}

projectExists := func() error {
allProjects, response, err := jiraClient.Project.GetList()
if err != nil {
body, readErr := io.ReadAll(response.Body)
errCtx := errors.New("error response from Jira when retrieving project list")
if readErr != nil {
return fmt.Errorf("%v: %w. Failed to decode response body: %v", errCtx, err, readErr)
}
return fmt.Errorf("%v: %w. Response body: %v", errCtx, err, string(body))
}
var projectExists bool
for _, project := range *allProjects {
if project.Key == config.Project {
projectExists = true
break
}
}
if !projectExists {
return fmt.Errorf("project %v does not exist on your Jira server", config.Project)
}
return nil
}

statusExists := func() error {
allStatuses, response, err := jiraClient.Status.GetAllStatuses()
if err != nil {
body, readErr := io.ReadAll(response.Body)
errCtx := errors.New("error response from Jira when retrieving status list: %+v")
if readErr != nil {
return fmt.Errorf("%v: %w. Failed to decode response body: %v", errCtx, err, readErr)
}
return fmt.Errorf("%v: %w. Response body: %v", errCtx, err, string(body))
}
var statusExists bool
for _, status := range allStatuses {
if status.Name == config.DefaultStatus {
statusExists = true
break
}
}
if !statusExists {
return fmt.Errorf("status %v does not exist on your Jira server", config.DefaultStatus)
}
return nil
}

serveHTTP := func() error {
http.HandleFunc("/webhook", handlePostWebhook)
http.HandleFunc("/", handleGetRoot)
logger.Printf("Listening on port %v\n", config.Port)
return http.ListenAndServe(fmt.Sprintf(":%v", config.Port), nil)
}

err := configSetup()
if err != nil {
Expand Down

0 comments on commit 0ded5cd

Please sign in to comment.