Skip to content

Commit 3a1cf9c

Browse files
authored
fix: auto-enrich missing URL fields in GitHub scopes (#8661)
Add automatic URL enrichment to PutScopes function to fetch missing htmlUrl and cloneUrl fields from GitHub API when only fullName is provided. This ensures gitextractor has the required URLs for cloning repositories. - Fetch repo details from GitHub API when URLs are missing - Populate htmlUrl and cloneUrl from API response - Add graceful error handling for inaccessible repos
1 parent e7a60cb commit 3a1cf9c

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

backend/plugins/github/api/scope_api.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ limitations under the License.
1818
package api
1919

2020
import (
21+
"context"
22+
2123
"github.com/apache/incubator-devlake/core/errors"
2224
"github.com/apache/incubator-devlake/core/plugin"
2325
"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
2426
"github.com/apache/incubator-devlake/plugins/github/models"
27+
"github.com/apache/incubator-devlake/plugins/github/tasks"
2528
)
2629

2730
type PutScopesReqBody api.PutScopesReqBody[models.GithubRepo]
@@ -39,6 +42,50 @@ type ScopeDetail api.ScopeDetail[models.GithubRepo, models.GithubScopeConfig]
3942
// @Failure 500 {object} shared.ApiBody "Internal Error"
4043
// @Router /plugins/github/connections/{connectionId}/scopes [PUT]
4144
func PutScopes(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
45+
// Enrich missing URL fields by fetching from GitHub API
46+
connection, err := dsHelper.ConnApi.GetMergedConnection(input)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, connection)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
data, ok := input.Body["data"].([]interface{})
57+
if ok {
58+
for _, row := range data {
59+
dict, ok := row.(map[string]interface{})
60+
if !ok {
61+
continue
62+
}
63+
64+
// Check if URL fields are missing
65+
htmlUrl, hasHtmlUrl := dict["htmlUrl"].(string)
66+
cloneUrl, hasCloneUrl := dict["cloneUrl"].(string)
67+
fullName, hasFullName := dict["fullName"].(string)
68+
69+
// If URLs are missing but we have fullName, fetch from GitHub API
70+
if hasFullName && fullName != "" && ((!hasHtmlUrl || htmlUrl == "") || (!hasCloneUrl || cloneUrl == "")) {
71+
repo, err := getApiRepo(&tasks.GithubOptions{Name: fullName}, apiClient)
72+
if err != nil {
73+
// Log warning but don't fail - might be a deleted/private repo
74+
basicRes.GetLogger().Warn(err, "failed to fetch repo details for %s, URL fields may be incomplete", fullName)
75+
continue
76+
}
77+
78+
// Populate missing fields from API
79+
if !hasHtmlUrl || htmlUrl == "" {
80+
dict["htmlUrl"] = repo.HTMLUrl
81+
}
82+
if !hasCloneUrl || cloneUrl == "" {
83+
dict["cloneUrl"] = repo.CloneUrl
84+
}
85+
}
86+
}
87+
}
88+
4289
return dsHelper.ScopeApi.PutMultiple(input)
4390
}
4491

0 commit comments

Comments
 (0)