|
| 1 | +// TODO: This is a PoC, need to be cleaned up much more before game time |
| 2 | +package cmd |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/shurcooL/githubv4" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "golang.org/x/oauth2" |
| 12 | +) |
| 13 | + |
| 14 | +var MakeIssueCmd = &cobra.Command{ |
| 15 | + Use: "make-issue", |
| 16 | + Short: "Make an issue to skip identified flaky tests", |
| 17 | + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
| 18 | + if githubToken == "" { |
| 19 | + githubToken = os.Getenv("GITHUB_TOKEN") |
| 20 | + } |
| 21 | + if githubToken == "" { |
| 22 | + return fmt.Errorf("GitHub token not set, set GITHUB_TOKEN env var or use --githubToken flag") |
| 23 | + } |
| 24 | + return nil |
| 25 | + }, |
| 26 | + RunE: makeIssue, |
| 27 | +} |
| 28 | + |
| 29 | +func makeIssue(cmd *cobra.Command, args []string) error { |
| 30 | + // Set up GitHub client |
| 31 | + tok := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken}) |
| 32 | + token := oauth2.NewClient(context.Background(), tok) |
| 33 | + graphqlClient := githubv4.NewClient(token) |
| 34 | + |
| 35 | + // https://docs.github.com/en/copilot/using-github-copilot/coding-agent/using-copilot-to-work-on-an-issue#assigning-an-issue-to-copilot-via-the-github-api |
| 36 | + |
| 37 | + // Get suggestedActors from the API to check if Copilot is enabled |
| 38 | + var suggestedActorsQuery struct { |
| 39 | + Repository struct { |
| 40 | + SuggestedActors struct { |
| 41 | + Nodes []struct { |
| 42 | + Login string |
| 43 | + Typename string `graphql:"__typename"` |
| 44 | + Bot struct { |
| 45 | + ID githubv4.ID |
| 46 | + } `graphql:"... on Bot"` |
| 47 | + User struct { |
| 48 | + ID githubv4.ID |
| 49 | + } `graphql:"... on User"` |
| 50 | + } |
| 51 | + } `graphql:"suggestedActors(capabilities: [CAN_BE_ASSIGNED], first: 100)"` |
| 52 | + } `graphql:"repository(owner: $owner, name: $name)"` |
| 53 | + } |
| 54 | + |
| 55 | + variables := map[string]interface{}{ |
| 56 | + "owner": githubv4.String("smartcontractkit"), // Replace with your repo owner |
| 57 | + "name": githubv4.String("chainlink"), // Replace with your repo name |
| 58 | + } |
| 59 | + |
| 60 | + err := graphqlClient.Query(context.Background(), &suggestedActorsQuery, variables) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("failed to get suggested actors: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + // Check if Copilot coding agent is enabled |
| 66 | + // If enabled, the first node will have login "copilot-swe-agent" |
| 67 | + copilotFound := false |
| 68 | + for _, actor := range suggestedActorsQuery.Repository.SuggestedActors.Nodes { |
| 69 | + if actor.Login == "copilot-swe-agent" { |
| 70 | + copilotFound = true |
| 71 | + break // Found it, no need to continue |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if copilotFound { |
| 76 | + fmt.Println("✅ Copilot coding agent is available for this user and repository") |
| 77 | + } else { |
| 78 | + fmt.Println("❌ Copilot coding agent is not enabled for this user/repository") |
| 79 | + os.Exit(ErrorExitCode) |
| 80 | + } |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func init() { |
| 86 | + MakeIssueCmd.Flags().StringVarP(&githubToken, "githubToken", "t", "", "GitHub token to use for creating the issue (can be set with GITHUB_TOKEN env var)") |
| 87 | +} |
0 commit comments