-
Notifications
You must be signed in to change notification settings - Fork 0
[Actions] Add General HTTP Driver to Enrich Pull Request #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onairmarc
wants to merge
4
commits into
main
Choose a base branch
from
enrich-http
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
359ff7c
[Actions] Add General HTTP Driver to Enrich Pull Request
onairmarc af84fcb
Update actions/github/enrichPullRequest/drivers/general/general.go
onairmarc 5d2f3d6
Update actions/github/enrichPullRequest/drivers/general/general.go
onairmarc 7a05f13
[Actions] Add General HTTP API Strategy to Enrich Pull Request
onairmarc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
223 changes: 223 additions & 0 deletions
223
actions/github/enrichPullRequest/drivers/general/general.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| package general | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/EncoreDigitalGroup/golib/logger" | ||
|
|
||
| branchname "github.com/EncoreDigitalGroup/ci-workflows/actions/github/enrichPullRequest/drivers/branch_name" | ||
| "github.com/EncoreDigitalGroup/ci-workflows/actions/github/enrichPullRequest/support/github" | ||
| ) | ||
|
|
||
| type Configuration struct { | ||
| Enable bool | ||
| Endpoint string | ||
| AuthToken string | ||
| TicketID string | ||
| } | ||
|
|
||
| type Label struct { | ||
| Title string `json:"title"` | ||
| Description *string `json:"description"` | ||
| Color *string `json:"color"` | ||
| } | ||
|
|
||
| type APIResponse struct { | ||
| Title string `json:"title"` | ||
| Description *string `json:"description"` | ||
| Assignee *string `json:"assignee"` | ||
| Labels *[]Label `json:"labels"` | ||
| } | ||
|
|
||
| type HTTPError struct { | ||
| StatusCode int | ||
| Message string | ||
| } | ||
|
|
||
| func (e *HTTPError) Error() string { | ||
| return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Message) | ||
| } | ||
|
|
||
| func Format(gh github.GitHub) { | ||
| branchName, err := gh.GetBranchName() | ||
| if err != nil { | ||
| logger.Error(err.Error()) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| endpoint := os.Getenv("OPT_HTTP_ENDPOINT") | ||
| if endpoint == "" { | ||
| logger.Error("OPT_HTTP_ENDPOINT is not set") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| authToken := os.Getenv("OPT_AUTH_TOKEN") | ||
| if authToken == "" { | ||
| logger.Error("OPT_AUTH_TOKEN is not set") | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| ticketID, err := branchname.GetIssueKeyFromBranchName(branchName) | ||
| if err != nil { | ||
| logger.Error(err.Error()) | ||
| os.Exit(1) | ||
| } | ||
|
|
||
| if ticketID == "" { | ||
| logger.Error("Ticket ID is empty") | ||
| return | ||
| } | ||
|
|
||
| config := Configuration{ | ||
| Enable: true, | ||
| Endpoint: endpoint, | ||
| AuthToken: authToken, | ||
| TicketID: ticketID, | ||
| } | ||
|
|
||
| apiResponse, err := getTicketInfo(config) | ||
| if err != nil { | ||
| var httpErr *HTTPError | ||
| if errors.As(err, &httpErr) { | ||
| if httpErr.StatusCode == 404 { | ||
| logger.Errorf("Ticket not found: %s", ticketID) | ||
| comment := fmt.Sprintf("**Ticket Not Found**\n\n"+ | ||
| "Unable to find ticket information for: `%s`\n\n"+ | ||
| "Please verify that the ticket ID in your branch name is correct.", ticketID) | ||
| gh.AddPRComment(comment) | ||
| return | ||
| } | ||
|
|
||
| logger.Errorf("HTTP API error: %v", httpErr) | ||
| comment := "**API Error**\n\n" + | ||
| "Failed to fetch ticket information due to an API error. " + | ||
| "Please check the GitHub Action logs for specific error information." | ||
| gh.AddPRComment(comment) | ||
| return | ||
| } | ||
|
|
||
| logger.Errorf("Failed to get ticket info: %v", err) | ||
| comment := "Failed to get information from the API.\n\n" + | ||
| "Please check the GitHub Action logs for specific error information." | ||
| gh.AddPRComment(comment) | ||
| return | ||
| } | ||
|
|
||
| newPRTitle := apiResponse.Title | ||
| if newPRTitle == "" { | ||
| logger.Error("API response missing required 'title' field") | ||
| comment := "**Invalid API Response**\n\n" + | ||
| "The API response is missing the required 'title' field." | ||
| gh.AddPRComment(comment) | ||
| return | ||
| } | ||
|
|
||
| var newPRDescription string | ||
| if apiResponse.Description != nil { | ||
| newPRDescription = *apiResponse.Description | ||
| gh.UpdatePR(newPRTitle, newPRDescription) | ||
| } else { | ||
| gh.UpdatePRTitle(newPRTitle) | ||
| } | ||
|
|
||
| if apiResponse.Assignee != nil && *apiResponse.Assignee != "" { | ||
| logger.Infof("Assignee information received: %s (Note: GitHub assignee setting not implemented)", *apiResponse.Assignee) | ||
| } | ||
|
|
||
| if apiResponse.Labels != nil { | ||
| for _, label := range *apiResponse.Labels { | ||
| if label.Title == "" { | ||
| continue | ||
| } | ||
|
|
||
| description := "" | ||
| if label.Description != nil { | ||
| description = *label.Description | ||
| } | ||
|
|
||
| color := "" | ||
| if label.Color != nil { | ||
| color = *label.Color | ||
| } | ||
|
|
||
| gh.EnsureLabelExists(label.Title, description, color) | ||
| gh.AddLabelToPR(label.Title) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func getTicketInfo(config Configuration) (*APIResponse, error) { | ||
| if !config.Enable { | ||
| return nil, errors.New("general driver is not enabled") | ||
| } | ||
|
|
||
| if config.Endpoint == "" || config.AuthToken == "" || config.TicketID == "" { | ||
| return nil, errors.New("missing required configuration: endpoint, auth token, or ticket ID") | ||
| } | ||
|
|
||
| reqURL, err := url.Parse(config.Endpoint) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid endpoint URL: %v", err) | ||
| } | ||
|
|
||
| query := reqURL.Query() | ||
| query.Set("id", config.TicketID) | ||
| reqURL.RawQuery = query.Encode() | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, "GET", reqURL.String(), nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create request: %v", err) | ||
| } | ||
|
|
||
| req.Header.Set("Authorization", "Bearer "+config.AuthToken) | ||
| req.Header.Set("Accept", "application/json") | ||
|
|
||
| client := &http.Client{} | ||
|
|
||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to make HTTP request: %v", err) | ||
| } | ||
| defer func() { | ||
| if err := resp.Body.Close(); err != nil { | ||
| logger.Errorf("Failed to close response body: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read response body: %v", err) | ||
| } | ||
|
|
||
| if resp.StatusCode == 404 { | ||
| return nil, &HTTPError{ | ||
| StatusCode: resp.StatusCode, | ||
| Message: "ticket not found", | ||
| } | ||
| } | ||
|
|
||
| if resp.StatusCode != 200 { | ||
| return nil, &HTTPError{ | ||
| StatusCode: resp.StatusCode, | ||
| Message: string(body), | ||
| } | ||
| } | ||
|
|
||
| var apiResponse APIResponse | ||
| if err := json.Unmarshal(body, &apiResponse); err != nil { | ||
| return nil, fmt.Errorf("failed to parse JSON response: %v", err) | ||
| } | ||
|
|
||
| return &apiResponse, nil | ||
| } | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.