Skip to content

Commit

Permalink
fix(visibility): set repo visibility based on provided value
Browse files Browse the repository at this point in the history
rather than always defaulting to `Private`
  • Loading branch information
travi committed Jul 2, 2024
1 parent abc9e11 commit a5e2a8d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ to leverage the GitHub API integration benefits of this plugin.

```javascript
import {scaffold} from '@form8ion/github';
import any from '@travi/any';
```

#### Execute
Expand All @@ -70,7 +71,8 @@ import {scaffold} from '@form8ion/github';
await scaffold({
projectRoot: process.cwd(),
name: 'foo',
owner: 'travi'
owner: 'travi',
visibility: any.fromList(['Public', 'Private'])
});
```

Expand Down
4 changes: 3 additions & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// remark-usage-ignore-next 2
import {resolve} from 'path';
import stubbedFs from 'mock-fs';
import any from '@travi/any';
import {scaffold} from './lib/index.js';

// remark-usage-ignore-next
Expand All @@ -12,5 +13,6 @@ stubbedFs({node_modules: stubbedFs.load(resolve('node_modules'))});
await scaffold({
projectRoot: process.cwd(),
name: 'foo',
owner: 'travi'
owner: 'travi',
visibility: any.fromList(['Public', 'Private'])
});
4 changes: 2 additions & 2 deletions src/scaffolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {info} from '@travi/cli-messages';
import {factory as getAuthenticatedOctokit} from './octokit/factory.js';
import {scaffold as scaffoldRepository} from './repository/index.js';

export default async function ({name, owner}) {
export default async function ({name, owner, visibility}) {
info('Initializing GitHub');

const octokit = getAuthenticatedOctokit();

const repositoryResult = await scaffoldRepository({octokit, name, owner});
const repositoryResult = await scaffoldRepository({octokit, name, owner, visibility});

return {...repositoryResult};
}
7 changes: 5 additions & 2 deletions src/scaffolder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ describe('scaffolder', () => {
const repositoryResult = any.simpleObject();
const name = any.word();
const owner = any.word();
const visibility = any.word();
octokitFactory.mockReturnValue(octokitClient);
when(scaffoldRepository).calledWith({octokit: octokitClient, name, owner}).mockResolvedValue(repositoryResult);
when(scaffoldRepository)
.calledWith({octokit: octokitClient, name, owner, visibility})
.mockResolvedValue(repositoryResult);

expect(await scaffold({name, owner})).toEqual({...repositoryResult});
expect(await scaffold({name, owner, visibility})).toEqual({...repositoryResult});
});
});
28 changes: 26 additions & 2 deletions test/integration/features/scaffold.feature
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
Feature: Scaffolder

Scenario: new repository
Scenario: new public repository
Given netrc contains a GitHub token
And no repository exists for the "user" on GitHub
And the visibility of the repository should be "Public"
# And next steps are provided
When the project is scaffolded
Then a repository is created on GitHub
# And issues are created for next-steps
# And repository settings are configured
And repository details are returned

Scenario: new private repository
Given netrc contains a GitHub token
And no repository exists for the "user" on GitHub
And the visibility of the repository should be "Private"
# And next steps are provided
When the project is scaffolded
Then a repository is created on GitHub
Expand Down Expand Up @@ -34,11 +46,23 @@ Feature: Scaffolder
# But repository settings are configured
And no repository details are returned

Scenario: user is a member of an organization and the project is new
Scenario: user is a member of an organization and the public project is new
Given netrc contains a GitHub token
And the user is a member of an organization
And no repository exists for the "organization" on GitHub
And the visibility of the repository should be "Public"
When the project is scaffolded
Then a repository is created on GitHub
# And repository settings are configured
And repository details are returned

Scenario: user is a member of an organization and the private project is new
Given netrc contains a GitHub token
And the user is a member of an organization
And no repository exists for the "organization" on GitHub
And the visibility of the repository should be "Private"
When the project is scaffolded
Then a repository is created on GitHub
# And repository settings are configured
And repository details are returned

Expand Down
3 changes: 2 additions & 1 deletion test/integration/features/step_definitions/common-steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ When('the project is scaffolded', async function () {
this.result = await scaffold({
projectRoot: this.projectRoot,
name: this.projectName,
owner: this.githubUser
owner: this.githubUser,
visibility: this.projectVisibility
});
} catch (err) {
debug(err);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {StatusCodes} from 'http-status-codes';

import {Given, Then} from '@cucumber/cucumber';
import {http, HttpResponse} from 'msw';
import {StatusCodes} from 'http-status-codes';
import assert from 'node:assert';

import {authorizationHeaderIncludesToken} from './repository-steps.js';
import {assert} from 'chai';

Given('the user is a member of an organization', async function () {
this.githubUser = this.organizationAccount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ Given('no repository exists for the {string} on GitHub', async function (account
);

server.use(
http.post(`https://api.github.com/orgs/${organizationAccount}/repos`, ({request}) => {
http.post(`https://api.github.com/orgs/${organizationAccount}/repos`, async ({request}) => {
if (authorizationHeaderIncludesToken(request)) {
this.createdRepositoryDetails = await request.clone().json();

return HttpResponse.json({
ssh_url: sshUrl,
html_url: htmlUrl
Expand Down Expand Up @@ -128,4 +130,5 @@ Then('no repository is created on GitHub', async function () {

Then('a repository is created on GitHub', async function () {
assert.equal(this.createdRepositoryDetails.name, this.projectName);
assert.equal(this.createdRepositoryDetails.private, 'Public' !== this.projectVisibility);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {Given} from '@cucumber/cucumber';

Given('the visibility of the repository should be {string}', async function (visibility) {
this.projectVisibility = visibility;
});

0 comments on commit a5e2a8d

Please sign in to comment.