|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + // "code.uber.internal/devexp/utils/jirawithretry" |
| 12 | + // "github.com/andygrunwald/go-jira" |
| 13 | + jira "github.com/andygrunwald/go-jira" |
| 14 | + "github.com/google/go-github/github" |
| 15 | + "golang.org/x/oauth2" |
| 16 | + |
| 17 | + "go.uber.org/cadence/activity" |
| 18 | + "go.uber.org/cadence/workflow" |
| 19 | + "go.uber.org/zap" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // ApplicationName is the task list for this sample |
| 24 | + ApplicationName = "jiraToGithubGroup" |
| 25 | + jiraURL = "https://t3.uberinternal.com" |
| 26 | + jiraUsername = "[email protected]" |
| 27 | +) |
| 28 | + |
| 29 | +// type Activity struct { |
| 30 | +// jiraClient jirawithretry.IssueClient |
| 31 | +// } |
| 32 | + |
| 33 | +func jiraToGithubWorkflow(ctx workflow.Context) (result string, err error) { |
| 34 | + // step 1, get JIRA tasks from cadence project |
| 35 | + ao := workflow.ActivityOptions{ |
| 36 | + ScheduleToStartTimeout: time.Minute, |
| 37 | + StartToCloseTimeout: time.Minute * 4, |
| 38 | + HeartbeatTimeout: time.Second * 20, |
| 39 | + } |
| 40 | + ctx1 := workflow.WithActivityOptions(ctx, ao) |
| 41 | + logger := workflow.GetLogger(ctx) |
| 42 | + logger.Info("Jira to Github workflow started") |
| 43 | + |
| 44 | + var issues []jira.Issue |
| 45 | + err = workflow.ExecuteActivity(ctx1, getJiraTasksActivity).Get(ctx1, &issues) |
| 46 | + if err != nil { |
| 47 | + return "", err |
| 48 | + } |
| 49 | + |
| 50 | + // step 2, create issues in github |
| 51 | + ao = workflow.ActivityOptions{ |
| 52 | + ScheduleToStartTimeout: time.Minute, |
| 53 | + StartToCloseTimeout: time.Minute * 8, |
| 54 | + } |
| 55 | + ctx2 := workflow.WithActivityOptions(ctx, ao) |
| 56 | + |
| 57 | + err = workflow.ExecuteActivity(ctx2, createGithubIssuesActivity, issues).Get(ctx2, nil) |
| 58 | + if err != nil { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + |
| 62 | + logger.Info("Workflow completed with Github issues created.") |
| 63 | + return "COMPLETED", nil |
| 64 | +} |
| 65 | + |
| 66 | +// func New() (*Activity, error) { |
| 67 | +// client, err := jirawithretry.NewIssueClient(&http.Client{}, jiraURL) |
| 68 | +// if err != nil { |
| 69 | +// return nil, err |
| 70 | +// } |
| 71 | +// envVars, err := getEnvVars() |
| 72 | +// if err != nil { |
| 73 | +// return nil, err |
| 74 | +// } |
| 75 | +// client.Authentication().SetBasicAuth(jiraUsername, envVars["JIRA_API_TOKEN"]) |
| 76 | +// return &Activity{jiraClient: client}, nil |
| 77 | +// } |
| 78 | + |
| 79 | +func getJiraTasksActivity(ctx context.Context) ([]jira.Issue, error) { |
| 80 | + jiraClient, err := jira.NewClient(nil, jiraURL) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + jql := "project = Cadence AND created >= -365d AND issuetype = Task AND labels = opensourceable ORDER BY created DESC" //AND labels = opensourceable AND description IS NOT EMPTY |
| 86 | + options := &jira.SearchOptions{ |
| 87 | + MaxResults: 10, |
| 88 | + } |
| 89 | + issues, _, err := jiraClient.Issue.Search(jql, options) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + activity.GetLogger(ctx).Info("JIRA tasks with label opensourceable fetched", zap.Int("Count", len(issues))) |
| 95 | + return issues, err |
| 96 | +} |
| 97 | + |
| 98 | +func createGithubIssuesActivity(ctx context.Context, issues []jira.Issue) error { |
| 99 | + // Replace with your actual token |
| 100 | + token := "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" |
| 101 | + |
| 102 | + // GitHub org and repo |
| 103 | + // org := "cadence-workflow" |
| 104 | + // repo := "cadence-java-samples" |
| 105 | + client := github.NewClient(nil) |
| 106 | + |
| 107 | + org := "vishwa-test-2" |
| 108 | + repo := "sample" |
| 109 | + |
| 110 | + ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) |
| 111 | + tc := oauth2.NewClient(ctx, ts) |
| 112 | + client2 := github.NewClient(tc) |
| 113 | + for _, issue := range issues { |
| 114 | + title := issue.Fields.Summary |
| 115 | + // key := issue.Key |
| 116 | + body := issue.Fields.Description |
| 117 | + |
| 118 | + // Check if the issue already exists in GitHub |
| 119 | + searchOpts := &github.SearchOptions{ |
| 120 | + TextMatch: true, |
| 121 | + } |
| 122 | + query := fmt.Sprintf("repo:vishwa-test-2/sample in:title %s state:open", title) |
| 123 | + result, _, err := client.Search.Issues(ctx, query, searchOpts) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if len(result.Issues) == 0 { |
| 129 | + // Create issue request |
| 130 | + issueRequest := &github.IssueRequest{ |
| 131 | + Title: github.String(title), |
| 132 | + Body: github.String(body), |
| 133 | + } |
| 134 | + |
| 135 | + // Create issue |
| 136 | + issue, _, err := client2.Issues.Create(ctx, org, repo, issueRequest) |
| 137 | + if err != nil { |
| 138 | + log.Fatalf("Error creating issue: %v", err) |
| 139 | + } |
| 140 | + |
| 141 | + // req := &github.IssueRequest{ |
| 142 | + // Title: github.String(fmt.Sprintf("[JIRA %s] %s", key, title)), |
| 143 | + // Body: github.String(body), |
| 144 | + // } |
| 145 | + // _, _, err := client.Issues.Create(ctx, "vishwa-test-2", "sample", req) |
| 146 | + // if err != nil { |
| 147 | + // return err |
| 148 | + // } |
| 149 | + activity.GetLogger(ctx).Info("Created an issue in GitHub", zap.String("title", issue.GetHTMLURL())) |
| 150 | + } else { |
| 151 | + activity.GetLogger(ctx).Info("GitHub issue already exists", zap.String("title", title)) |
| 152 | + } |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +// type IssueRequest struct { |
| 158 | +// Title string `json:"title"` |
| 159 | +// Body string `json:"body"` |
| 160 | +// } |
| 161 | + |
| 162 | +// func createGithubIssuesActivity(ctx context.Context, issues []jira.Issue) error { |
| 163 | +// for _, issue := range issues { |
| 164 | +// title := issue.Fields.Summary |
| 165 | +// key := issue.Key |
| 166 | +// body := issue.Fields.Description |
| 167 | + |
| 168 | +// // Check if the issue already exists in GitHub |
| 169 | +// searchArgs := []string{ |
| 170 | +// "gh", |
| 171 | +// "issue", |
| 172 | +// "list", |
| 173 | +// "--repo", |
| 174 | +// "cadence-workflow/cadence-java-samples", |
| 175 | +// "--search", |
| 176 | +// fmt.Sprintf("[JIRA issue] %s in:title", title), |
| 177 | +// } |
| 178 | + |
| 179 | +// searchOutput, err := exec.Command(searchArgs[0], searchArgs[1:]...).Output() |
| 180 | +// if err != nil { |
| 181 | +// return err |
| 182 | +// } |
| 183 | + |
| 184 | +// if len(searchOutput) == 0 { |
| 185 | +// Create a new issue in GitHub |
| 186 | + |
| 187 | +// issueData, err := json.Marshal(IssueRequest{ |
| 188 | +// Title: title, |
| 189 | +// Body: body, |
| 190 | +// }) |
| 191 | +// if err != nil { |
| 192 | +// fmt.Println("Error marshaling JSON:", err) |
| 193 | +// } |
| 194 | + |
| 195 | +// url := fmt.Sprintf("https://api.github.com/repos/%s/%s/issues", "vishwa2-uber", "cadence-java-samples") |
| 196 | +// req, err := http.NewRequest("POST", url, bytes.NewBuffer(issueData)) |
| 197 | +// if err != nil { |
| 198 | +// fmt.Println("Error creating request:", err) |
| 199 | +// } |
| 200 | + |
| 201 | +// req.Header.Set("Content-Type", "application/json") |
| 202 | + |
| 203 | +// client := &http.Client{} |
| 204 | +// resp, err := client.Do(req) |
| 205 | +// if err != nil { |
| 206 | +// fmt.Println("Error sending request:", err) |
| 207 | +// } |
| 208 | +// defer resp.Body.Close() |
| 209 | + |
| 210 | +// if resp.StatusCode != http.StatusCreated { |
| 211 | +// fmt.Println("Failed to create issue. Status code:", resp.StatusCode) |
| 212 | +// buf := new(bytes.Buffer) |
| 213 | +// buf.ReadFrom(resp.Body) |
| 214 | +// newStr := buf.String() |
| 215 | +// fmt.Println(newStr) |
| 216 | +// os.Exit(1) |
| 217 | +// // return |
| 218 | +// } |
| 219 | + |
| 220 | +// fmt.Println("Issue created successfully!") |
| 221 | + |
| 222 | +// createArgs := []string{ |
| 223 | +// "gh", |
| 224 | +// "issue", |
| 225 | +// "create", |
| 226 | +// "--repo", |
| 227 | +// "cadence-workflow/cadence-java-samples", |
| 228 | +// "--title", |
| 229 | +// fmt.Sprintf("[JIRA %s] %s", key, title), |
| 230 | +// "--body", |
| 231 | +// body, |
| 232 | +// } |
| 233 | + |
| 234 | +// _, err := exec.Command(createArgs[0], createArgs[1:]...).Output() |
| 235 | +// if err != nil { |
| 236 | +// return err |
| 237 | +// } |
| 238 | +// activity.GetLogger(ctx).Info("Created an issue in GitHub", zap.String("title", title)) |
| 239 | +// } else { |
| 240 | +// activity.GetLogger(ctx).Info("GitHub issue already exists", zap.String("title", title)) |
| 241 | +// } |
| 242 | +// } |
| 243 | +// return nil |
| 244 | +// } |
| 245 | + |
| 246 | +func getEnvVars() (map[string]string, error) { |
| 247 | + jiraArgs := []string{"usso", "-ussh", "t3", "-print"} |
| 248 | + output, err := exec.Command(jiraArgs[0], jiraArgs[1:]...).Output() |
| 249 | + if err != nil { |
| 250 | + return nil, err |
| 251 | + } |
| 252 | + |
| 253 | + githubArgs := []string{"usso", "-ussh", "git", "-print"} |
| 254 | + githubTokenOutput, err := exec.Command(githubArgs[0], githubArgs[1:]...).Output() |
| 255 | + if err != nil { |
| 256 | + return nil, err |
| 257 | + } |
| 258 | + |
| 259 | + envVars := map[string]string{ |
| 260 | + "JIRA_API_TOKEN": string(output), |
| 261 | + "GITHUB_TOKEN": strings.TrimSuffix(string(githubTokenOutput), "\n"), |
| 262 | + } |
| 263 | + |
| 264 | + return envVars, nil |
| 265 | +} |
0 commit comments