LCOV - code coverage report
Current view: top level - lib - gh-event.js (source / functions) Hit Total Coverage
Test: lcov.info Lines: 113 123 91.9 %
Date: 2025-02-13 17:12:20 Functions: 6 6 100.0 %
Branches: 19 29 65.5 %

           Branch data     Line data    Source code
       1            [ + ]:         59 : /**
       2                 :         59 :  * @module  github-event
       3                 :         59 :  * @desc    A module to define GitHub related classes.
       4                 :         59 :  * @version 1.0.0
       5                 :         59 :  * @author  Essam A. El-Sherif
       6                 :         59 :  */
       7                 :         59 : 
       8                 :         59 : /**
       9                 :         59 :  * @name main
      10                 :         59 :  * @function
      11                 :         59 :  * @desc Self running expression for testing purpose.
      12                 :         59 :  */
      13            [ + ]:         59 : (() => {
      14                 :         59 :         if(process.env.GH_ACT_TEST === 'testGitHubEvent'){
      15                 :         59 :                 let input = '';
      16                 :         59 : 
      17            [ + ]:         59 :                 process.stdin.on('data', (chunk) => {
      18                 :          2 :                         input += chunk;
      19                 :         59 :                 });
      20                 :         59 : 
      21            [ + ]:         59 :                 process.stdin.on('end', () => {
      22                 :          2 :                         let event = new GitHubEvent(JSON.parse(input));
      23                 :          2 :                         process.stdout.write(event.phrase);
      24                 :         59 :                 });
      25                 :         59 :         }
      26                 :         59 : })('Test Function');
      27                 :         59 : 
      28                 :         59 : /**
      29                 :         59 :  * @class  GitHubEvent
      30                 :         59 :  * @static
      31                 :         59 :  * @desc   A class to represent a GitHub event.
      32                 :         59 :  * @see    {@link https://docs.github.com/en/rest/using-the-rest-api/github-event-types GitHub event types}
      33                 :         59 :  */
      34                 :         59 : export class GitHubEvent{
      35                 :         59 : 
      36                 :         59 :         /**
      37                 :         59 :          * @method   constructor
      38                 :         59 :          * @instance
      39                 :         59 :          * @memberof module:github-event.GitHubEvent
      40                 :         59 :          * @param    {object} obj - Object that respresents a GitHub event.
      41                 :         59 :          * @return   {object} GitHubEvent object.
      42                 :         59 :          * @desc     Constructs a new GitHubEvent object.
      43                 :         59 :          */
      44            [ + ]:         59 :         constructor(obj){
      45                 :        122 : 
      46                 :        122 :                 /** @member {integer} id - The unique identifier for the event. */
      47                 :        122 :                 this['id'] = obj['id'];
      48                 :        122 : 
      49                 :        122 :                 /** @member {string} type - Type of event. Events uses PascalCase for the name. */
      50                 :        122 :                 this['type'] = obj['type'];
      51                 :        122 : 
      52                 :        122 :                 /** @member {object} actor -  User that triggered the event. */
      53                 :        122 :                 this['actor'] = Object.assign({}, obj['actor']);
      54                 :        122 : 
      55                 :        122 :                 /** @member {object} repo - Repository object where the event occurred. */
      56                 :        122 :                 this['repo'] = Object.assign({}, obj['repo']);
      57                 :        122 : 
      58                 :        122 :                 /** @member {object} payload - Payload object is unique to the event type. */
      59                 :        122 :                 this['payload'] = Object.assign({}, obj['payload']);
      60                 :        122 : 
      61                 :        122 :                 /** @member {boolean} public - Whether the event is visible to all users. */
      62                 :        122 :                 this['public'] = obj['public'];
      63                 :        122 : 
      64                 :        122 :                 /** @member {string} created_at - Date and time when the event was triggered formatted according to ISO 8601. */
      65                 :        122 :                 this['created_at'] = obj['created_at'];
      66                 :        122 : 
      67                 :        122 :                 /** @member {object} org - (Optional) Organization that was chosen by the actor to perform action that triggers the event. */
      68                 :        122 :                 this['org'] = Object.assign({}, obj['org']);
      69                 :        122 :         }
      70                 :         59 : 
      71                 :         59 :         /**
      72                 :         59 :          * @member   {string} phrase
      73                 :         59 :          * @memberof module:github-event.GitHubEvent
      74                 :         59 :          * @readonly
      75                 :         59 :          * @instance
      76                 :         59 :          * @desc     Describe the GitHubEvent.
      77                 :         59 :          */
      78            [ + ]:         59 :         get phrase(){
      79                 :        122 : 
      80                 :        122 :                 switch(this.type){
      81            [ - ]:        122 :                         case 'CommitCommentEvent':
      82                 :          0 :                                 return `${this._toCamelCase(this.payload.action)} a commit comment in repository ${this.repo.name}`;
      83            [ + ]:        122 :                         case 'CreateEvent':
      84       [ + ][ + ]:         10 :                                 return `Created ${this.payload.ref_type === 'repository' ? '' : this.payload.ref_type + ' ' + this.payload.ref + ' in '}repository ${this.repo.name}`;
      85            [ + ]:        122 :                         case 'DeleteEvent':
      86                 :          2 :                                 return `Deleted ${this.payload.ref_type + ' ' + this.payload.ref + ' in '}repository ${this.repo.name}`;
      87            [ + ]:        122 :                         case 'ForkEvent':
      88                 :          4 :                                 return `Forked repository ${this.repo.name}`;
      89            [ - ]:        122 :                         case 'GollumEvent':
      90                 :          0 :                                 return `Created or Updated a Wiki page for repository ${this.repo.name}`;
      91            [ + ]:        122 :                         case 'IssueCommentEvent':
      92                 :          6 :                                 return `${this._toCamelCase(this.payload.action)} a comment on an issue in repository ${this.repo.name}`;
      93            [ - ]:        122 :                         case 'IssuesEvent':
      94                 :          0 :                                 return `${this._toCamelCase(this.payload.action)} an issue in repository ${this.repo.name}`;
      95            [ - ]:        122 :                         case 'MemberEvent':
      96                 :          0 :                                 return `Added a member to or edited a member permissions for collaborators of repository ${this.repo.name}`;
      97            [ - ]:        122 :                         case 'PublicEvent':
      98                 :          0 :                                 return `Private repository ${this.repo.name} is made public.`;
      99            [ + ]:        122 :                         case 'PullRequestEvent':
     100                 :          4 :                                 return `${this._toCamelCase(this.payload.action)} a pull request in repository ${this.repo.name}`;
     101            [ - ]:        122 :                         case 'PullRequestReviewEvent':
     102                 :          0 :                                 return `${this._toCamelCase(this.payload.action)} a pull request review in repository ${this.repo.name}`;
     103            [ - ]:        122 :                         case 'PullRequestReviewCommentEvent':
     104                 :          0 :                                 return `${this._toCamelCase(this.payload.action)} a pull request review comment in repository ${this.repo.name}`;
     105            [ - ]:        122 :                         case 'PullRequestReviewThreadEvent':
     106                 :          0 :                                 return `${this._toCamelCase(this.payload.action)} a pull request review thread in repository ${this.repo.name}`;
     107            [ + ]:        122 :                         case 'PushEvent':
     108       [ + ][ + ]:         80 :                                 return `Pushed ${this.payload.size} commit${this.payload.size === 1 ? '' : 's'} to repository ${this.repo.name}`;
     109            [ + ]:        122 :                         case 'ReleaseEvent':
     110                 :          2 :                                 return `${this._toCamelCase(this.payload.action)} repository ${this.repo.name}`;
     111            [ - ]:        122 :                         case 'SponsorshipEvent':
     112                 :          0 :                                 return `SponsorshipEvent pertaining to ${this.repo.name}`;
     113            [ + ]:        122 :                         case 'WatchEvent':
     114                 :         12 :                                 return `Starred repository ${this.repo.name}`;
     115            [ - ]:        122 :                         default:
     116                 :          0 :                                 return '';
     117                 :        122 :                 }
     118                 :        122 :         }
     119                 :         59 : 
     120            [ + ]:         59 :         _toCamelCase(str){
     121                 :         12 :                 return str[0].toUpperCase() + str.substring(1);
     122                 :         12 :         }
     123                 :         59 : }

Generated by: LCOV version 1.14