Skip to content

Commit 007a197

Browse files
committed
Merge pull request #43 from inaka/davecaos.42.Restric_gh_status_api_descr_140
[#42] Add fix for error 422 [The message submitted is longer that the ma...
2 parents 5a52f34 + c5f3278 commit 007a197

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/egithub.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
application, egithub,
33
[
44
{description, "GitHub API client"},
5-
{vsn, "0.1.12"},
5+
{vsn, "0.1.13"},
66
{applications, [kernel, stdlib, ibrowse, ssl, jiffy, lager]},
77
{mod, {egithub, []}},
88
{modules, []},

src/egithub.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
-type result() :: ok | {ok, term()} | {error, term()}.
7878

7979
-define(GITHUB_API, "https://api.github.com").
80+
-define(MAX_DESCRIPTION_LENGTH, 140).
8081

8182
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8283
%% Public API
@@ -434,8 +435,9 @@ remove_collaborator(Cred, Repo, Collaborator) ->
434435
result().
435436
create_status(Cred, Repo, Sha, State, Description, Context) ->
436437
Url = make_url(new_status, {Repo, Sha}),
438+
FormatDescription = format_description(Description),
437439
Data = #{<<"state">> => State,
438-
<<"description">> => list_to_binary(Description),
440+
<<"description">> => list_to_binary(FormatDescription),
439441
<<"context">> => list_to_binary(Context)
440442
},
441443
Body = egithub_json:encode(Data),
@@ -480,6 +482,17 @@ combined_status(Cred, Repo, Ref) ->
480482
%% Private Functions
481483
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482484

485+
%doc format_description format the description to avoid error 422
486+
%doc The message submitted is longer that the maximum length of 140 characters
487+
-spec format_description(string()) -> string().
488+
format_description(Description) ->
489+
case length(Description) of
490+
Size when Size >= ?MAX_DESCRIPTION_LENGTH ->
491+
%% to be continued.
492+
string:sub_string(Description, 1, ?MAX_DESCRIPTION_LENGTH - 3) ++ "...";
493+
Size when Size < ?MAX_DESCRIPTION_LENGTH -> Description
494+
end.
495+
483496
%% Create Status
484497
make_url(new_status, {Repo, Sha}) ->
485498
Url = ?GITHUB_API ++ "/repos/~s/statuses/~s",

0 commit comments

Comments
 (0)