Skip to content

Commit 3094a1d

Browse files
authored
feat: add support for dynamic branch (#35)
* feat: add support for dynamic branch * fix: ensure repoBranch is set from project configuration
1 parent e7fd72c commit 3094a1d

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

lib/db/build-index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ async function * loadProject (octokit, graphQL, project, config, _repo) {
6767
}
6868
}
6969

70+
// Check config for repoBranch
71+
project.repoBranch = project.repoBranch ?? repo.branch
72+
7073
let pkg
7174
try {
7275
pkg = await files.getPackageJson(project)
@@ -146,7 +149,7 @@ async function * loadProject (octokit, graphQL, project, config, _repo) {
146149
}
147150

148151
try {
149-
for await (const commit of github.getRepoCommits(graphQL, project.repoOwner, project.repoName)) {
152+
for await (const commit of github.getRepoCommits(graphQL, project.repoOwner, project.repoName, project.repoBranch)) {
150153
yield projectDetail('COMMIT', project, commit)
151154
}
152155
} catch (e) {

lib/github.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const repoQuerySnip = `{
3535
name
3636
}
3737
homepageUrl
38+
defaultBranchRef {
39+
name
40+
}
3841
}`
3942

4043
const issueQuerySnip = `pageInfo {
@@ -136,6 +139,7 @@ class Repo {
136139
this.license = repo.licenseInfo && (repo.licenseInfo.spdx_id || repo.licenseInfo.name)
137140
this.language = repo.primaryLanguage ? repo.primaryLanguage.name : repo.primaryLanguage
138141
this.homepage = repo.homepageUrl
142+
this.branch = repo.defaultBranchRef.name
139143
}
140144
}
141145

@@ -465,12 +469,12 @@ class Commit {
465469
}
466470
}
467471

468-
async function getRemainingCommits (graphQL, owner, repo, cursor) {
472+
async function getRemainingCommits (graphQL, owner, repo, cursor, branch) {
469473
try {
470474
const resp = await graphQL({
471-
query: `query ($org: String!, $repo: String!, $cursor: String!) {
475+
query: `query ($org: String!, $repo: String!, $cursor: String!, $branch: String!) {
472476
repository(name: $repo, owner: $org) {
473-
object(expression: "master") {
477+
object(expression: $branch) {
474478
... on Commit {
475479
history(first: 100, after: $cursor) {
476480
nodes {
@@ -502,10 +506,11 @@ async function getRemainingCommits (graphQL, owner, repo, cursor) {
502506
`,
503507
org: owner,
504508
repo,
505-
cursor
509+
cursor,
510+
branch
506511
})
507512
const { pageInfo, nodes } = resp.repository.object.history
508-
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor))
513+
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor, branch))
509514

510515
return commits
511516
} catch (error) {
@@ -515,12 +520,12 @@ async function getRemainingCommits (graphQL, owner, repo, cursor) {
515520
}
516521

517522
module.exports.getRepoCommits =
518-
async function * getRepoCommits (graphQL, owner, repo) {
523+
async function * getRepoCommits (graphQL, owner, repo, branch) {
519524
try {
520525
const resp = await graphQL({
521-
query: `query ($org: String!, $repo: String!) {
526+
query: `query ($org: String!, $repo: String!, $branch: String!) {
522527
repository(name: $repo, owner: $org) {
523-
object(expression: "master") {
528+
object(expression: $branch) {
524529
... on Commit {
525530
history(first: 100) {
526531
nodes {
@@ -551,10 +556,11 @@ async function * getRepoCommits (graphQL, owner, repo) {
551556
}
552557
`,
553558
org: owner,
554-
repo
559+
repo,
560+
branch
555561
})
556562
const { pageInfo, nodes } = resp.repository.object.history
557-
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor))
563+
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor, branch))
558564

559565
for (const c of commits) {
560566
yield new Commit(owner, repo, c)

lib/project.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports.Project = class Project {
2323
this.repo = repo
2424
this.repoOwner = repoOwner
2525
this.repoName = repoName
26-
this.repoBranch = proj.repoBranch || 'HEAD'
26+
this.repoBranch = proj.repoBranch
2727
this.repoDirectory = proj.repoDirectory || '/'
2828
this.packageName = null
2929
}

0 commit comments

Comments
 (0)