Created
April 12, 2022 13:33
-
-
Save frbayart/c6c96ae6449f34bccb1f5675cff51004 to your computer and use it in GitHub Desktop.
gh-to-jira-etl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module kensu.io/m/v2 | |
go 1.16 | |
require github.com/aws/aws-lambda-go v1.24.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"context" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
type MyEvent struct { | |
Ref string `json:"ref"` | |
RefType string `json:"ref_type"` | |
MasterBranch string `json:"master_branch"` | |
Repository struct { | |
Name string `json:"name"` | |
FullName string `json:"full_name"` | |
} `json:"repository"` | |
Organization struct { | |
Login string `json:"login"` | |
} `json:"organization"` | |
Sender struct { | |
Login string `json:"login"` | |
} `json:"sender"` | |
} | |
var httpClient = &http.Client{Timeout: 5 * time.Second} | |
type JiraCreateVersion struct { | |
Archived bool `json:"archived"` | |
Released bool `json:"released"` | |
StartDate string `json:"startDate"` | |
Name string `json:"name"` | |
Description string `json:"description"` | |
ProjectId int `json:"projectId"` | |
} | |
type SlackMessage struct { | |
Text string `json:"text"` | |
} | |
func sendMessageToSlack(message string) { | |
slackMessage := &SlackMessage{ | |
Text: message, | |
} | |
buf := new(bytes.Buffer) | |
json.NewEncoder(buf).Encode(slackMessage) | |
req, err := http.NewRequest("POST", "https://hooks.slack.com/services///", buf) | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Accept", "application/json") | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("slack request code: %d", resp.StatusCode) | |
defer resp.Body.Close() | |
} | |
func createJiraVersion(version string, orgRepoName string, sender string, projectId int) (events.APIGatewayProxyResponse, error) { | |
var ( | |
startDate = fmt.Sprintf("%s", time.Now().UTC().Format("2006-01-02T15:04:05.000Z")) | |
description = fmt.Sprintf("[bot] action from github by %s", sender) | |
) | |
jiraCreateVersion := &JiraCreateVersion{ | |
Archived: false, | |
Released: false, | |
StartDate: startDate, | |
Name: version, | |
Description: description, | |
ProjectId: projectId, | |
} | |
buf := new(bytes.Buffer) | |
json.NewEncoder(buf).Encode(jiraCreateVersion) | |
req, err := http.NewRequest("POST", "https://kensu.atlassian.net/rest/api/2/version", buf) | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Accept", "application/json") | |
req.Header.Set("Accept-Encoding", "identity") | |
req.SetBasicAuth("", "") | |
if err != nil { | |
log.Fatal(err) | |
} | |
resp, err := httpClient.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode == 201 { | |
messageResponse := fmt.Sprintf("New tag created on %s by %s, **%s** release created on Jira %d", orgRepoName, sender, version, projectId) | |
log.Printf("%s", messageResponse) | |
sendMessageToSlack(messageResponse) | |
return events.APIGatewayProxyResponse{Body: messageResponse, StatusCode: 200}, nil | |
} else { | |
log.Printf("Atlassian error: %d", resp.StatusCode) | |
body, _ := ioutil.ReadAll(resp.Body) | |
return events.APIGatewayProxyResponse{Body: string(body), StatusCode: resp.StatusCode}, nil | |
} | |
} | |
func HandleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
var myevent MyEvent | |
json.Unmarshal([]byte(request.Body), &myevent) | |
log.Printf("Repo name: %s", myevent.Repository.Name) | |
log.Printf("Ref: %s", myevent.Ref) | |
log.Printf("REGION: %s", os.Getenv("AWS_REGION")) | |
if myevent.RefType == "tag" { | |
jiraVersion := fmt.Sprintf("%s:%s", myevent.Repository.Name, myevent.Ref) | |
// QA 10014 | |
// DAM 10000 | |
// OPS 10013 | |
// RELEASE 10028 | |
createJiraVersion(jiraVersion, myevent.Repository.FullName, myevent.Sender.Login, 10000) | |
createJiraVersion(jiraVersion, myevent.Repository.FullName, myevent.Sender.Login, 10014) | |
createJiraVersion(jiraVersion, myevent.Repository.FullName, myevent.Sender.Login, 10028) | |
return createJiraVersion(jiraVersion, myevent.Repository.FullName, myevent.Sender.Login, 10013) | |
} | |
//return fmt.Sprintf("Hello %s!", name.Ref ), nil | |
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil | |
} | |
func main() { | |
lambda.Start(HandleRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment