Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed May 17, 2024
1 parent 852ba06 commit 7914c69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
9 changes: 7 additions & 2 deletions pkg/scripts/issues/assign-labels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Issue struct {
}

func readGitHubIssuesBucket() []Issue {
f, err := os.Open("GitHubIssuesBucket.csv")
f, err := os.Open("/Users/jcieslak/Documents/terraform-provider-snowflake/pkg/scripts/issues/assign-labels/GitHubIssuesBucket.csv")
if err != nil {
panic(err)
}
Expand All @@ -88,7 +88,7 @@ func readGitHubIssuesBucket() []Issue {
}

func assignLabelsToIssues(accessToken string, issues []Issue) (successful []AssignResult, failed []AssignResult) {
for _, issue := range issues {
for i, issue := range issues {
addLabelsRequestBody := createAddLabelsRequestBody(issue)
if addLabelsRequestBody == nil {
log.Println("couldn't create add label request body from issue", issue)
Expand Down Expand Up @@ -140,6 +140,10 @@ func assignLabelsToIssues(accessToken string, issues []Issue) (successful []Assi
continue
}

if i > 3 {
break
}

successful = append(successful, AssignResult{
IssueId: issue.ID,
Labels: addLabelsRequestBody.Labels,
Expand All @@ -155,6 +159,7 @@ type AddLabelsRequestBody struct {

func createAddLabelsRequestBody(issue Issue) *AddLabelsRequestBody {
if categoryLabel, ok := lookupTable[issue.Category]; ok {
// TODO: Split those into two
if issue.Category == "RESOURCE" || issue.Category == "DATA_SOURCE" {
if resourceName, ok := lookupTable[issue.Object]; ok {
return &AddLabelsRequestBody{
Expand Down
23 changes: 16 additions & 7 deletions pkg/scripts/issues/create-labels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/scripts/issues"
"io"
"log"
"net/http"
"os"
"slices"
"strings"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/scripts/issues"
)

func main() {
Expand Down Expand Up @@ -73,6 +72,7 @@ func loadRepoLabels(accessToken string) []ReadLabel {
type CreateLabelRequestBody struct {
Name string `json:"name"`
Description string `json:"description"`
Color string `json:"color"`
}

func createLabelsIfNotPresent(accessToken string, repoLabels []ReadLabel, labels []string) (successful []string, failed []string) {
Expand All @@ -86,9 +86,6 @@ func createLabelsIfNotPresent(accessToken string, repoLabels []ReadLabel, labels
continue
}

time.Sleep(3 * time.Second)
log.Println("Processing:", label)

var requestBody []byte
var err error
parts := strings.Split(label, ":")
Expand All @@ -97,10 +94,17 @@ func createLabelsIfNotPresent(accessToken string, repoLabels []ReadLabel, labels

switch labelType {
// Categories will be created by hand
case "resource", "data_source":
case "resource":
requestBody, err = json.Marshal(&CreateLabelRequestBody{
Name: label,
Description: fmt.Sprintf("Issue connected to the snowflake_%s resource", labelValue),
Color: "1D76DB",
})
case "data_source":
requestBody, err = json.Marshal(&CreateLabelRequestBody{
Name: label,
Description: fmt.Sprintf("Issue connected to the snowflake_%s data source", labelValue),
Color: "6321BE",
})
default:
log.Println("Unknown label type:", labelType)
Expand All @@ -113,6 +117,10 @@ func createLabelsIfNotPresent(accessToken string, repoLabels []ReadLabel, labels
continue
}

time.Sleep(1 * time.Second)
log.Println("Processing:", label)

// based on https://docs.github.com/en/rest/issues/labels?apiVersion=2022-11-28#create-a-label
req, err := http.NewRequest(http.MethodPost, "https://api.github.com/repos/Snowflake-Labs/terraform-provider-snowflake/labels", bytes.NewReader(requestBody))
if err != nil {
log.Println("failed to create label request:", err)
Expand All @@ -131,7 +139,8 @@ func createLabelsIfNotPresent(accessToken string, repoLabels []ReadLabel, labels
}

if resp.StatusCode != http.StatusCreated {
log.Println("incorrect status code, expected 201, and got:", resp.StatusCode)
responseBody, _ := io.ReadAll(resp.Body)
log.Println("incorrect status code, expected 201, and got:", resp.StatusCode, string(responseBody))
failed = append(failed, label)
continue
}
Expand Down

0 comments on commit 7914c69

Please sign in to comment.