Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit d45b015

Browse files
committed
Converting the action into a Typescript Action instead of being a javascript action. Not using babel anymore. For testing: instead of jasmine, using jest.
1 parent 1f9e062 commit d45b015

20 files changed

+2850
-2788
lines changed

actions/add-review-url/.babelrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

actions/add-review-url/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ Navigate to the directory: .\actions\add-review-url\src and run
2121
npm run build
2222
```
2323
## How to build and test the unit tests for this GitHub Action
24-
Using Jasmine Test Framework. Please refer: https://medium.com/backticks-tildes/how-to-test-javascript-with-jasmine-framework-2e2b8dfa7a9e
25-
Refer to tests in \actions\add-review-url\test
24+
Using Jest Test Framework.
25+
Refer to tests in \actions\add-review-url\__test__
2626
```
2727
npm install
2828
npm run build
29-
npm run buildtest
30-
npm test
29+
npm run test
3130
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
import addReviewUrl from '../src/AddReviewUrl';
4+
beforeEach(() => {
5+
if(!process.env.GITHUB_REPOSITORY)
6+
{
7+
process.env.GITHUB_REPOSITORY = 'someactionowner/reponame';
8+
}
9+
});
10+
test('Add-review-url tests', () => {
11+
const addComment = new addReviewUrl();
12+
var a:string[] = addComment.getOwnerAndRepo();
13+
expect(a[0]).toBe('someactionowner');
14+
expect(a[1]).toBe('reponame');
15+
});

actions/add-review-url/action.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ description: 'Add Review Url as a comment on PR'
33
author: 'Azure Dev Spaces'
44
inputs:
55
repo-token: # GitHub Authorization Token
6-
description: 'This is GitHub Authorization Token that is available by default as a GitHub secret. Set it's value to ${{ secrets.GITHUB_TOKEN }} in your <workflow>.yml'
6+
description: "This is GitHub Authorization Token that is available by default as a GitHub secret. Set it's value to GITHUB_TOKEN secret in your workflow yml"
77
required: true
88
host: # Complete host.
9-
description: 'Complete host. This expands to: [rootSpace.]webfrontend.<random suffix>.<region>.azds.io. For more information see https://aka.ms/devspaces/routing'
9+
description: "Complete host. This expands to: [rootSpace.]webfrontend.<random suffix>.<region>.azds.io. For more information see https://aka.ms/devspaces/routing"
1010
required: true
1111
runs:
1212
using: 'node12'
13-
main: 'lib/AddReviewUrlEntry.js'
13+
main: 'lib/index.js'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
clearMocks: true,
3+
testEnvironment: 'node',
4+
transform: {
5+
'^.+\\.ts$': 'ts-jest'
6+
},
7+
testMatch: ['**/*.test.ts'],
8+
testRunner: 'jest-circus/runner',
9+
moduleFileExtensions: ['ts', 'js'],
10+
verbose: true
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare class AddComment {
2+
getOwnerAndRepo(): string[];
3+
private addPullRequestCommentMutation;
4+
private getPullNumber;
5+
private findPullRequestQuery;
6+
private addCommentUsingSubjectId;
7+
private getSubjectId;
8+
addComment(comment: string): Promise<{
9+
[key: string]: any;
10+
} | null | undefined>;
11+
}
12+
export default AddComment;
Lines changed: 116 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,116 @@
1-
"use strict";
2-
3-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4-
5-
Object.defineProperty(exports, "__esModule", {
6-
value: true
7-
});
8-
exports["default"] = void 0;
9-
10-
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
11-
12-
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
13-
14-
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15-
16-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17-
18-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
19-
20-
// Copyright (c) Microsoft Corporation. All rights reserved.
21-
// Licensed under the MIT License.
22-
var _require = require('@octokit/graphql'),
23-
graphql = _require.graphql;
24-
25-
var core = require('@actions/core');
26-
27-
var AddComment =
28-
/*#__PURE__*/
29-
function () {
30-
function AddComment(comment) {
31-
(0, _classCallCheck2["default"])(this, AddComment);
32-
this.addComment(comment);
33-
}
34-
35-
(0, _createClass2["default"])(AddComment, [{
36-
key: "getOwnerAndRepo",
37-
value: function getOwnerAndRepo() {
38-
if (process.env.GITHUB_REPOSITORY) {
39-
var _process$env$GITHUB_R = process.env.GITHUB_REPOSITORY.split('/'),
40-
_process$env$GITHUB_R2 = (0, _slicedToArray2["default"])(_process$env$GITHUB_R, 2),
41-
owner = _process$env$GITHUB_R2[0],
42-
repo = _process$env$GITHUB_R2[1];
43-
44-
return {
45-
'owner': owner,
46-
'repo': repo
47-
};
48-
} else {
49-
throw new Error('not able to obtain GITHUB_REPOSITORY from process.env');
50-
}
51-
}
52-
}, {
53-
key: "addPullRequestCommentMutation",
54-
value: function addPullRequestCommentMutation() {
55-
return "mutation AddPullRequestComment($subjectId: ID!, $body: String!) {\n addComment(input:{subjectId:$subjectId, body: $body}) {\n commentEdge {\n node {\n createdAt\n body\n }\n }\n subject {\n id\n }\n }\n }";
56-
}
57-
}, {
58-
key: "getPullNumber",
59-
value: function getPullNumber() {
60-
if (process.env.GITHUB_REF) {
61-
return parseInt(process.env.GITHUB_REF.split('/')[2]);
62-
}
63-
}
64-
}, {
65-
key: "getGraphqlWithAuth",
66-
value: function getGraphqlWithAuth() {
67-
var token = core.getInput('repo-token');
68-
return graphql.defaults({
69-
headers: {
70-
authorization: "token ".concat(token)
71-
}
72-
});
73-
}
74-
}, {
75-
key: "findPullRequestQuery",
76-
value: function findPullRequestQuery() {
77-
return "query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){\n repository(owner:$owner, name:$repo) {\n pullRequest(number:$pullNumber) {\n id\n }\n }\n }";
78-
}
79-
}, {
80-
key: "addCommentUsingSubjectId",
81-
value: function addCommentUsingSubjectId(pullRequestId, comment) {
82-
var obj = JSON.parse(JSON.stringify(pullRequestId));
83-
var graphqlWithAuth = this.getGraphqlWithAuth();
84-
graphqlWithAuth(this.addPullRequestCommentMutation(), {
85-
subjectId: obj.repository.pullRequest.id,
86-
body: comment
87-
});
88-
}
89-
}, {
90-
key: "addComment",
91-
value: function () {
92-
var _addComment = (0, _asyncToGenerator2["default"])(
93-
/*#__PURE__*/
94-
_regenerator["default"].mark(function _callee(comment) {
95-
var nameAndRepo, graphqlWithAuth, findPullRequestIdQuery, subjectId;
96-
return _regenerator["default"].wrap(function _callee$(_context) {
97-
while (1) {
98-
switch (_context.prev = _context.next) {
99-
case 0:
100-
nameAndRepo = this.getOwnerAndRepo();
101-
graphqlWithAuth = this.getGraphqlWithAuth();
102-
findPullRequestIdQuery = this.findPullRequestQuery();
103-
_context.prev = 3;
104-
_context.next = 6;
105-
return this.getSubjectId(graphqlWithAuth, findPullRequestIdQuery, nameAndRepo);
106-
107-
case 6:
108-
subjectId = _context.sent;
109-
_context.next = 9;
110-
return this.addCommentUsingSubjectId(subjectId, comment);
111-
112-
case 9:
113-
_context.next = 13;
114-
break;
115-
116-
case 11:
117-
_context.prev = 11;
118-
_context.t0 = _context["catch"](3);
119-
120-
case 13:
121-
case "end":
122-
return _context.stop();
123-
}
124-
}
125-
}, _callee, this, [[3, 11]]);
126-
}));
127-
128-
function addComment(_x) {
129-
return _addComment.apply(this, arguments);
130-
}
131-
132-
return addComment;
133-
}()
134-
}, {
135-
key: "getSubjectId",
136-
value: function getSubjectId(graphqlWithAuth, findPullRequestIdQuery, nameAndRepo) {
137-
return graphqlWithAuth(findPullRequestIdQuery, {
138-
owner: nameAndRepo.owner,
139-
repo: nameAndRepo.repo,
140-
pullNumber: this.getPullNumber()
141-
});
142-
}
143-
}]);
144-
return AddComment;
145-
}();
146-
147-
var _default = AddComment;
148-
exports["default"] = _default;
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importStar = (this && this.__importStar) || function (mod) {
12+
if (mod && mod.__esModule) return mod;
13+
var result = {};
14+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
15+
result["default"] = mod;
16+
return result;
17+
};
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
// Copyright (c) Microsoft Corporation. All rights reserved.
20+
// Licensed under the MIT License.
21+
const core = __importStar(require("@actions/core"));
22+
const graphql_1 = require("@octokit/graphql");
23+
class AddComment {
24+
getOwnerAndRepo() {
25+
core.debug(`process.env.GITHUB_REPOSITORY ${process.env.GITHUB_REPOSITORY}`);
26+
if (process.env.GITHUB_REPOSITORY) {
27+
return process.env.GITHUB_REPOSITORY.split('/');
28+
}
29+
else {
30+
core.debug('Error in getOwnerAndRepo');
31+
throw new Error('not able to obtain GITHUB_REPOSITORY from process.env');
32+
}
33+
}
34+
addPullRequestCommentMutation() {
35+
return `mutation AddPullRequestComment($subjectId: ID!, $body: String!) {
36+
addComment(input:{subjectId:$subjectId, body: $body}) {
37+
commentEdge {
38+
node {
39+
createdAt
40+
body
41+
}
42+
}
43+
subject {
44+
id
45+
}
46+
}
47+
}`;
48+
}
49+
getPullNumber() {
50+
if (process.env.GITHUB_REF) {
51+
return parseInt(process.env.GITHUB_REF.split('/')[2]);
52+
}
53+
else {
54+
throw new Error('GITHUB_REF is missing in process.env');
55+
}
56+
}
57+
findPullRequestQuery() {
58+
return `query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){
59+
repository(owner:$owner, name:$repo) {
60+
pullRequest(number:$pullNumber) {
61+
id
62+
}
63+
}
64+
}`;
65+
}
66+
addCommentUsingSubjectId(pullRequestId, comment) {
67+
return __awaiter(this, void 0, void 0, function* () {
68+
core.debug(`pullRequestId ===>>>> ${pullRequestId}`);
69+
let data = JSON.parse(JSON.stringify(pullRequestId));
70+
core.debug(`Parsed pull request id ${data}`);
71+
const token = core.getInput('repo-token');
72+
let graphQlResponse = graphql_1.graphql(this.addPullRequestCommentMutation(), {
73+
headers: {
74+
authorization: `token ${token}`,
75+
},
76+
subjectId: data.repository.pullRequest.id,
77+
body: comment,
78+
});
79+
core.debug(`Adding the comment ...`);
80+
return yield graphQlResponse;
81+
});
82+
}
83+
getSubjectId(findPullRequestIdQuery, nameAndRepo) {
84+
return __awaiter(this, void 0, void 0, function* () {
85+
core.debug('Inside getSubjectId');
86+
const token = core.getInput('repo-token');
87+
let newVar = yield graphql_1.graphql(findPullRequestIdQuery, {
88+
headers: {
89+
authorization: `token ${token}`,
90+
},
91+
owner: nameAndRepo[0],
92+
repo: nameAndRepo[1],
93+
pullNumber: this.getPullNumber(),
94+
});
95+
core.debug(`Exiting getSubject Id`);
96+
return newVar;
97+
});
98+
}
99+
addComment(comment) {
100+
return __awaiter(this, void 0, void 0, function* () {
101+
core.debug('Inside addComment');
102+
const nameAndRepo = this.getOwnerAndRepo();
103+
const [name, repo] = nameAndRepo;
104+
core.debug(`Name is ${name} and repo is ${repo}`);
105+
const findPullRequestIdQuery = this.findPullRequestQuery();
106+
try {
107+
const subjectId = yield this.getSubjectId(findPullRequestIdQuery, nameAndRepo);
108+
return yield this.addCommentUsingSubjectId(subjectId, comment);
109+
}
110+
catch (error) {
111+
core.setFailed(error.message);
112+
}
113+
});
114+
}
115+
}
116+
exports.default = AddComment;

actions/add-review-url/lib/AddReviewUrlEntry.js

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

0 commit comments

Comments
 (0)