Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for being unable to rename github repo #52

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion GitHub-Repositories-Repository/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ _Required_: Yes

_Type_: String

_Update requires_: [Replacement](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-replacement)
_Update requires_: [No interruption](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks-update-behaviors.html#update-no-interrupt)

#### Description

Expand Down Expand Up @@ -301,6 +301,10 @@ The `Fn::GetAtt` intrinsic function returns a value for a specified attribute of

For more information about using the `Fn::GetAtt` intrinsic function, see [Fn::GetAtt](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html).

#### Id

The ID of the repository.

#### Owner

ID of the repository owner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
]
},
"properties": {
"Id": {
"description": "The ID of the repository.",
"type": "number"
},
"Organization": {
"description": "The organization name. The name is not case sensitive. If not specified, then the managed repository will be within the currently logged-in user account.",
"type": "string"
Expand Down Expand Up @@ -245,6 +249,7 @@
"Name"
],
"readOnlyProperties": [
"/properties/Id",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might break existing stack updates. Please try testing following scenario :
1.Create a stack with out this changes(using current prod changes).
2.Update the stack with this changes.

"/properties/Owner",
"/properties/HtmlUrl",
"/properties/GitUrl",
Expand All @@ -257,7 +262,6 @@
],
"createOnlyProperties": [
"/properties/Organization",
"/properties/Name",
"/properties/TeamId",
"/properties/AutoInit",
"/properties/GitIgnoreTemplate",
Expand All @@ -273,24 +277,34 @@
"/properties/SecurityAndAnalysis"
],
"primaryIdentifier": [
"/properties/Owner",
"/properties/Name"
"/properties/Id",
"/properties/Owner"
],
"handlers": {
"create": {
"permissions": []
"permissions": [
"appsync:CreateApiKey"
]
},
"read": {
"permissions": []
"permissions": [
"appsync:CreateApiKey"
]
},
"update": {
"permissions": []
"permissions": [
"appsync:CreateApiKey"
]
},
"delete": {
"permissions": []
"permissions": [
"appsync:CreateApiKey"
]
},
"list": {
"permissions": []
"permissions": [
"appsync:CreateApiKey"
]
}
}
}
1 change: 0 additions & 1 deletion GitHub-Repositories-Repository/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions GitHub-Repositories-Repository/resource-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ Resources:
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Deny
- Effect: Allow
Action:
- "*"
- "appsync:CreateApiKey"
Resource: "*"
Outputs:
ExecutionRoleArn:
Expand Down
27 changes: 18 additions & 9 deletions GitHub-Repositories-Repository/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ class Resource extends AbstractGitHubResource<ResourceModel, GetUserRepoResponse
userAgent: this.userAgent
});

const response = await octokit.request<GetUserRepoEndpoint>('GET /repos/{owner}/{repo}', {
owner: model.owner,
repo: model.name
// Note - in order to allow renaming the github repo, we need to use the id instead of owner and name for the path.
// See here for more information about this api (since it's undocumented): https://github.com/piotrmurach/github/issues/282
// ts ignore below necessary as this path is not part of octokit endpoints

// @ts-ignore
const response = await octokit.request<GetRepoByIdEndpoint>('GET /repositories/{id}', {
id: model.id
});

return response.data;
Expand Down Expand Up @@ -168,9 +172,14 @@ class Resource extends AbstractGitHubResource<ResourceModel, GetUserRepoResponse
payload.allow_forking = model.allowForking;
}

const response = await octokit.request<UpdateRepoEndpoint>(
'PATCH /repos/{owner}/{repo}',
payload);
const pathWithId = `PATCH /repositories/${model.id}`;

// Note - in order to allow renaming the github repo, we need to use the id instead of owner and name for the path.
// See here for more information about this api (since it's undocumented): https://github.com/piotrmurach/github/issues/282
// ts ignore below necessary as this path is not part of octokit endpoints

// @ts-ignore
const response = await octokit.request<UpdateRepoEndpoint>(pathWithId, payload);

return response.data;
}
Expand All @@ -181,9 +190,8 @@ class Resource extends AbstractGitHubResource<ResourceModel, GetUserRepoResponse
userAgent: this.userAgent
});

await octokit.request('DELETE /repos/{owner}/{repo}', {
owner: model.owner,
repo: model.name
await octokit.request('DELETE /repositories/{repositoryId}', {
repositoryId: model.id,
});
}

Expand All @@ -199,6 +207,7 @@ class Resource extends AbstractGitHubResource<ResourceModel, GetUserRepoResponse
delete from.updated_at;

let resourceModel: ResourceModel = new ResourceModel( {
id: from.id,
owner: from.owner?.login ? from.owner.login : model.owner,
licenseTemplate: from.license?.key,
name: from.name,
Expand Down
21 changes: 15 additions & 6 deletions GitHub-Repositories-Repository/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ export class ResourceModel extends BaseModel {
public static readonly TYPE_NAME: string = 'GitHub::Repositories::Repository';

@Exclude()
protected readonly IDENTIFIER_KEY_OWNER: string = '/properties/Owner';
protected readonly IDENTIFIER_KEY_ID: string = '/properties/Id';
@Exclude()
protected readonly IDENTIFIER_KEY_NAME: string = '/properties/Name';
protected readonly IDENTIFIER_KEY_OWNER: string = '/properties/Owner';

@Expose({ name: 'Id' })
@Transform(
(value: any, obj: any) =>
transformValue(Number, 'id', value, obj, []),
{
toClassOnly: true,
}
)
id?: Optional<number>;
@Expose({ name: 'Organization' })
@Transform(
(value: any, obj: any) =>
Expand Down Expand Up @@ -290,12 +299,12 @@ export class ResourceModel extends BaseModel {
@Exclude()
public getPrimaryIdentifier(): Dict {
const identifier: Dict = {};
if (this.owner != null) {
identifier[this.IDENTIFIER_KEY_OWNER] = this.owner;
if (this.id != null) {
identifier[this.IDENTIFIER_KEY_ID] = this.id;
}

if (this.name != null) {
identifier[this.IDENTIFIER_KEY_NAME] = this.name;
if (this.owner != null) {
identifier[this.IDENTIFIER_KEY_OWNER] = this.owner;
}

// only return the identifier if it can be used, i.e. if all components are present
Expand Down