summaryrefslogtreecommitdiff
path: root/internal/github/github.go
blob: 38aafa0a66c0587a7c4e5b4a5dc529b5a14d493c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package github

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

// Client handles GitHub API operations
type Client struct {
	token string
	org   string
}

// NewClient creates a new GitHub API client
func NewClient(token, org string) *Client {
	// If no token provided, try other sources
	if token == "" {
		fmt.Println("  No token in config, trying environment variable...")
		// Try environment variable
		token = os.Getenv("GITHUB_TOKEN")
		
		// If still no token, try reading from file
		if token == "" {
			fmt.Println("  No GITHUB_TOKEN env var, trying ~/.gitsyncer_github_token file...")
			home, err := os.UserHomeDir()
			if err == nil {
				tokenFile := filepath.Join(home, ".gitsyncer_github_token")
				data, err := os.ReadFile(tokenFile)
				if err == nil {
					token = strings.TrimSpace(string(data))
					fmt.Printf("  Loaded token from file (length: %d)\n", len(token))
					// Check for common issues
					if strings.Contains(token, "\n") || strings.Contains(token, "\r") {
						fmt.Println("  Warning: Token contains newline characters")
					}
					if strings.HasPrefix(token, " ") || strings.HasSuffix(token, " ") {
						fmt.Println("  Warning: Token has leading/trailing spaces")
					}
				} else {
					fmt.Printf("  Could not read token file: %v\n", err)
				}
			}
		} else {
			fmt.Printf("  Loaded token from env var (length: %d)\n", len(token))
		}
	} else {
		fmt.Printf("  Using token from config (length: %d)\n", len(token))
	}
	return &Client{
		token: token,
		org:   org,
	}
}

// CreateRepoRequest represents the request to create a repository
type CreateRepoRequest struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Private     bool   `json:"private"`
	AutoInit    bool   `json:"auto_init"`
}

// CreateRepoResponse represents the response from creating a repository
type CreateRepoResponse struct {
	ID       int64  `json:"id"`
	Name     string `json:"name"`
	FullName string `json:"full_name"`
	Private  bool   `json:"private"`
	SSHURL   string `json:"ssh_url"`
	CloneURL string `json:"clone_url"`
}

// ErrorResponse represents an error response from GitHub API
type ErrorResponse struct {
	Message string `json:"message"`
	Errors  []struct {
		Resource string `json:"resource"`
		Field    string `json:"field"`
		Code     string `json:"code"`
	} `json:"errors,omitempty"`
}

// RepoExists checks if a repository exists
func (c *Client) RepoExists(repoName string) (bool, error) {
	if c.token == "" {
		return false, fmt.Errorf("GitHub token required")
	}

	url := fmt.Sprintf("https://api.github.com/repos/%s/%s", c.org, repoName)
	fmt.Printf("  Checking URL: %s\n", url)
	fmt.Printf("  Token present: %v (length: %d)\n", c.token != "", len(c.token))
	
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return false, err
	}
	
	req.Header.Set("Authorization", "Bearer "+c.token)
	req.Header.Set("Accept", "application/vnd.github.v3+json")
	
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return false, err
	}
	defer resp.Body.Close()
	
	if resp.StatusCode == 200 {
		return true, nil
	} else if resp.StatusCode == 404 {
		return false, nil
	} else if resp.StatusCode == 401 {
		// Read the response body for 401 errors
		body, _ := io.ReadAll(resp.Body)
		fmt.Printf("  401 Unauthorized - Response: %s\n", string(body))
		fmt.Printf("  Authorization header: %s\n", req.Header.Get("Authorization"))
		return false, fmt.Errorf("authentication failed (401): %s", string(body))
	}
	
	return false, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

// CreateRepo creates a new repository
func (c *Client) CreateRepo(repoName, description string, private bool) error {
	if c.token == "" {
		return fmt.Errorf("GitHub token required to create repository")
	}

	fmt.Printf("  Checking if GitHub repo %s/%s exists...\n", c.org, repoName)
	// First check if it already exists
	exists, err := c.RepoExists(repoName)
	if err != nil {
		return fmt.Errorf("failed to check if repo exists: %w", err)
	}
	if exists {
		fmt.Printf("  GitHub repo already exists, skipping creation\n")
		// Repo already exists, nothing to do
		return nil
	}
	
	url := fmt.Sprintf("https://api.github.com/user/repos")
	
	reqBody := CreateRepoRequest{
		Name:        repoName,
		Description: description,
		Private:     private,
		AutoInit:    false, // Don't auto-init, we'll push content
	}
	
	jsonBody, err := json.Marshal(reqBody)
	if err != nil {
		return err
	}
	
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		return err
	}
	
	req.Header.Set("Authorization", "Bearer "+c.token)
	req.Header.Set("Accept", "application/vnd.github.v3+json")
	req.Header.Set("Content-Type", "application/json")
	
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	
	if resp.StatusCode == 201 {
		var createResp CreateRepoResponse
		if err := json.NewDecoder(resp.Body).Decode(&createResp); err != nil {
			return fmt.Errorf("failed to parse response: %w", err)
		}
		fmt.Printf("Created GitHub repository: %s\n", createResp.FullName)
		return nil
	}
	
	// Handle error response
	var errResp ErrorResponse
	if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
		return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
	}
	
	if errResp.Message != "" {
		return fmt.Errorf("GitHub API error: %s", errResp.Message)
	}
	
	return fmt.Errorf("failed to create repository: status %d", resp.StatusCode)
}

// HasToken returns whether a token is configured
func (c *Client) HasToken() bool {
	return c.token != ""
}