Skip to content

Commit

Permalink
update channel for feature branches (#92)
Browse files Browse the repository at this point in the history
* update channel for feature branches
* update test
  • Loading branch information
beliaev-maksim authored Mar 16, 2023
1 parent 91ef33d commit 65ee829
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 59 deletions.
13 changes: 6 additions & 7 deletions channel/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# canonical/charming-actions/channel

This action allows you to, based on the github reference, decide which channel on CharHhub to release
This action allows you to, based on the github reference, decide which channel on Charmhub to release
a change to. In itself, this action does not mutate any state on either GitHub or CharmHub.

## Usage
Expand Down Expand Up @@ -29,9 +29,9 @@ None

## Branch Selection

This action automatically selects a Charmhub channel based on the Github branch naming. For `push`
events, a Charmhub channel is selected. For `pull_request` events, a branch is added to the channel, so
that the PR can be tested.
This action automatically selects a Charmhub channel based on the GitHub PR number. For `push`
events, a Charmhub channel is selected. For `pull_request` events, a `pr-<pr number>` is added to the
channel, so that the PR can be tested.

The Charmhub channel/branch selection logic looks like this:

Expand All @@ -40,6 +40,5 @@ The Charmhub channel/branch selection logic looks like this:
| push | | `<default branch>` | `latest/edge` |
| push | | `track/<track-name>` | `<track-name>/edge` |
| push | | Any other name | Ignored |
| pull_request | `<branch-name>` | `<default branch>` | `latest/edge/<branch-name>` |
| pull_request | `<branch-name>` | `track/<track-name>` | `<track-name>/edge/<branch-name>` |
| pull_request | Any other name | Any other name | Ignored |
| pull_request | `<branch-name>` | `track/<track-name>` | `<track-name>/edge/pr-<pr number>`|
| pull_request | Any other name | Any other name | `latest/edge/pr-<pr number>` |
13 changes: 6 additions & 7 deletions dist/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22854,12 +22854,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23194,15 +23196,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
13 changes: 6 additions & 7 deletions dist/check-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23047,12 +23047,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23387,15 +23389,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
13 changes: 6 additions & 7 deletions dist/release-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22946,12 +22946,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23286,15 +23288,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
13 changes: 6 additions & 7 deletions dist/release-libraries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23077,12 +23077,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23417,15 +23419,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
13 changes: 6 additions & 7 deletions dist/upload-bundle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22925,12 +22925,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23265,15 +23267,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
13 changes: 6 additions & 7 deletions dist/upload-charm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22953,12 +22953,14 @@ class Charmcraft {
// however, we expect charmcraft pack to always output one charm file.
const globber = yield glob.create('./*.charm');
const paths = yield globber.glob();
// filter all characters which are not letters, numbers or hyphens
const allowedChannel = channel.replace(/[^a-zA-Z0-9\-/]/gi, '');
const args = [
'upload',
'--format',
'json',
'--release',
channel,
allowedChannel,
paths[0],
...flags,
];
Expand Down Expand Up @@ -23293,15 +23295,12 @@ class Ref {
if (!metadata) {
throw new Error('Pull request metadata missing in the actions context');
}
const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
const { base, number } = metadata;
const branch = `pr-${number}`;
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}
throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}
exports.Ref = Ref;
Expand Down
9 changes: 6 additions & 3 deletions src/services/ref/ref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function createContext(
eventName: string,
ref: string,
main: string,
prRefs: { base?: string; head?: string } = {}
prRefs: { base?: string; head?: string; number?: number } = {}
) {
return {
eventName,
Expand All @@ -24,6 +24,7 @@ function createContext(
head: {
ref: prRefs.head,
},
number: prRefs.number,
},
},
} as unknown as Context;
Expand Down Expand Up @@ -56,17 +57,19 @@ describe('the ref helper', () => {
const context = createContext('pull_request', '', 'main', {
base: 'main',
head: 'branch/PR-123',
number: 123,
});
const ref = new Ref(context);
expect(ref.channel()).toEqual('latest/edge/PR-123');
expect(ref.channel()).toEqual('latest/edge/pr-123');
});
it('should return some-track/edge/whatever when creating a PR towards main', () => {
const context = createContext('pull_request', '', 'main', {
base: 'track/some-track',
head: 'branch/PR-123',
number: 123,
});
const ref = new Ref(context);
expect(ref.channel()).toEqual('some-track/edge/PR-123');
expect(ref.channel()).toEqual('some-track/edge/pr-123');
});
});
});
10 changes: 3 additions & 7 deletions src/services/ref/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,12 @@ export class Ref {
throw new Error('Pull request metadata missing in the actions context');
}

const { base, head } = metadata;
const branch = head.ref.replace('branch/', '');
const { base, number } = metadata;
const branch = `pr-${number}`;

if (base.ref === base.repo.default_branch) {
return `latest/edge/${branch}`;
}
if (base.ref.startsWith('track/')) {
return `${base.ref.replace('track/', '')}/edge/${branch}`;
}

throw new Error(`Unhandled PR base name ${base.ref}`);
return `latest/edge/${branch}`;
}
}

0 comments on commit 65ee829

Please sign in to comment.