1
1
const inquirer = require ( 'inquirer' ) . default
2
2
const knexInit = require ( 'knex' )
3
+ const { simplifyObject } = require ( '@ulisesgascon/simplify-object' )
3
4
const { getConfig } = require ( '../../src/config' )
4
5
const { runWorkflowCommand, listWorkflowCommand } = require ( '../../src/cli' )
5
- const { resetDatabase, getAllProjects, getAllGithubOrgs, addGithubOrg, addProject } = require ( '../../__utils__' )
6
+ const { resetDatabase, getAllProjects, getAllGithubOrgs, addGithubOrg, addProject, getAllGithubRepos , addGithubRepo } = require ( '../../__utils__' )
6
7
const { github } = require ( '../../src/providers' )
7
- const { sampleGithubOrg } = require ( '../../__fixtures__' )
8
+ const { sampleGithubOrg, sampleGithubListOrgRepos , sampleGithubRepository } = require ( '../../__fixtures__' )
8
9
9
10
const { dbSettings } = getConfig ( 'test' )
10
11
@@ -47,23 +48,23 @@ describe('run GENERIC - Non-Interactive Mode', () => {
47
48
} )
48
49
49
50
describe ( 'run update-github-orgs' , ( ) => {
50
- // Mock inquirer for testing
51
- jest . spyOn ( inquirer , 'prompt' ) . mockImplementation ( async ( questions ) => {
52
- const questionMap = {
53
- 'What is the name of the workflow?' : 'update-github-orgs'
54
- }
55
- return questions . reduce ( ( acc , question ) => {
56
- acc [ question . name ] = questionMap [ question . message ]
57
- return acc
58
- } , { } )
59
- } )
51
+ // // Mock inquirer for testing
52
+ // jest.spyOn(inquirer, 'prompt').mockImplementation(async (questions) => {
53
+ // const questionMap = {
54
+ // 'What is the name of the workflow?': 'update-github-orgs'
55
+ // }
56
+ // return questions.reduce((acc, question) => {
57
+ // acc[question.name] = questionMap[question.message]
58
+ // return acc
59
+ // }, {})
60
+ // })
60
61
61
62
test ( 'Should throw an error when no Github orgs are stored in the database' , async ( ) => {
62
63
const projects = await getAllProjects ( knex )
63
64
expect ( projects . length ) . toBe ( 0 )
64
65
const githubOrgs = await getAllGithubOrgs ( knex )
65
66
expect ( githubOrgs . length ) . toBe ( 0 )
66
- await expect ( runWorkflowCommand ( knex , { } ) )
67
+ await expect ( runWorkflowCommand ( knex , { name : 'update-github-orgs' } ) )
67
68
. rejects
68
69
. toThrow ( 'No organizations found. Please add organizations/projects before running this workflow.' )
69
70
} )
@@ -79,7 +80,7 @@ describe('run update-github-orgs', () => {
79
80
expect ( githubOrgs [ 0 ] . description ) . toBe ( null )
80
81
// Mock the fetchOrgByLogin method
81
82
jest . spyOn ( github , 'fetchOrgByLogin' ) . mockResolvedValue ( sampleGithubOrg )
82
- await runWorkflowCommand ( knex , { } )
83
+ await runWorkflowCommand ( knex , { name : 'update-github-orgs' } )
83
84
// Check the database changes
84
85
githubOrgs = await getAllGithubOrgs ( knex )
85
86
expect ( githubOrgs . length ) . toBe ( 1 )
@@ -88,3 +89,61 @@ describe('run update-github-orgs', () => {
88
89
89
90
test . todo ( 'Should throw an error when the Github API is not available' )
90
91
} )
92
+
93
+ describe ( 'run upsert-github-repositories' , ( ) => {
94
+ test ( 'Should throw an error when no Github orgs are stored in the database' , async ( ) => {
95
+ const projects = await getAllProjects ( knex )
96
+ expect ( projects . length ) . toBe ( 0 )
97
+ const githubOrgs = await getAllGithubOrgs ( knex )
98
+ expect ( githubOrgs . length ) . toBe ( 0 )
99
+ await expect ( runWorkflowCommand ( knex , { name : 'upsert-github-repositories' } ) )
100
+ . rejects
101
+ . toThrow ( 'No organizations found. Please add organizations/projects before running this workflow.' )
102
+ } )
103
+ test ( 'Should add the repositories related to the organization' , async ( ) => {
104
+ // Prepare the database
105
+ const project = await addProject ( knex , { name : sampleGithubOrg . login , category : 'impact' } )
106
+ await addGithubOrg ( knex , { login : sampleGithubOrg . login , html_url : sampleGithubOrg . html_url , project_id : project . id } )
107
+ const projects = await getAllProjects ( knex )
108
+ expect ( projects . length ) . toBe ( 1 )
109
+ const githubOrgs = await getAllGithubOrgs ( knex )
110
+ expect ( githubOrgs . length ) . toBe ( 1 )
111
+ let githubRepos = await getAllGithubRepos ( knex )
112
+ expect ( githubRepos . length ) . toBe ( 0 )
113
+ // Mock the github methods used
114
+ jest . spyOn ( github , 'fetchOrgReposListByLogin' ) . mockResolvedValue ( sampleGithubListOrgRepos )
115
+ jest . spyOn ( github , 'fetchRepoByFullName' ) . mockResolvedValue ( sampleGithubRepository )
116
+ await runWorkflowCommand ( knex , { name : 'upsert-github-repositories' } )
117
+ // Check the database changes
118
+ githubRepos = await getAllGithubRepos ( knex )
119
+ expect ( githubRepos . length ) . toBe ( 1 )
120
+ expect ( githubRepos [ 0 ] . description ) . toBe ( sampleGithubRepository . description )
121
+ } )
122
+ test ( 'Should update the repositories related to the organization' , async ( ) => {
123
+ // Prepare the database
124
+ const project = await addProject ( knex , { name : sampleGithubOrg . login , category : 'impact' } )
125
+ const org = await addGithubOrg ( knex , { login : sampleGithubOrg . login , html_url : sampleGithubOrg . html_url , project_id : project . id } )
126
+ const githubRepoData = simplifyObject ( sampleGithubRepository , {
127
+ include : [ 'node_id' , 'name' , 'full_name' , 'html_url' , 'url' , 'git_url' , 'ssh_url' , 'clone_url' , 'visibility' , 'default_branch' ]
128
+ } )
129
+ githubRepoData . github_organization_id = org . id
130
+ githubRepoData . description = 'existing data'
131
+ await addGithubRepo ( knex , githubRepoData )
132
+ const projects = await getAllProjects ( knex )
133
+ expect ( projects . length ) . toBe ( 1 )
134
+ const githubOrgs = await getAllGithubOrgs ( knex )
135
+ expect ( githubOrgs . length ) . toBe ( 1 )
136
+ let githubRepos = await getAllGithubRepos ( knex )
137
+ expect ( githubRepos . length ) . toBe ( 1 )
138
+ expect ( githubRepos [ 0 ] . description ) . toBe ( 'existing data' )
139
+ // Mock the github methods used
140
+ jest . spyOn ( github , 'fetchOrgReposListByLogin' ) . mockResolvedValue ( sampleGithubListOrgRepos )
141
+ jest . spyOn ( github , 'fetchRepoByFullName' ) . mockResolvedValue ( sampleGithubRepository )
142
+ await runWorkflowCommand ( knex , { name : 'upsert-github-repositories' } )
143
+ // Check the database changes
144
+ githubRepos = await getAllGithubRepos ( knex )
145
+ expect ( githubRepos . length ) . toBe ( 1 )
146
+ expect ( githubRepos [ 0 ] . description ) . toBe ( sampleGithubRepository . description )
147
+ } )
148
+ test . todo ( 'Should throw an error when the Github API is not available' )
149
+ } )
0 commit comments