Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions backend/plugins/github/e2e/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@ limitations under the License.
package e2e

import (
"encoding/json"
"fmt"
"testing"
"time"

"github.com/apache/devlake/core/dal"
"github.com/apache/devlake/core/models/domainlayer/crossdomain"
"github.com/apache/devlake/core/models/domainlayer/didgen"
"github.com/apache/devlake/helpers/e2ehelper"
"github.com/apache/devlake/helpers/pluginhelper/api"
"github.com/apache/devlake/plugins/github/impl"
"github.com/apache/devlake/plugins/github/models"
"github.com/apache/devlake/plugins/github/tasks"
graphqlTasks "github.com/apache/devlake/plugins/github_graphql/tasks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -142,3 +147,103 @@ func TestAccountDataFlow(t *testing.T) {
}
assert.True(t, sawOrphanCase, "fixture should include the non-committer orphan case (milichev)")
}

// Without profile enrichment, bot classification must survive every repository's
// conversion of the shared account, including REST aliases of a GraphQL actor.
func TestAccountDataFlowBotWithoutProfile(t *testing.T) {
t.Setenv("GITHUB_PR_EXCLUDELIST", "")
tasks.ResetExcludedUsernamesForTest()
t.Cleanup(tasks.ResetExcludedUsernamesForTest)
df := e2ehelper.NewDataFlowTester(t, "github", impl.Github{})
for _, model := range []interface{ TableName() string }{
&models.GithubAccount{}, &models.GithubAccountOrg{}, &models.GithubRepoAccount{},
&crossdomain.Account{}, &models.GithubPullRequest{}, &models.GithubPrReview{},
&models.GithubReviewer{}, &models.GithubPrLabel{}, &models.GithubPrCommit{},
&models.GithubCommit{}, &models.GithubPrComment{},
} {
df.FlushTabler(model)
}
graphqlTable := "_raw_" + graphqlTasks.RAW_PRS_TABLE
restTable := "_raw_" + tasks.RAW_PR_REVIEW_COMMENTS_TABLE
df.FlushRawTable(graphqlTable)
df.FlushRawTable(restTable)

accountIdGen := didgen.NewDomainIdGenerator(&models.GithubAccount{})
for step, run := range []struct {
repoID int
graphql bool
}{
{101, true}, {102, false}, {101, false}, {102, true},
} {
t.Run(fmt.Sprintf("%d/repo-%d/graphql-%t", step, run.repoID, run.graphql), func(t *testing.T) {
name := fmt.Sprintf("owner/repo-%d", run.repoID)
data := &tasks.GithubTaskData{Options: &tasks.GithubOptions{
ConnectionId: 1, GithubId: run.repoID, Name: name,
}}
params, err := json.Marshal(tasks.GithubApiParams{ConnectionId: 1, Name: name})
require.NoError(t, err)
wantBotLogin := "Copilot[bot]"
if run.graphql {
// Match the real decoder's overlapping fragment fields and the
// collector's JSON serialization, rather than a Bot-only fragment.
raw := fmt.Sprintf(`{
"DatabaseId":%d, "Number":1, "State":"OPEN",
"CreatedAt":"2026-01-01T00:00:00Z", "UpdatedAt":"2026-01-02T00:00:00Z",
"Author":{
"TypeName":"User", "Login":"octocat", "Id":583231,
"Bot":{"Login":"octocat", "Id":583231}
},
"Reviews":{"Nodes":[{
"databaseId":%d, "state":"COMMENTED",
"Author":{
"TypeName":"Bot", "Login":"copilot-pull-request-reviewer", "Id":175728472,
"Bot":{"Login":"copilot-pull-request-reviewer", "Id":175728472}
}
}]}
}`, run.repoID, run.repoID)
var pr graphqlTasks.GraphqlQueryPr
require.NoError(t, json.Unmarshal([]byte(raw), &pr))
serialized, err := json.Marshal(pr)
require.NoError(t, err)
require.NoError(t, df.Db.Table(graphqlTable).Create(&api.RawData{
Params: string(params), Data: serialized, CreatedAt: time.Now().UTC(),
}).Error)
df.Subtask(graphqlTasks.ExtractPrsMeta, data)
wantBotLogin = "copilot-pull-request-reviewer[bot]"
} else {
for i, user := range []string{
`{"login":"Copilot","id":175728472,"type":"Bot"}`,
`{"login":"octocat","id":583231,"type":"User"}`,
} {
raw := fmt.Sprintf(`{"id":%d,"body":"review","user":%s,"pull_request_url":"https://api.github.com/repos/%s/pulls/1","created_at":"2026-01-01T00:00:00Z","updated_at":"2026-01-02T00:00:00Z"}`, run.repoID*10+i, user, name)
require.NoError(t, df.Db.Table(restTable).Create(&api.RawData{
Params: string(params), Data: []byte(raw), CreatedAt: time.Now().UTC(),
}).Error)
}
df.Subtask(tasks.ExtractApiPrReviewCommentsMeta, data)
}
df.Subtask(tasks.ConvertAccountsMeta, data)
for _, expected := range []struct {
id int
login string
bot bool
}{
{175728472, wantBotLogin, true}, {583231, "octocat", false},
} {
var mapping models.GithubRepoAccount
require.NoError(t, df.Dal.First(&mapping, dal.Where("connection_id = ? AND repo_github_id = ? AND account_id = ?", 1, run.repoID, expected.id)))
assert.Equal(t, expected.login, mapping.Login)
var account crossdomain.Account
require.NoError(t, df.Dal.First(&account, dal.Where("id = ?", accountIdGen.Generate(uint64(1), expected.id))))
assert.Equal(t, expected.login, account.UserName)
assert.Equal(t, expected.bot, account.IsBot)
}
})
}
count, err := df.Dal.Count(dal.From(&models.GithubAccount{}))
require.NoError(t, err)
assert.Zero(t, count, "profile enrichment must remain absent throughout the test")
count, err = df.Dal.Count(dal.From(&crossdomain.Account{}))
require.NoError(t, err)
assert.Equal(t, int64(2), count, "repositories must share the same numeric account identities")
}
10 changes: 9 additions & 1 deletion backend/plugins/github/tasks/account_pre_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package tasks

import (
"strings"

"github.com/apache/devlake/core/errors"
"github.com/apache/devlake/plugins/github/models"
)
Expand All @@ -44,10 +46,16 @@ type GithubAccountResponse struct {
}

func convertAccount(res *GithubAccountResponse, repoId int, connId uint64) (*models.GithubRepoAccount, errors.Error) {
login := res.Login
// Some REST endpoints return bot aliases without the usual suffix. Keep
// the explicit type available to conversion even without profile enrichment.
if res.Type == "Bot" && !strings.HasSuffix(login, "[bot]") {
login += "[bot]"
}
githubAccount := &models.GithubRepoAccount{
ConnectionId: connId,
RepoGithubId: repoId,
Login: res.Login,
Login: login,
AccountId: res.Id,
}
return githubAccount, nil
Expand Down
48 changes: 48 additions & 0 deletions backend/plugins/github/tasks/account_pre_extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package tasks

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConvertAccountBotType(t *testing.T) {
for _, tc := range []struct{ name, raw, wantLogin string }{
{"bot alias", `{"login":"Copilot","id":175728472,"type":"Bot"}`, "Copilot[bot]"},
{"suffixed bot", `{"login":"copilot-pull-request-reviewer[bot]","id":175728472,"type":"Bot"}`, "copilot-pull-request-reviewer[bot]"},
{"human", `{"login":"octocat","id":583231,"type":"User"}`, "octocat"},
{"no type", `{"login":"legacy","id":1}`, "legacy"},
} {
t.Run(tc.name, func(t *testing.T) {
var response GithubAccountResponse
require.NoError(t, json.Unmarshal([]byte(tc.raw), &response))
original := response
account, err := convertAccount(&response, 42, 7)
require.NoError(t, err)
assert.Equal(t, tc.wantLogin, account.Login)
assert.Equal(t, response.Id, account.AccountId)
assert.Equal(t, 42, account.RepoGithubId)
assert.Equal(t, uint64(7), account.ConnectionId)
assert.Equal(t, original, response, "normalization must not mutate the API response")
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (q *GraphqlInlineAccountQuery) Account() GithubAccountEdge {
// `User`-typed field makes GitHub reject the whole query with
// "Fragment on Bot can't be spread inside User".
type GraphqlInlineActorQuery struct {
TypeName string `graphql:"__typename"`
GithubAccountEdge `graphql:"... on User"`
Bot GithubBotEdge `graphql:"... on Bot"`
}
Expand All @@ -79,8 +80,21 @@ func (q *GraphqlInlineActorQuery) Account() GithubAccountEdge {
if q == nil {
return GithubAccountEdge{}
}
if q.GithubAccountEdge.Id != 0 {
// The GraphQL decoder populates overlapping fields in both fragments.
// Only __typename identifies which fragment actually resolved.
switch q.TypeName {
case "User":
return q.GithubAccountEdge
case "Bot":
// Normalize the bot fragment below.
case "":
// Historical raw records lack a discriminator. Preserve their original
// user-first behavior, including when both fragments are populated.
if q.GithubAccountEdge.Id != 0 {
return q.GithubAccountEdge
}
default:
return GithubAccountEdge{}
}
if q.Bot.Id == 0 {
return GithubAccountEdge{}
Expand Down
Loading